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 184 185 186 187 188 189 190 191 192 | 3x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 1x 1x 8x 8x 8x 8x 8x 1x 1x 1x 8x 8x 8x 8x 8x 3x | import React, { useCallback, useEffect, useRef, useState } from 'react';
import ErrorBoundary from 'react-native-error-boundary';
import Toast from 'react-native-toast-message';
import { StyleSheet, Text, View } from 'react-native';
import { useFocusEffect } from '@react-navigation/native';
import { SCREENS } from '@repo/constants/screens';
import { useEnableOldUI } from '@repo/hooks';
import { useGetExtraUsers } from '@repo/hooks';
import { MealReservationFormType } from '@repo/types/form';
import { getErrorMessage } from '@repo/utils/error';
import { FallbackError } from '@/components/FallbackError';
import { LoadingSlider } from '@/components/LoadingSlider';
import { useAuth } from '@/contexts/AuthContext';
import { useChat } from '@/contexts/ChatContext';
import { useBlockBackNavigationWhileSubmitting } from '@/hooks/useBlockNavigation';
import { useRegistrationData } from '@/hooks/useRegistrationData';
import { useMealReservationRequest } from '@/hooks/useRequest';
import { useGetRooms } from '@/hooks/useRoom';
import type { AppStackScreenProps } from '@/types/navigation';
import type { MealReservationRequestParams } from '@/types/request';
import { UserRole } from '@/types/user';
import { sentryService } from '@/services/sentryService';
const MealReservationRemote = React.lazy(
// @ts-ignore
() => import('mealReservation/MealReservation'),
);
type LazyLoadMealReservationScreenProps = AppStackScreenProps<typeof SCREENS.MEAL_RESERVATION>;
export const LazyLoadMealReservationScreen = ({
navigation,
}: LazyLoadMealReservationScreenProps) => {
const { user } = useAuth();
const { setFabOffset } = useChat();
const { value: enableOldUI } = useEnableOldUI(user?.email);
const [id, setId] = useState<string>(user?.email || '');
const handleChange = (useId: string) => setId(useId);
const handleOnBehalfOff = () => setId(user?.email || id);
// Ref to store the close modals function from MealReservation component
const closeModalsRef = useRef<(() => void) | null>(null);
// Sync id with user?.email when it becomes available
React.useEffect(() => {
Iif (user?.email && !id) {
setId(user.email);
}
}, [user?.email, id]);
// Close modals when screen loses focus (e.g., when navigating away via notification)
const {
meInfo,
registration,
isLoading: isLoadingData,
isError: isDataError,
isLoadingRegistration,
isRegistrationError,
refetchMeInfo,
} = useRegistrationData(id);
const isManager = meInfo?.groups?.includes(UserRole.ON_BEHALF_MANAGER) || false;
const {
data: extraUsers,
isLoading: isLoadingExtraUsers,
isError: isErrorExtraUsers,
} = useGetExtraUsers(!isManager);
const { data: rooms, isLoading: isLoadingRooms, isError: isRoomsError } = useGetRooms(!isManager);
useFocusEffect(
useCallback(() => {
refetchMeInfo();
return () => {
// This runs when the screen loses focus
closeModalsRef.current?.();
};
}, [refetchMeInfo]),
);
const { mutate, isPending: isSubmitting } = useMealReservationRequest(
id,
() => {
Toast.show({
type: 'success',
text1: 'Meal reservation request created successfully',
});
if (enableOldUI) {
navigation.navigate(SCREENS.NEW_REQUEST);
} else {
navigation.navigate(SCREENS.HOME_V2);
}
},
(error: unknown) => {
const apiMessage = getErrorMessage(error);
Toast.show({
type: 'error',
text1: 'Failed to create meal reservation request',
...(apiMessage && { text2: apiMessage }),
});
},
);
useBlockBackNavigationWhileSubmitting(navigation, isSubmitting);
const extraUsersOptions = (extraUsers || []).map((item: { id: string; name: string }) => ({
id: item.id,
name: item.name,
}));
const offices = [...new Set((rooms || []).map(item => item.office).filter(Boolean))];
const handleBack = () => {
navigation.goBack();
};
const handleSubmit = (formData: MealReservationFormType) => {
const dataToSubmit: MealReservationRequestParams = {
breakfast: formData.breakfast,
lunch: formData.lunch,
vegetarian: formData.vegetarian,
...(formData.office && id !== user?.email && { office: formData.office }),
...(formData.user && id !== user?.email && { user: formData.user }),
};
console.log(dataToSubmit);
mutate(dataToSubmit);
};
const isLoading = [
isLoadingData,
isLoadingExtraUsers,
isLoadingRooms,
isLoadingRegistration,
].some(Boolean);
const isError = [isDataError, isErrorExtraUsers, isRoomsError, !!isRegistrationError].some(
Boolean,
);
useEffect(() => {
setFabOffset(20);
}, [setFabOffset]);
return (
<View style={styles.container}>
{isError && <Text>Something went wrong</Text>}
<ErrorBoundary
FallbackComponent={FallbackError}
onError={error => sentryService.captureException(error)}
>
<React.Suspense fallback={<LoadingSlider />}>
<MealReservationRemote
extraUsers={extraUsersOptions}
office={registration?.office}
offices={offices}
breakfast={registration?.breakfast}
lunch={registration?.lunch}
vegetarian={registration?.vegetarian}
isManager={isManager}
isSubmitting={isSubmitting}
isLoadingRegistration={isLoading}
allowEmptyChange={false}
onBehalfOff={handleOnBehalfOff}
onSelectUser={handleChange}
onBack={handleBack}
onSubmit={handleSubmit}
onScreenBlur={(closeModals: () => void) => {
closeModalsRef.current = closeModals;
}}
/>
</React.Suspense>
</ErrorBoundary>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
|