|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { Button, TextField, Dialog, DialogTitle, DialogContent, DialogActions, CircularProgress, Box } from '@mui/material'; |
| 3 | + |
| 4 | +interface Props { |
| 5 | + status: string |
| 6 | + surchargeId: string; |
| 7 | + imageName: string | undefined; |
| 8 | + isOpen: boolean; |
| 9 | + onClose: () => void; |
| 10 | + onConfirm: (surchargeId: string, action: string, newSurchargeAmount: number | undefined, newTotalAmount: number | undefined) => Promise<void>; |
| 11 | +} |
| 12 | + |
| 13 | +const ConfirmationModal: React.FC<Props> = ({status, surchargeId, imageName, isOpen, onClose, onConfirm }) => { |
| 14 | + const [newSurchargeAmount, setNewSurchargeAmount] = useState(''); |
| 15 | + const [newTotalAmount, setNewTotalAmount] = useState(''); |
| 16 | + const [imageBase64, setImageBase64] = useState<string | null>(null); |
| 17 | + const [loadingImage, setLoadingImage] = useState(false); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (isOpen && imageName) { |
| 21 | + const fetchImage = async () => { |
| 22 | + setLoadingImage(true); |
| 23 | + try { |
| 24 | + const baseURL = import.meta.env.VITE_BASE_URL; |
| 25 | + const response = await fetch(`${baseURL}/admin/image?image=${imageName}`, { // TODO: api -> admin |
| 26 | + method: 'GET', |
| 27 | + headers: { |
| 28 | + Accept: 'application/json', |
| 29 | + 'Content-Type': 'application/json', |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + if (response.ok) { |
| 34 | + const data = await response.json(); |
| 35 | + setImageBase64(data.image); // Assuming `data.image` contains the Base64 string |
| 36 | + } else { |
| 37 | + console.error('Error fetching image:', response.statusText); |
| 38 | + setImageBase64(null); |
| 39 | + } |
| 40 | + } catch (error) { |
| 41 | + console.error('Error fetching image:', error); |
| 42 | + setImageBase64(null); |
| 43 | + } finally { |
| 44 | + setLoadingImage(false); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + fetchImage(); |
| 49 | + } else { |
| 50 | + setImageBase64(null); |
| 51 | + } |
| 52 | + }, [isOpen, imageName]); |
| 53 | + |
| 54 | + const handleInputChange = ( |
| 55 | + e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, |
| 56 | + setFunction: (value: React.SetStateAction<string>) => void |
| 57 | + ) => { |
| 58 | + setFunction(e.target.value); |
| 59 | + }; |
| 60 | + |
| 61 | + function renderContent() { |
| 62 | + if (status === "REPORTED") { |
| 63 | + return ( |
| 64 | + <> |
| 65 | + <Button |
| 66 | + onClick={() => |
| 67 | + onConfirm(surchargeId, "CONFIRM", Number(newSurchargeAmount), Number(newTotalAmount)) |
| 68 | + } |
| 69 | + color="primary" |
| 70 | + variant="contained" |
| 71 | + > |
| 72 | + Confirm Surcharge |
| 73 | + </Button> |
| 74 | + <Button |
| 75 | + onClick={() => |
| 76 | + onConfirm(surchargeId, "REJECT", Number(newSurchargeAmount), Number(newTotalAmount)) |
| 77 | + } |
| 78 | + color="secondary" |
| 79 | + variant="contained" |
| 80 | + > |
| 81 | + Reject Surcharge |
| 82 | + </Button> |
| 83 | + </> |
| 84 | + ); |
| 85 | + } else if (status === "CONFIRMED") { |
| 86 | + return ( |
| 87 | + <Button |
| 88 | + onClick={() => |
| 89 | + onConfirm(surchargeId, "REJECT", Number(newSurchargeAmount), Number(newTotalAmount)) |
| 90 | + } |
| 91 | + color="secondary" |
| 92 | + variant="contained" |
| 93 | + > |
| 94 | + Reject Surcharge |
| 95 | + </Button> |
| 96 | + ); |
| 97 | + } else if (status === "REJECTED") { |
| 98 | + return ( |
| 99 | + <Button |
| 100 | + onClick={() => |
| 101 | + onConfirm(surchargeId, "CONFIRM", Number(newSurchargeAmount), Number(newTotalAmount)) |
| 102 | + } |
| 103 | + color="primary" |
| 104 | + variant="contained" |
| 105 | + > |
| 106 | + Confirm Surcharge |
| 107 | + </Button> |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + return ( |
| 114 | + <Dialog open={isOpen} onClose={onClose} maxWidth="sm" fullWidth> |
| 115 | + <DialogTitle>Confirm Surcharge</DialogTitle> |
| 116 | + <DialogContent dividers> |
| 117 | + <Box display="flex" justifyContent="center" mb={2}> |
| 118 | + {loadingImage ? ( |
| 119 | + <CircularProgress /> |
| 120 | + ) : imageBase64 ? ( |
| 121 | + <img |
| 122 | + src={`data:image/jpeg;base64,${imageBase64}`} |
| 123 | + alt="Surcharge Evidence" |
| 124 | + style={{ maxWidth: '100%', maxHeight: '300px', borderRadius: '8px' }} |
| 125 | + /> |
| 126 | + ) : ( |
| 127 | + <p>No image available</p> |
| 128 | + )} |
| 129 | + </Box> |
| 130 | + <TextField |
| 131 | + label="New Surcharge Amount" |
| 132 | + type="number" |
| 133 | + value={newSurchargeAmount} |
| 134 | + onChange={(e) => handleInputChange(e, setNewSurchargeAmount)} |
| 135 | + fullWidth |
| 136 | + variant="outlined" |
| 137 | + margin="normal" |
| 138 | + /> |
| 139 | + <TextField |
| 140 | + label="New Total Amount" |
| 141 | + type="number" |
| 142 | + value={newTotalAmount} |
| 143 | + onChange={(e) => handleInputChange(e, setNewTotalAmount)} |
| 144 | + fullWidth |
| 145 | + variant="outlined" |
| 146 | + margin="normal" |
| 147 | + /> |
| 148 | + </DialogContent> |
| 149 | + <DialogActions> |
| 150 | + <Button onClick={onClose} color="secondary" variant="outlined"> |
| 151 | + Close |
| 152 | + </Button> |
| 153 | + {renderContent()} |
| 154 | + </DialogActions> |
| 155 | + </Dialog> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export default ConfirmationModal; |
0 commit comments