|
3 | 3 | import { Button } from '@/components/ui/button'
|
4 | 4 | import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
5 | 5 | import { Checkbox } from '@/components/ui/checkbox'
|
| 6 | +import { |
| 7 | + Dialog, |
| 8 | + DialogContent, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | +} from '@/components/ui/dialog' |
6 | 13 | import {
|
7 | 14 | Form,
|
8 | 15 | FormControl,
|
@@ -68,6 +75,9 @@ const Page = ({
|
68 | 75 | const [error, setError] = useState<string | null>(null)
|
69 | 76 | const [isEmergencyApplicationEditable, setIsEmergencyApplicationEditable] =
|
70 | 77 | useState<boolean>(false)
|
| 78 | + const [showUpdateConfirmDialog, setShowUpdateConfirmDialog] = useState(false) |
| 79 | + const [pendingSaveValues, setPendingSaveValues] = |
| 80 | + useState<EvacuationApplicationEditableFields | null>(null) |
71 | 81 |
|
72 | 82 | const canUpdateApplication =
|
73 | 83 | emergencyEvacuationApplicationDetails?.status !== 'COMPLETED' &&
|
@@ -143,19 +153,40 @@ const Page = ({
|
143 | 153 | showErrorToast(undefined, 'common.error.noChange')
|
144 | 154 | return
|
145 | 155 | }
|
146 |
| - updateEmergencyEvacuationApplication(params.id, currentValues) |
| 156 | + |
| 157 | + setPendingSaveValues(currentValues) |
| 158 | + |
| 159 | + if ( |
| 160 | + currentValues.status === 'COMPLETED' || |
| 161 | + currentValues.status === 'CANCELLED' |
| 162 | + ) { |
| 163 | + setShowUpdateConfirmDialog(true) |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + handleConfirmSave(currentValues) |
| 168 | + } |
| 169 | + |
| 170 | + const handleConfirmSave = ( |
| 171 | + values: EvacuationApplicationEditableFields = pendingSaveValues! |
| 172 | + ) => { |
| 173 | + if (!values) return |
| 174 | + |
| 175 | + updateEmergencyEvacuationApplication(params.id, values) |
147 | 176 | .then((response) => {
|
148 | 177 | if (response.isSuccess) {
|
149 |
| - setEmergencyEvacuationApplicationDetails({ |
150 |
| - ...emergencyEvacuationApplicationDetails!, |
151 |
| - ...currentValues, |
152 |
| - }) |
153 |
| - setInitialApplicationValues({ |
154 |
| - ...emergencyEvacuationApplicationDetails!, |
155 |
| - ...currentValues, |
156 |
| - }) |
| 178 | + setEmergencyEvacuationApplicationDetails((prev) => ({ |
| 179 | + ...prev!, |
| 180 | + ...values, |
| 181 | + })) |
| 182 | + setInitialApplicationValues((prev) => ({ |
| 183 | + ...prev!, |
| 184 | + ...values, |
| 185 | + })) |
157 | 186 | showSuccessToast('application.updateSuccess')
|
158 | 187 | setIsEmergencyApplicationEditable(false)
|
| 188 | + setShowUpdateConfirmDialog(false) |
| 189 | + setPendingSaveValues(null) |
159 | 190 | } else {
|
160 | 191 | showErrorToast(undefined, 'application.updateError')
|
161 | 192 | }
|
@@ -592,6 +623,27 @@ const Page = ({
|
592 | 623 | </form>
|
593 | 624 | </Form>
|
594 | 625 | )}
|
| 626 | + <Dialog |
| 627 | + open={showUpdateConfirmDialog} |
| 628 | + onOpenChange={setShowUpdateConfirmDialog} |
| 629 | + > |
| 630 | + <DialogContent> |
| 631 | + <DialogHeader> |
| 632 | + <DialogTitle>{t('common.confirmDescription')}</DialogTitle> |
| 633 | + </DialogHeader> |
| 634 | + <DialogFooter> |
| 635 | + <Button |
| 636 | + variant="outline" |
| 637 | + onClick={() => setShowUpdateConfirmDialog(false)} |
| 638 | + > |
| 639 | + {t('common.no')} |
| 640 | + </Button> |
| 641 | + <Button onClick={() => handleConfirmSave()}> |
| 642 | + {t('common.yes')} |
| 643 | + </Button> |
| 644 | + </DialogFooter> |
| 645 | + </DialogContent> |
| 646 | + </Dialog> |
595 | 647 | </div>
|
596 | 648 | )
|
597 | 649 | }
|
|
0 commit comments