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

100% Statements 29/29
100% Branches 26/26
100% Functions 7/7
100% Lines 27/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 171 172 173 174 175 176 177 178 179 180 181 182 183                                              1x 9x 9x   9x     9x 9x     9x                 9x           9x   9x 9x 9x   9x 9x 8x             9x 9x 9x 9x 9x                 9x 9x             9x   9x 1x             8x                                                                               9x                                                                                              
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 { textStyles } from '@repo/ui/themes/typography';
 
import {
  useGetMeInfo,
  useGetMyMealRegistration,
} from '@repo/hooks/useUserQueries';
 
import { MealReservationFormType } from '@repo/types/form';
 
import {
  useReallocationActionsSelector,
  useReallocationStateSelector,
} from '../Reallocation/context';
 
import { getUpdateTimeMessage } from './utils';
 
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 style={styles.errorText}>
          Failed to load. Please try again later.
        </Text>
      )}
      <View style={styles.mealCard}>
        <FormSwitch
          control={control}
          name="breakfast"
          label="BREAKFAST"
          labelStyle={styles.mealTitle}
          testID="breakfast-registration-switch"
        />
        <FormSwitch
          control={control}
          name="lunch"
          label="LUNCH"
          labelStyle={styles.mealTitle}
          testID="lunch-registration-switch"
        />
        <FormSwitch
          control={control}
          name="vegetarian"
          label="Vegan meal"
          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: {
    fontWeight: theme.metrics.fontWeight.semiBold,
    textTransform: 'uppercase',
    color: theme.colors.text.onBehalf,
    fontSize: theme.metrics.textSize[32],
    ...textStyles.content.semiBold,
  },
  veganLabel: {
    ...textStyles.content.regular,
    fontSize: theme.metrics.textSize[16],
    fontWeight: theme.metrics.fontWeight.normal,
    color: theme.colors.text.primary,
  },
  mealCard: {
    padding: theme.metrics.spacing[4],
    borderRadius: theme.metrics.borderRadius[4],
    backgroundColor: theme.colors.background.surface,
    gap: theme.metrics.spacing[3],
  },
  infoBox: {
    marginTop: theme.metrics.spacing[4],
    padding: theme.metrics.spacing[4],
    borderRadius: theme.metrics.borderRadius[4],
    backgroundColor: theme.colors.background.surface,
  },
  infoText: {
    ...textStyles.content.medium,
    fontSize: theme.metrics.textSize[16],
    textAlign: 'center',
    color: theme.colors.text.onBehalf,
  },
  overlay: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
  },
  errorText: {
    ...textStyles.content.regular,
  },
}));
 
export default MealReservation;