All files / apps/reallocation/src/screens/MealReservation index.tsx

93.1% Statements 27/29
92.3% Branches 24/26
71.42% Functions 5/7
96.29% Lines 26/27

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 167 168 169 170                                            1x 7x 7x   7x     7x       7x                 7x           7x   7x 7x 7x   7x 7x 6x             7x 7x 7x 7x 7x                 7x 7x             7x   7x 1x             6x                                                                               7x                                                                      
import { useEffect } from 'react';
import { useForm } from 'react-hook-form';
import { ActivityIndicator, ScrollView, Text, View } from 'react-native';
 
import { FormSwitch } from '@repo/ui/components/Form/FormSwitch';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import {
  useGetMeInfo,
  useGetMyMealRegistration,
} from '@repo/hooks/useUserQueries';
 
import { MealReservationFormType } from '@repo/types/form';
 
import { getUpdateTimeMessage } from '@repo/utils/date';
 
import {
  useReallocationActionsSelector,
  useReallocationStateSelector,
} from '../Reallocation/context';
 
const MealReservation = () => {
  const { theme } = useTheme();
  const styles = useStyles();
 
  const { form } = useReallocationStateSelector(state => ({
    form: state.form,
  }));
  const changeMealConfig = useReallocationActionsSelector(
    actions => actions.changeMealConfig,
  );
 
  const { control, watch, setValue } = useForm<MealReservationFormType>({
    defaultValues: {
      breakfast: false,
      lunch: false,
      vegetarian: false,
      office: '',
    },
  });
 
  const { data: meInfo, isLoading: isLoadingMeInfo } = useGetMeInfo();
 
  const {
    data: registrationData,
    isLoading: isMealRegistrationLoading,
    isError: isMealRegistrationError,
  } = useGetMyMealRegistration(meInfo?.id || '');
 
  const lunchValue = watch('lunch');
  const breakfastValue = watch('breakfast');
  const vegetarianValue = watch('vegetarian');
 
  useEffect(() => {
    if (!lunchValue) {
      setValue('vegetarian', false, {
        shouldDirty: true,
        shouldTouch: false,
      });
    }
  }, [lunchValue, setValue]);
 
  useEffect(() => {
    setValue('breakfast', registrationData?.breakfast || false);
    setValue('lunch', registrationData?.lunch || false);
    setValue('vegetarian', registrationData?.vegetarian || false);
    setValue('office', registrationData?.office || '');
  }, [
    registrationData?.breakfast,
    registrationData?.lunch,
    registrationData?.office,
    registrationData?.vegetarian,
    setValue,
  ]);
 
  useEffect(() => {
    changeMealConfig({
      breakfast: breakfastValue || false,
      lunch: lunchValue || false,
      vegetarian: vegetarianValue || false,
    });
  }, [breakfastValue, lunchValue, vegetarianValue, changeMealConfig]);
 
  const isLoading = isLoadingMeInfo || isMealRegistrationLoading;
 
  if (isLoading) {
    return (
      <View style={styles.overlay} testID="meal-loader">
        <ActivityIndicator color={theme.colors.icon.active} />
      </View>
    );
  }
 
  return (
    <ScrollView style={styles.container}>
      {isMealRegistrationError && (
        <Text>Failed to load. Please try again later.</Text>
      )}
      <View style={styles.mealGroup}>
        <FormSwitch
          control={control}
          name="breakfast"
          label="BREAKFAST"
          labelStyle={styles.mealTitle}
          testID="breakfast-registration-switch"
        />
      </View>
      <View style={styles.mealGroup}>
        <FormSwitch
          control={control}
          name="lunch"
          label="LUNCH"
          labelStyle={styles.mealTitle}
          testID="lunch-registration-switch"
        />
        <FormSwitch
          control={control}
          name="vegetarian"
          label="I'm vegan"
          labelStyle={styles.veganLabel}
          testID="vegetarian-registration-switch"
          disabled={!lunchValue}
        />
      </View>
      <View style={styles.infoBox}>
        <Text style={styles.infoText}>
          {getUpdateTimeMessage(form.date?.toISOString() || '')}
        </Text>
      </View>
    </ScrollView>
  );
};
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    paddingHorizontal: theme.metrics.spacing[4],
  },
  mealTitle: {
    fontSize: theme.metrics.textSize[24],
    fontWeight: '700' as const,
    color: theme.colors.text.primary,
  },
  veganLabel: {
    color: theme.colors.text.primary,
  },
  mealGroup: {
    marginTop: theme.metrics.spacing[4],
    gap: theme.metrics.spacing[2],
  },
  infoBox: {
    marginTop: theme.metrics.spacing[4],
    padding: theme.metrics.spacing[4],
    borderRadius: theme.metrics.borderRadius[4],
    backgroundColor: theme.colors.background.tertiary,
  },
  infoText: {
    fontSize: theme.metrics.textSize[16],
    color: theme.colors.text.primary,
  },
  overlay: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
  },
}));
 
export default MealReservation;