|
1 | 1 | import { FormEvent, useCallback, useEffect, useRef, useState } from 'react'; |
2 | 2 | import { useMutation } from '@tanstack/react-query'; |
3 | | -import { Button, Column, Form, Grid, InlineNotification, Loading, Modal, Stack } from '@carbon/react'; |
| 3 | +import { Button, Column, Form, Grid, InlineNotification, Loading, Stack } from '@carbon/react'; |
4 | 4 | import { useBlocker, useNavigate } from 'react-router-dom'; |
5 | 5 | import { ArrowRight, TrashCan } from '@carbon/icons-react'; |
6 | 6 | import { scrollToSection } from '@/utils/InputUtils'; |
@@ -28,7 +28,7 @@ interface CreateOpeningFormProps { |
28 | 28 |
|
29 | 29 | export const CreateOpeningForm = ({ type, currentStep, setCurrentStep }: CreateOpeningFormProps) => { |
30 | 30 | // All hooks called unconditionally at component top |
31 | | - const [isCancelModalOpen, setIsCancelModalOpen] = useState<boolean>(false); |
| 31 | + const [isLeavePageModalOpen, setIsLeavePageModalOpen] = useState<boolean>(false); |
32 | 32 | const navigate = useNavigate(); |
33 | 33 | const [form, setForm] = useState<CreateOpeningFormType>(() => { |
34 | 34 | return structuredClone(DefaultOpeningForm); |
@@ -215,16 +215,30 @@ export const CreateOpeningForm = ({ type, currentStep, setCurrentStep }: CreateO |
215 | 215 | } |
216 | 216 |
|
217 | 217 | const handleCancel = () => { |
218 | | - setIsCancelModalOpen(true); |
| 218 | + if (!isNavigationBlocked) { |
| 219 | + bypassBlockerRef.current = true; |
| 220 | + navigate(OpeningsRoute.path!); |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + setIsLeavePageModalOpen(true); |
219 | 225 | }; |
220 | 226 |
|
221 | 227 | const handleLeaveConfirm = () => { |
222 | 228 | bypassBlockerRef.current = true; |
223 | | - blocker.proceed(); |
| 229 | + setIsLeavePageModalOpen(false); |
| 230 | + |
| 231 | + if (blocker.state === 'blocked') { |
| 232 | + blocker?.proceed?.(); |
| 233 | + return; |
| 234 | + } |
| 235 | + |
| 236 | + navigate(OpeningsRoute.path!); |
224 | 237 | }; |
225 | 238 |
|
226 | 239 | const handleStay = () => { |
227 | | - blocker.reset(); |
| 240 | + setIsLeavePageModalOpen(false); |
| 241 | + blocker?.reset?.(); |
228 | 242 | }; |
229 | 243 |
|
230 | 244 | const handleCreate = () => { |
@@ -325,38 +339,8 @@ export const CreateOpeningForm = ({ type, currentStep, setCurrentStep }: CreateO |
325 | 339 | </Grid> |
326 | 340 | </Column> |
327 | 341 |
|
328 | | - <Modal |
329 | | - passiveModal |
330 | | - danger |
331 | | - open={isCancelModalOpen} |
332 | | - modalHeading={<ModalHead title="Are you sure you want to cancel?" helperTop="Create new opening" />} |
333 | | - onRequestClose={() => setIsCancelModalOpen(false)} |
334 | | - className="default-modal" |
335 | | - preventCloseOnClickOutside |
336 | | - size="sm" |
337 | | - > |
338 | | - <Grid> |
339 | | - <Column sm={4} md={8} lg={16}> |
340 | | - <p className='cancel-content'> |
341 | | - If you leave this page, all the information you've entered will be lost. |
342 | | - </p> |
343 | | - </Column> |
344 | | - <Column sm={4} md={8} lg={16}> |
345 | | - <Stack orientation="horizontal" gap={2} className="default-equal-split-stack"> |
346 | | - <Button className="modal-button" kind="secondary" onClick={() => setIsCancelModalOpen(false)}> |
347 | | - Continue reviewing |
348 | | - </Button> |
349 | | - |
350 | | - <Button className="modal-button" kind="danger" renderIcon={TrashCan} onClick={() => { bypassBlockerRef.current = true; navigate(OpeningsRoute.path!); }}> |
351 | | - Leave without saving |
352 | | - </Button> |
353 | | - </Stack> |
354 | | - </Column> |
355 | | - </Grid> |
356 | | - </Modal> |
357 | | - |
358 | 342 | <LeavePageModal |
359 | | - open={blocker.state === 'blocked'} |
| 343 | + open={isLeavePageModalOpen || blocker.state === 'blocked'} |
360 | 344 | onRequestClose={handleStay} |
361 | 345 | onLeave={handleLeaveConfirm} |
362 | 346 | onStay={handleStay} |
|
0 commit comments