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

100% Statements 38/38
100% Branches 18/18
100% Functions 11/11
100% Lines 37/37

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                                                              1x                 10x 10x     10x   10x 10x   10x 10x   10x 10x     10x 2x 1x 1x   1x     10x                                       1x 12x 12x 12x   12x   12x 2x             10x                             1x 12x 12x     12x 12x             12x 12x   12x 12x               12x 12x                   12x                     22x                                    
import React, { useCallback, useMemo } from 'react';
import { ActivityIndicator, useColorScheme, View } from 'react-native';
 
import { HeaderRequest } from '@repo/ui/components/HeaderRequest';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import {
  darkTheme,
  darkV2Theme,
  lightTheme,
  lightV2Theme,
  ThemeContext,
  useTheme,
} from '@repo/ui/themes/ThemeContext';
 
import { useGetMeInfo, useGetMyMealRegistration } from '@repo/hooks';
import { ApiQueryClientsProvider } from '@repo/hooks/useApiClients';
 
import MealRegistrationScreen from '../MealRegistration';
 
import {
  MealReservationContextProvider,
  useMealReservationActionsSelector,
  useMealReservationStateSelector,
} from './context';
import {
  createMealReservationRuntimeValue,
  MealReservationRuntimeProvider,
  useMealReservationRuntime,
} from './runtime';
import { MealReservationRemoteProps } from './types';
 
const ScreenWrapper = ({
  children,
  title,
  description,
}: {
  children: React.ReactNode;
  title: string;
  description?: string;
}) => {
  const styles = useStyles();
  const { theme } = useTheme();
  const {
    actions: { onClose },
  } = useMealReservationRuntime();
 
  const isSubmitting = useMealReservationStateSelector(
    state => state.isSubmitting,
  );
  const isSearchOpen = useMealReservationStateSelector(
    state => state.isSearchOpen,
  );
  const toggleSearch = useMealReservationActionsSelector(
    actions => actions.toggleSearch,
  );
 
  const handleClose = useCallback(() => {
    if (isSearchOpen) {
      toggleSearch(false);
      return;
    }
    onClose();
  }, [isSearchOpen, onClose, toggleSearch]);
 
  return (
    <View style={styles.container}>
      <View style={styles.header}>
        <HeaderRequest
          title={title}
          image={theme.assets.mealMascot}
          description={description}
          isShowBackButton={false}
          isLoading={isSubmitting}
          onClose={handleClose}
          imageWidth={135}
          imageHeight={160}
        />
      </View>
 
      {children}
    </View>
  );
};
 
const MealReservationFlow = () => {
  const styles = useStyles();
  const { data: meInfo, isLoading: isLoadingMe } = useGetMeInfo();
  const meEmail = meInfo?.email || '';
  const { data: registration, isLoading: isLoadingReg } =
    useGetMyMealRegistration(meEmail, !!meEmail);
 
  if (isLoadingMe || isLoadingReg) {
    return (
      <View style={styles.loader}>
        <ActivityIndicator size="large" />
      </View>
    );
  }
 
  return (
    <MealReservationContextProvider
      initialRegistration={registration}
      meEmail={meEmail}
    >
      <ScreenWrapper
        title="MEAL RESERVATION"
        description="I would like to reserve meals"
      >
        <MealRegistrationScreen />
      </ScreenWrapper>
    </MealReservationContextProvider>
  );
};
 
const MealReservation = (props: MealReservationRemoteProps) => {
  const runtimeValue = useMemo(
    () => createMealReservationRuntimeValue(props),
    [props],
  );
  const apiQueryClients = useMemo(
    () => ({
      mainHttp: runtimeValue.meta.api.mainHttp,
      spaceHttp: runtimeValue.meta.api.spaceHttp,
    }),
    [runtimeValue],
  );
 
  const isNewTheme = props.featureFlags?.isNewTheme ?? false;
  const colorScheme = useColorScheme();
  const resolvedMode: 'light' | 'dark' =
    colorScheme === 'dark' ? 'dark' : 'light';
  const theme = isNewTheme
    ? resolvedMode === 'dark'
      ? darkV2Theme
      : lightV2Theme
    : resolvedMode === 'dark'
      ? darkTheme
      : lightTheme;
 
  const themeContextValue = useMemo(
    () => ({
      theme,
      mode: 'system' as const,
      resolvedMode,
      isDark: resolvedMode === 'dark',
      isNewTheme,
    }),
    [theme, resolvedMode, isNewTheme],
  );
 
  return (
    <ThemeContext.Provider value={themeContextValue}>
      <MealReservationRuntimeProvider value={runtimeValue}>
        <ApiQueryClientsProvider value={apiQueryClients}>
          <MealReservationFlow />
        </ApiQueryClientsProvider>
      </MealReservationRuntimeProvider>
    </ThemeContext.Provider>
  );
};
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    backgroundColor: theme.colors.background.pageMuted,
  },
  loader: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  header: {
    paddingHorizontal: theme.metrics.spacing[4],
    paddingTop: theme.metrics.spacing[4],
    backgroundColor: theme.colors.background.pageMuted,
  },
}));
 
export default MealReservation;