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

100% Statements 15/15
84% Branches 21/25
100% Functions 3/3
100% Lines 14/14

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                                              2x 9x 9x           9x                                       9x             9x   9x 9x 1x     8x     9x 1x     8x                                                                                                                                               9x                            
import { useMemo } from 'react';
import { ActivityIndicator, Text, TouchableOpacity, View } from 'react-native';
 
import { Button } from '@repo/ui/components/Button';
import { HeaderRequest } from '@repo/ui/components/HeaderRequest';
import { ScreenLayout } from '@repo/ui/components/ScreenLayout';
import { CloseCircleIcon } from '@repo/ui/icons/CloseCircle';
import { TickCircleIcon } from '@repo/ui/icons/TickCircle';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import { SCREENS } from '@repo/constants/screens';
 
import { RequestCardDetail } from '@/components/RequestCardDetail';
import { RequestCardDetailSkeleton } from '@/components/RequestCardDetailSkeleton';
import { SubmitRejectModal } from '@/components/SubmitRejectModal';
 
import type { AppStackScreenProps } from '@/types/navigation';
 
import { useRequestDetail } from './useRequestDetail';
 
type RequestDetailScreenProps = AppStackScreenProps<typeof SCREENS.REQUEST_DETAIL>;
 
export const RequestDetailScreen = ({ navigation, route }: RequestDetailScreenProps) => {
  const { theme } = useTheme();
  const styles = useStyles();
  const {
    id = '',
    fromDeepLink = false,
    refreshTimeStamp = 0,
    category: categoryParam,
  } = route.params || {};
 
  const {
    request,
    isLoading,
    isRefetching,
    isSubmitting,
    isModalVisible,
    note,
    noteInputRef,
    isNoteValid,
    setNote,
    visibility,
    requestType,
    handlePressBack,
    handleApprove,
    handleReject,
    handleCancel,
    handleToggleModal,
    submitReject,
  } = useRequestDetail({
    id,
    fromDeepLink,
    refreshTimeStamp,
    navigation,
  });
 
  const { isShowCancelBtn, isShowAction, isShowApproveBtn, isShowRejectBtn } = visibility;
 
  const renderCardDetail = useMemo(() => {
    if (!request) {
      return <Text>Request not found</Text>;
    }
 
    return <RequestCardDetail {...request} />;
  }, [request]);
 
  if (isLoading || isRefetching) {
    return <RequestCardDetailSkeleton isFullScreen category={categoryParam || request?.category} />;
  }
 
  return (
    <ScreenLayout>
      {isSubmitting && (
        <View className="absolute inset-0 bg-black/35 justify-center items-center z-[9999]">
          <ActivityIndicator />
        </View>
      )}
      <HeaderRequest title="Request Detail" onBack={handlePressBack} />
 
      <View style={[styles.container, { gap: theme.metrics.spacing[10] }]}>
        {renderCardDetail}
 
        {isShowAction && (
          <View className="flex-row justify-center items-center w-full gap-20">
            {isShowRejectBtn && (
              <TouchableOpacity
                onPress={handleReject}
                accessibilityRole="button"
                accessibilityLabel="Cancel"
                accessibilityHint="Cancels the current item"
              >
                <CloseCircleIcon />
              </TouchableOpacity>
            )}
 
            {isShowApproveBtn && (
              <TouchableOpacity
                onPress={handleApprove}
                accessibilityRole="button"
                accessibilityLabel="Tick"
                accessibilityHint="Ticks the current item"
              >
                <TickCircleIcon color="green" />
              </TouchableOpacity>
            )}
          </View>
        )}
 
        {isShowCancelBtn && (
          <View style={styles.cancelSection}>
            {requestType.isTimeOff && (
              <View style={styles.noteSection}>
                <Text>
                  Meal preparation may be affected if you cancel after 7:00 AM on the due date
                </Text>
              </View>
            )}
            <Button
              accessibilityRole="button"
              accessibilityLabel={`Cancel ${request?.category?.toLowerCase()} request`}
              accessibilityHint="Double tap to cancel this request"
              onPress={handleCancel}
            >
              Cancel
            </Button>
          </View>
        )}
      </View>
 
      <SubmitRejectModal
        isOpen={isModalVisible}
        note={note}
        noteInputRef={noteInputRef}
        isNoteValid={isNoteValid}
        onChangeNote={setNote}
        onReject={handleToggleModal}
        onSubmit={submitReject}
      />
    </ScreenLayout>
  );
};
 
const useStyles = makeStyles(theme => ({
  container: {
    paddingHorizontal: theme.metrics.spacing[4],
    flex: 1,
  },
  noteSection: {
    padding: theme.metrics.spacing[4],
    borderRadius: theme.metrics.borderRadius[4],
    backgroundColor: theme.colors.background.tertiary,
  },
  cancelSection: {
    gap: 10,
  },
}));