|
| 1 | +import React, { useRef, useState, useEffect, useCallback } from 'react'; |
| 2 | +import { useNavigation, useFocusEffect } from '@react-navigation/native'; |
| 3 | +import { useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 4 | +import { FlatList, Pressable, Modal } from 'react-native'; |
| 5 | +import { SimpleGrid } from 'react-native-super-grid'; |
| 6 | +import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome'; |
| 7 | +import { faTimes, faQrcode } from '@fortawesome/free-solid-svg-icons'; |
| 8 | +import { Text, YStack, XStack, useTheme, Button } from 'tamagui'; |
| 9 | +import { format } from 'date-fns'; |
| 10 | +import FastImage from 'react-native-fast-image'; |
| 11 | +import useStorage from '../hooks/use-storage'; |
| 12 | +import useFleetbase from '../hooks/use-fleetbase'; |
| 13 | + |
| 14 | +const PROOF_COLUMN_WIDTH = 160; |
| 15 | + |
| 16 | +const OrderProofOfDelivery = ({ order, subject }) => { |
| 17 | + const { adapter } = useFleetbase(); |
| 18 | + const theme = useTheme(); |
| 19 | + const insets = useSafeAreaInsets(); |
| 20 | + const id = order.id ?? null; |
| 21 | + const [proofs, setProofs] = useStorage(`${id}_proofs`, []); |
| 22 | + const [isLoading, setIsLoading] = useState(false); |
| 23 | + const [fullscreenImage, setFullscreenImage] = useState(null); |
| 24 | + |
| 25 | + const loadOrderProof = useCallback(async () => { |
| 26 | + if (!adapter) return; |
| 27 | + setIsLoading(true); |
| 28 | + |
| 29 | + const subjectSuffix = subject ? `/${subject.id}` : ''; |
| 30 | + |
| 31 | + try { |
| 32 | + const proofs = await adapter.get(`orders/${order.id}/proofs${subjectSuffix}`); |
| 33 | + setProofs(proofs); |
| 34 | + return proofs; |
| 35 | + } catch (err) { |
| 36 | + console.warn('Error loading order proofs:', err); |
| 37 | + } finally { |
| 38 | + setIsLoading(false); |
| 39 | + } |
| 40 | + }, [adapter]); |
| 41 | + |
| 42 | + const openImage = (proof) => { |
| 43 | + let url = proof.url; |
| 44 | + if (proof.remarks.endsWith('Signature')) { |
| 45 | + url = proof.raw; |
| 46 | + } |
| 47 | + setFullscreenImage(url); |
| 48 | + }; |
| 49 | + |
| 50 | + const closeImage = () => { |
| 51 | + setFullscreenImage(null); |
| 52 | + }; |
| 53 | + |
| 54 | + const renderProof = ({ item: proof }) => { |
| 55 | + return ( |
| 56 | + <YStack px='$3' py='$2' width={PROOF_COLUMN_WIDTH}> |
| 57 | + <YStack mb='$2'> |
| 58 | + <Pressable onPress={() => openImage(proof)}> |
| 59 | + {proof.remarks.endsWith('Photo') && ( |
| 60 | + <FastImage |
| 61 | + source={{ uri: proof.url }} |
| 62 | + style={{ |
| 63 | + width: PROOF_COLUMN_WIDTH - 25, |
| 64 | + height: PROOF_COLUMN_WIDTH - 25, |
| 65 | + borderRadius: 8, |
| 66 | + }} |
| 67 | + /> |
| 68 | + )} |
| 69 | + {proof.remarks.endsWith('Signature') && ( |
| 70 | + <FastImage |
| 71 | + source={{ uri: proof.raw }} |
| 72 | + style={{ |
| 73 | + width: PROOF_COLUMN_WIDTH - 25, |
| 74 | + height: PROOF_COLUMN_WIDTH - 25, |
| 75 | + borderRadius: 8, |
| 76 | + }} |
| 77 | + /> |
| 78 | + )} |
| 79 | + {proof.remarks.endsWith('Scan') && ( |
| 80 | + <YStack alignItems='center' justifyContent='center'> |
| 81 | + <FontAwesomeIcon icon={faQrcode} color={theme['$blue-400'].val} size={40} /> |
| 82 | + </YStack> |
| 83 | + )} |
| 84 | + </Pressable> |
| 85 | + </YStack> |
| 86 | + <YStack textAlign='center' alignItems='center' justifyContent='center' width='100%' flex={1}> |
| 87 | + <Text color='$textPrimary' fontSize='$2' textAlign='center' fontWeight='bold' mb='$1'> |
| 88 | + {proof.remarks} |
| 89 | + </Text> |
| 90 | + <Text color='$textSecondary' fontSize='$2' textAlign='center'> |
| 91 | + {proof.id} |
| 92 | + </Text> |
| 93 | + <Text color='$textSecondary' fontSize='$2' textAlign='center'> |
| 94 | + {format(proof.created_at, 'MMM d, Y HH:mm')} |
| 95 | + </Text> |
| 96 | + </YStack> |
| 97 | + </YStack> |
| 98 | + ); |
| 99 | + }; |
| 100 | + |
| 101 | + useFocusEffect( |
| 102 | + useCallback(() => { |
| 103 | + loadOrderProof(); |
| 104 | + }, [id]) |
| 105 | + ); |
| 106 | + |
| 107 | + if (proofs.length === 0) { |
| 108 | + return ( |
| 109 | + <YStack py='$5' alignItems='center' justifyContent='center'> |
| 110 | + <Text color='$textSecondary'>No Proof of Delivery Captured.</Text> |
| 111 | + </YStack> |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + return ( |
| 116 | + <YStack> |
| 117 | + <SimpleGrid |
| 118 | + data={proofs} |
| 119 | + maxItemsPerRow={2} |
| 120 | + itemDimension={PROOF_COLUMN_WIDTH} |
| 121 | + scrollEnabled={false} |
| 122 | + showsVerticalScrollIndicator={false} |
| 123 | + showsHorizontalScrollIndicator={false} |
| 124 | + keyExtractor={(item, index) => item.id || index} |
| 125 | + renderItem={renderProof} |
| 126 | + /> |
| 127 | + |
| 128 | + {fullscreenImage && ( |
| 129 | + <Modal visible={true} animationType='fade' transparent={true}> |
| 130 | + <YStack position='absolute' fullscreen bg='rgba(0,0,0,0.9)' justifyContent='center' alignItems='center' zIndex={9999}> |
| 131 | + <Button position='absolute' top={20} right={20} size='$3' circular bg='$colorTransparent' onPress={closeImage} zIndex={10000} mt={insets.top}> |
| 132 | + <FontAwesomeIcon icon={faTimes} color={theme['$textPrimary'].val} /> |
| 133 | + </Button> |
| 134 | + |
| 135 | + <FastImage |
| 136 | + source={{ uri: fullscreenImage }} |
| 137 | + style={{ |
| 138 | + width: '100%', |
| 139 | + height: '100%', |
| 140 | + resizeMode: 'contain', |
| 141 | + }} |
| 142 | + /> |
| 143 | + </YStack> |
| 144 | + </Modal> |
| 145 | + )} |
| 146 | + </YStack> |
| 147 | + ); |
| 148 | +}; |
| 149 | + |
| 150 | +export default OrderProofOfDelivery; |
0 commit comments