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

78.12% Statements 25/32
80% Branches 8/10
53.84% Functions 7/13
75.86% Lines 22/29

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                                                        2x         2x 7x   7x     7x                         7x   10x         7x   1x       1x                       7x   7x       7x 2x 1x     7x 1x               1x     7x 7x     7x                                                 2x          
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 { MaintenanceFormType } from '@repo/types/form';
 
import { getErrorMessage } from '@repo/utils/error';
import { useBlockBackNavigation } from '@repo/utils/useBlockBackNavigation';
 
import { FallbackError } from '@/components/FallbackError';
import { LoadingSlider } from '@/components/LoadingSlider';
 
import { useChatActionsSelector } from '@/contexts/ChatContext';
 
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 setFabOffset = useChatActionsSelector(a => a.setFabOffset);
  // 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',
      });
      navigation.navigate(SCREENS.HOME);
    },
    (error: unknown) => {
      const apiMessage = getErrorMessage(error);
      Toast.show({
        type: 'error',
        text1: 'Failed to create maintenance request',
        ...(apiMessage && { text2: apiMessage }),
      });
    },
  );
 
  useBlockBackNavigation(isSubmitting, { navigation });
 
  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={<LoadingSlider />}>
            <MaintenanceRemote
              scopes={formatScopes}
              isSubmitting={isSubmitting}
              onSubmit={handleSubmit}
              onScreenBlur={handleScreenBlur}
              onBack={handleBack}
            />
          </React.Suspense>
        </ErrorBoundary>
      )}
    </View>
  );
};
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});