|
1
|
import { Prompt } from "@medusajs/ui" |
|
2
|
import { PropsWithChildren } from "react" |
|
3
|
import { FieldValues, UseFormReturn } from "react-hook-form" |
|
4
|
import { useTranslation } from "react-i18next" |
|
5
|
import { useBlocker } from "react-router-dom" |
|
6
|
import { Form } from "../../common/form" |
|
7
|
|
|
8
|
type RouteModalFormProps<TFieldValues extends FieldValues> = PropsWithChildren<{ |
|
9
|
form: UseFormReturn<TFieldValues> |
|
10
|
blockSearchParams?: boolean |
|
11
|
onClose?: (isSubmitSuccessful: boolean) => void |
|
12
|
}> |
|
13
|
|
|
14
|
export const RouteModalForm = <TFieldValues extends FieldValues = any>({ |
|
15
|
form, |
|
16
|
blockSearchParams: blockSearch = false, |
|
17
|
children, |
|
18
|
onClose, |
|
19
|
}: RouteModalFormProps<TFieldValues>) => { |
|
20
|
const { t } = useTranslation() |
|
21
|
|
|
22
|
const { |
|
23
|
formState: { isDirty }, |
|
24
|
} = form |
|
25
|
|
|
26
|
const blocker = useBlocker(({ currentLocation, nextLocation }) => { |
|
27
|
// Check both nextLocation and currentLocation state for successful submission |
|
28
|
// This handles browser history navigation (-1) where we set state on current location |
|
29
|
const { isSubmitSuccessful: nextIsSuccessful } = nextLocation.state || {} |
|
30
|
const { isSubmitSuccessful: currentIsSuccessful } = |
|
31
|
currentLocation.state || {} |
|
32
|
const isSubmitSuccessful = nextIsSuccessful || currentIsSuccessful |
|
33
|
|
|
34
|
if (isSubmitSuccessful) { |
|
35
|
onClose?.(true) |
|
36
|
return false |
|
37
|
} |
|
38
|
|
|
39
|
const isPathChanged = currentLocation.pathname !== nextLocation.pathname |
|
40
|
const isSearchChanged = currentLocation.search !== nextLocation.search |
|
41
|
|
|
42
|
if (blockSearch) { |
|
43
|
const shouldBlock = isDirty && (isPathChanged || isSearchChanged) |
|
44
|
|
|
45
|
if (isPathChanged) { |
|
46
|
onClose?.(isSubmitSuccessful) |
|
47
|
} |
|
48
|
|
|
49
|
return shouldBlock |
|
50
|
} |
|
51
|
|
|
52
|
const shouldBlock = isDirty && isPathChanged |
|
53
|
|
|
54
|
if (isPathChanged) { |
|
55
|
onClose?.(isSubmitSuccessful) |
|
56
|
} |
|
57
|
|
|
58
|
return shouldBlock |
|
59
|
}) |
|
60
|
|
|
61
|
const handleCancel = () => { |
|
62
|
blocker?.reset?.() |
|
63
|
} |
|
64
|
|
|
65
|
const handleContinue = () => { |
|
66
|
blocker?.proceed?.() |
|
67
|
onClose?.(false) |
|
68
|
} |
|
69
|
|
|
70
|
return ( |
|
71
|
<Form {...form}> |
|
72
|
{children} |
|
73
|
<Prompt open={blocker.state === "blocked"} variant="confirmation"> |
|
74
|
<Prompt.Content> |
|
75
|
<Prompt.Header> |
|
76
|
<Prompt.Title>{t("general.unsavedChangesTitle")}</Prompt.Title> |
|
77
|
<Prompt.Description> |
|
78
|
{t("general.unsavedChangesDescription")} |
|
79
|
</Prompt.Description> |
|
80
|
</Prompt.Header> |
|
81
|
<Prompt.Footer> |
|
82
|
<Prompt.Cancel onClick={handleCancel} type="button"> |
|
83
|
{t("actions.cancel")} |
|
84
|
</Prompt.Cancel> |
|
85
|
<Prompt.Action onClick={handleContinue} type="button"> |
|
86
|
{t("actions.continue")} |
|
87
|
</Prompt.Action> |
|
88
|
</Prompt.Footer> |
|
89
|
</Prompt.Content> |
|
90
|
</Prompt> |
|
91
|
</Form> |
|
92
|
) |
|
93
|
} |