|
1
|
import { zodResolver } from "@hookform/resolvers/zod" |
|
2
|
import { |
|
3
|
Button, |
|
4
|
clx, |
|
5
|
DatePicker, |
|
6
|
Heading, |
|
7
|
RadioGroup, |
|
8
|
toast, |
|
9
|
} from "@medusajs/ui" |
|
10
|
import { useState } from "react" |
|
11
|
import { useForm } from "react-hook-form" |
|
12
|
import { useParams } from "react-router-dom" |
|
13
|
import { z } from "@medusajs/framework/zod" |
|
14
|
import { AdminGiftCard } from "../../../../../types" |
|
15
|
import { Form } from "../../../../components/form" |
|
16
|
import { KeyboundForm } from "../../../../components/keybound-form" |
|
17
|
import { RouteDrawer, useRouteModal } from "../../../../components/modals" |
|
18
|
import { |
|
19
|
useGiftCard, |
|
20
|
useUpdateGiftCard, |
|
21
|
} from "../../../../hooks/api/gift-cards" |
|
22
|
|
|
23
|
const GiftCardExpiration = () => { |
|
24
|
const { id } = useParams() |
|
25
|
const { gift_card: giftCard, isPending, isError, error } = useGiftCard(id!) |
|
26
|
|
|
27
|
if (isError) { |
|
28
|
throw error |
|
29
|
} |
|
30
|
|
|
31
|
const isReady = !isPending && !!giftCard |
|
32
|
|
|
33
|
return ( |
|
34
|
<RouteDrawer> |
|
35
|
<RouteDrawer.Header> |
|
36
|
<RouteDrawer.Title asChild> |
|
37
|
<Heading>Edit expiration date</Heading> |
|
38
|
</RouteDrawer.Title> |
|
39
|
|
|
40
|
<RouteDrawer.Description asChild> |
|
41
|
<span className="sr-only"> |
|
42
|
Edit the expiration date for the gift card |
|
43
|
</span> |
|
44
|
</RouteDrawer.Description> |
|
45
|
</RouteDrawer.Header> |
|
46
|
|
|
47
|
{isReady && <GiftCardExpirationForm giftCard={giftCard} />} |
|
48
|
</RouteDrawer> |
|
49
|
) |
|
50
|
} |
|
51
|
|
|
52
|
interface GiftCardExpirationFormProps { |
|
53
|
giftCard: AdminGiftCard |
|
54
|
} |
|
55
|
|
|
56
|
const GiftCardExpirationForm = ({ giftCard }: GiftCardExpirationFormProps) => { |
|
57
|
const form = useForm<z.infer<typeof schema>>({ |
|
58
|
defaultValues: { |
|
59
|
expires_at: giftCard.expires_at ? new Date(giftCard.expires_at) : null, |
|
60
|
}, |
|
61
|
resolver: zodResolver(schema), |
|
62
|
}) |
|
63
|
|
|
64
|
const { mutateAsync, isPending } = useUpdateGiftCard(giftCard.id) |
|
65
|
const { handleSuccess } = useRouteModal() |
|
66
|
const [isSettingExpiration, setIsSettingExpiration] = useState( |
|
67
|
!!giftCard.expires_at |
|
68
|
) |
|
69
|
|
|
70
|
const onSubmit = form.handleSubmit(async (data) => { |
|
71
|
if (isSettingExpiration && !data.expires_at) { |
|
72
|
form.setError("expires_at", { |
|
73
|
message: "Please select an expiration date and time", |
|
74
|
}) |
|
75
|
return |
|
76
|
} |
|
77
|
|
|
78
|
await mutateAsync( |
|
79
|
{ |
|
80
|
expires_at: isSettingExpiration |
|
81
|
? data.expires_at!.toISOString() |
|
82
|
: null, |
|
83
|
}, |
|
84
|
{ |
|
85
|
onSuccess: () => { |
|
86
|
toast.success("Expiration date updated successfully") |
|
87
|
handleSuccess() |
|
88
|
}, |
|
89
|
onError: (error) => { |
|
90
|
toast.error(error.message) |
|
91
|
}, |
|
92
|
} |
|
93
|
) |
|
94
|
}) |
|
95
|
|
|
96
|
return ( |
|
97
|
<RouteDrawer.Form form={form}> |
|
98
|
<KeyboundForm |
|
99
|
className="flex flex-1 flex-col overflow-hidden" |
|
100
|
onSubmit={onSubmit} |
|
101
|
> |
|
102
|
<RouteDrawer.Body className="flex flex-col gap-y-6 overflow-y-auto"> |
|
103
|
<RadioGroup |
|
104
|
className="flex flex-col gap-y-3" |
|
105
|
value={isSettingExpiration.toString()} |
|
106
|
onValueChange={(value) => setIsSettingExpiration(value === "true")} |
|
107
|
> |
|
108
|
<RadioGroup.ChoiceBox |
|
109
|
value={"false"} |
|
110
|
description={""} |
|
111
|
label={"No expiration date"} |
|
112
|
className={clx("basis-1/2")} |
|
113
|
/> |
|
114
|
|
|
115
|
<RadioGroup.ChoiceBox |
|
116
|
value={"true"} |
|
117
|
label={"Set expiration date"} |
|
118
|
description={""} |
|
119
|
className={clx("basis-1/2")} |
|
120
|
/> |
|
121
|
</RadioGroup> |
|
122
|
|
|
123
|
{isSettingExpiration && ( |
|
124
|
<Form.Field |
|
125
|
control={form.control} |
|
126
|
name="expires_at" |
|
127
|
render={({ field }) => { |
|
128
|
return ( |
|
129
|
<Form.Item> |
|
130
|
<Form.Label>Expiration date</Form.Label> |
|
131
|
|
|
132
|
<Form.Control> |
|
133
|
<DatePicker |
|
134
|
granularity="minute" |
|
135
|
hourCycle={12} |
|
136
|
shouldCloseOnSelect={false} |
|
137
|
{...field} |
|
138
|
/> |
|
139
|
</Form.Control> |
|
140
|
|
|
141
|
<Form.ErrorMessage /> |
|
142
|
</Form.Item> |
|
143
|
) |
|
144
|
}} |
|
145
|
/> |
|
146
|
)} |
|
147
|
</RouteDrawer.Body> |
|
148
|
|
|
149
|
<RouteDrawer.Footer> |
|
150
|
<div className="flex justify-end gap-2"> |
|
151
|
<RouteDrawer.Close asChild> |
|
152
|
<Button size="small" variant="secondary"> |
|
153
|
Cancel |
|
154
|
</Button> |
|
155
|
</RouteDrawer.Close> |
|
156
|
<Button size="small" type="submit" isLoading={isPending}> |
|
157
|
Save |
|
158
|
</Button> |
|
159
|
</div> |
|
160
|
</RouteDrawer.Footer> |
|
161
|
</KeyboundForm> |
|
162
|
</RouteDrawer.Form> |
|
163
|
) |
|
164
|
} |
|
165
|
|
|
166
|
const schema = z.object({ |
|
167
|
expires_at: z.date().nullish(), |
|
168
|
}) |
|
169
|
|
|
170
|
export default GiftCardExpiration |