Skip to content

Commit 1de20e9

Browse files
authored
AYS-300 | Add confirmation modal (#330)
1 parent 9fc6472 commit 1de20e9

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

src/app/(private)/emergency-evacuation-applications/[id]/page.tsx

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import { Button } from '@/components/ui/button'
44
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
55
import { Checkbox } from '@/components/ui/checkbox'
6+
import {
7+
Dialog,
8+
DialogContent,
9+
DialogFooter,
10+
DialogHeader,
11+
DialogTitle,
12+
} from '@/components/ui/dialog'
613
import {
714
Form,
815
FormControl,
@@ -68,6 +75,9 @@ const Page = ({
6875
const [error, setError] = useState<string | null>(null)
6976
const [isEmergencyApplicationEditable, setIsEmergencyApplicationEditable] =
7077
useState<boolean>(false)
78+
const [showUpdateConfirmDialog, setShowUpdateConfirmDialog] = useState(false)
79+
const [pendingSaveValues, setPendingSaveValues] =
80+
useState<EvacuationApplicationEditableFields | null>(null)
7181

7282
const canUpdateApplication =
7383
emergencyEvacuationApplicationDetails?.status !== 'COMPLETED' &&
@@ -143,19 +153,40 @@ const Page = ({
143153
showErrorToast(undefined, 'common.error.noChange')
144154
return
145155
}
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)
147176
.then((response) => {
148177
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+
}))
157186
showSuccessToast('application.updateSuccess')
158187
setIsEmergencyApplicationEditable(false)
188+
setShowUpdateConfirmDialog(false)
189+
setPendingSaveValues(null)
159190
} else {
160191
showErrorToast(undefined, 'application.updateError')
161192
}
@@ -592,6 +623,27 @@ const Page = ({
592623
</form>
593624
</Form>
594625
)}
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>
595647
</div>
596648
)
597649
}

src/i18n/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"send": "Send",
1010
"reject": "Reject",
1111
"approve": "Approve",
12+
"confirmDescription": "These changes are irreversible. Do you want to proceed?",
1213
"institution": "Institution",
1314
"firstName": "First Name",
1415
"lastName": "Last Name",

src/i18n/locales/tr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"send": "Gönder",
1010
"reject": "Reddet",
1111
"approve": "Onayla",
12+
"confirmDescription": "Yapılan değişiklikler geri alınamayacaktır. Onaylıyor musunuz?",
1213
"institution": "Kurum",
1314
"firstName": "Ad",
1415
"lastName": "Soyad",

src/modules/emergencyEvacuationApplications/constants/formValidationSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const EmergencyEvacuationApplicationSchema = z.object({
2929
hasObstaclePersonExist: z.boolean().default(false).optional(),
3030
notes: z
3131
.string()
32-
.max(1000, { message: 'maxLength' })
33-
.refine((value) => !/^\s/.test(value), {
32+
.max(1000, { message: 'validation.maxLength' })
33+
.refine((value) => !/\s$/.test(value), {
3434
message: 'validation.whitespace',
3535
})
3636
.optional(),

0 commit comments

Comments
 (0)