All files / apps/mealReservation/src/screens/MealReservation context.tsx

100% Statements 35/35
100% Branches 30/30
100% Functions 16/16
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                    1x                               1x                   1x                           32x                 32x 32x   32x   8x                     32x   2x                   32x 1x           32x   1x               32x 2x     32x   5x 5x 4x 4x 2x               2x             32x 32x               32x 14x                                   32x   32x            
import React, { useCallback, useMemo, useState } from 'react';
 
import { createScopedContext } from '@repo/hooks/createScopedContext';
 
import {
  MealReservationActions,
  MealReservationForm,
  MealReservationState,
} from './types';
 
const INITIAL_FORM: MealReservationForm = {
  breakfast: false,
  lunch: false,
  vegetarian: false,
  isOnBehalf: false,
  user: '',
  userName: '',
  userAvatar: '',
  office: '',
};
 
const {
  Context: MealReservationContext,
  useSelector: useMealReservationSelector,
  useStateSelector: useMealReservationStateSelector,
  useActionsSelector: useMealReservationActionsSelector,
} = createScopedContext<MealReservationState, MealReservationActions>(
  'MealReservation context hooks must be used within MealReservationContextProvider',
);
 
export {
  useMealReservationActionsSelector,
  useMealReservationSelector,
  useMealReservationStateSelector,
};
 
export const MealReservationContextProvider = ({
  children,
  initialRegistration,
  meEmail,
}: {
  children: React.ReactNode;
  initialRegistration?: {
    breakfast: boolean;
    lunch: boolean;
    vegetarian: boolean;
    office: string;
  };
  meEmail?: string;
}) => {
  const [form, setForm] = useState<MealReservationForm>(() => ({
    ...INITIAL_FORM,
    breakfast: initialRegistration?.breakfast ?? false,
    lunch: initialRegistration?.lunch ?? false,
    vegetarian: initialRegistration?.vegetarian ?? false,
    office: initialRegistration?.office ?? '',
    user: meEmail || '',
    userName: meEmail ? 'Me' : '',
  }));
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [isSearchOpen, setIsSearchOpen] = useState(false);
 
  const toggleOnBehalf = useCallback(
    (isOnBehalf: boolean) => {
      setForm(prev => ({
        ...prev,
        isOnBehalf,
        user: isOnBehalf ? '' : meEmail || '',
        userName: isOnBehalf ? '' : 'Me',
        userAvatar: '',
      }));
    },
    [meEmail],
  );
 
  const selectEmployee = useCallback(
    (employee: { id: string; name: string; avatar?: string }) => {
      setForm(prev => ({
        ...prev,
        user: employee.id,
        userName: employee.name,
        userAvatar: employee.avatar || '',
      }));
    },
    [],
  );
 
  const selectOffice = useCallback((office: string) => {
    setForm(prev => ({
      ...prev,
      office,
    }));
  }, []);
 
  const updateMealConfig = useCallback(
    (config: { breakfast: boolean; lunch: boolean; vegetarian: boolean }) => {
      setForm(prev => ({
        ...prev,
        ...config,
      }));
    },
    [],
  );
 
  const setSubmitting = useCallback((submitting: boolean) => {
    setIsSubmitting(submitting);
  }, []);
 
  const toggleSearch = useCallback(
    (open: boolean) => {
      setIsSearchOpen(open);
      if (!open) {
        setForm(prev => {
          if (prev.isOnBehalf && !prev.user) {
            return {
              ...prev,
              isOnBehalf: false,
              user: meEmail || '',
              userName: meEmail ? 'Me' : '',
              userAvatar: '',
            };
          }
          return prev;
        });
      }
    },
    [meEmail],
  );
 
  const state = useMemo<MealReservationState>(
    () => ({
      form,
      isSubmitting,
      isSearchOpen,
    }),
    [form, isSubmitting, isSearchOpen],
  );
 
  const actions = useMemo<MealReservationActions>(
    () => ({
      toggleOnBehalf,
      selectEmployee,
      selectOffice,
      updateMealConfig,
      setSubmitting,
      toggleSearch,
    }),
    [
      toggleOnBehalf,
      selectEmployee,
      selectOffice,
      updateMealConfig,
      setSubmitting,
      toggleSearch,
    ],
  );
 
  const value = useMemo(() => ({ state, actions }), [state, actions]);
 
  return (
    <MealReservationContext.Provider value={value}>
      {children}
    </MealReservationContext.Provider>
  );
};