All files / apps/host/src/screens/LazyLoadMaintenance index.tsx

80% Statements 28/35
83.33% Branches 10/12
50% Functions 6/12
78.78% Lines 26/33

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                                                                3x         3x 8x 8x 8x   8x     8x                         8x   12x         8x   2x       2x 1x       1x                         8x   8x       8x 2x 1x     8x 1x               1x     8x 8x     8x                                                 3x          
import React, { useCallback, useEffect, useRef } 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/flags';
 
import { MaintenanceFormType } from '@repo/types/form';
 
import { getErrorMessage } from '@repo/utils/error';
 
import { FallbackError } from '@/components/FallbackError';
import { FallbackLoadRemote } from '@/components/FallbackLoadRemote';
import { LoadingSlider } from '@/components/LoadingSlider';
 
import { useAuth } from '@/contexts/AuthContext';
import { useChat } from '@/contexts/ChatContext';
 
import { useBlockBackNavigationWhileSubmitting } from '@/hooks/useBlockNavigation';
import { useCreateRequest, useGetMaintenanceScopes } from '@/hooks/useRequest';
 
import type { AppStackScreenProps } from '@/types/navigation';
import { type MaintenanceRequestParams, RequestCategory } from '@/types/request';
 
import { sentryService } from '@/services/sentryService';
 
type LazyLoadMaintenanceScreenProps = AppStackScreenProps<typeof SCREENS.MAINTENANCE>;
 
const MaintenanceRemote = React.lazy(
  // @ts-ignore
  () => import('maintenance/Maintenance'),
);
 
export const LazyLoadMaintenanceScreen = ({ navigation }: LazyLoadMaintenanceScreenProps) => {
  const { user } = useAuth();
  const { setFabOffset } = useChat();
  const { value: enableOldUI } = useEnableOldUI(user?.email);
  // Ref to store the close modals function from RoomBooking component
  const closeModalsRef = useRef<(() => void) | null>(null);
 
  // Close modals when screen loses focus (e.g., when navigating away via notification)
  useFocusEffect(
    useCallback(() => {
      return () => {
        // This runs when the screen loses focus
        closeModalsRef.current?.();
      };
    }, []),
  );
 
  const {
    data: scopes,
    isLoading: isLoadingScopes,
    isError: isScopesError,
  } = useGetMaintenanceScopes();
 
  const formatScopes = (scopes || []).map(scope => ({
    label: scope.id,
    value: scope.id,
  }));
 
  const { mutate, isPending: isSubmitting } = useCreateRequest(
    () => {
      Toast.show({
        type: 'success',
        text1: 'Maintenance request created successfully',
      });
      if (enableOldUI) {
        navigation.navigate(SCREENS.BOTTOM_TAB, {
          screen: SCREENS.MY_REQUEST,
        });
      } else {
        navigation.navigate(SCREENS.HOME_V2);
      }
    },
    (error: unknown) => {
      const apiMessage = getErrorMessage(error);
      Toast.show({
        type: 'error',
        text1: 'Failed to create maintenance request',
        ...(apiMessage && { text2: apiMessage }),
      });
    },
  );
 
  useBlockBackNavigationWhileSubmitting(navigation, isSubmitting);
 
  const handleScreenBlur = (closeModals: () => void) => {
    closeModalsRef.current = closeModals;
  };
 
  const handleBack = () => {
    if (isSubmitting) return;
    navigation.goBack();
  };
 
  const handleSubmit = (formData: MaintenanceFormType) => {
    const dataToSubmit: MaintenanceRequestParams = {
      category: RequestCategory.MAINTENANCE,
      data: {
        scope: formData.scope,
        note: formData.note,
      },
    };
 
    mutate(dataToSubmit);
  };
 
  useEffect(() => {
    setFabOffset(20);
  }, [setFabOffset]);
 
  return (
    <View style={styles.container}>
      {isScopesError && <Text>Something went wrong</Text>}
      {isLoadingScopes ? (
        <LoadingSlider />
      ) : (
        <ErrorBoundary
          FallbackComponent={FallbackError}
          onError={error => sentryService.captureException(error)}
        >
          <React.Suspense fallback={<FallbackLoadRemote />}>
            <MaintenanceRemote
              scopes={formatScopes}
              isSubmitting={isSubmitting}
              onSubmit={handleSubmit}
              onScreenBlur={handleScreenBlur}
              onBack={handleBack}
            />
          </React.Suspense>
        </ErrorBoundary>
      )}
    </View>
  );
};
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});