|
1
|
import { zodResolver } from "@hookform/resolvers/zod" |
|
2
|
import { Button, Heading, Textarea, toast } from "@medusajs/ui" |
|
3
|
import { useForm } from "react-hook-form" |
|
4
|
import { useParams } from "react-router-dom" |
|
5
|
import { z } from "@medusajs/framework/zod" |
|
6
|
import { AdminGiftCard } from "../../../../../types" |
|
7
|
import { Form } from "../../../../components/form" |
|
8
|
import { KeyboundForm } from "../../../../components/keybound-form" |
|
9
|
import { RouteDrawer, useRouteModal } from "../../../../components/modals" |
|
10
|
import { |
|
11
|
useGiftCard, |
|
12
|
useUpdateGiftCard, |
|
13
|
} from "../../../../hooks/api/gift-cards" |
|
14
|
|
|
15
|
const Note = () => { |
|
16
|
const { id } = useParams() |
|
17
|
const { |
|
18
|
gift_card: giftCard, |
|
19
|
isPending, |
|
20
|
isError, |
|
21
|
error, |
|
22
|
} = useGiftCard(id!, {}) |
|
23
|
|
|
24
|
if (isError) { |
|
25
|
throw error |
|
26
|
} |
|
27
|
|
|
28
|
const isReady = !isPending && !!giftCard |
|
29
|
|
|
30
|
return ( |
|
31
|
<RouteDrawer> |
|
32
|
<RouteDrawer.Header> |
|
33
|
<RouteDrawer.Title asChild> |
|
34
|
<Heading>Edit note</Heading> |
|
35
|
</RouteDrawer.Title> |
|
36
|
|
|
37
|
<RouteDrawer.Description asChild> |
|
38
|
<span className="sr-only">Edit the note for the gift card</span> |
|
39
|
</RouteDrawer.Description> |
|
40
|
</RouteDrawer.Header> |
|
41
|
|
|
42
|
{isReady && <GiftCardNoteForm giftCard={giftCard} />} |
|
43
|
</RouteDrawer> |
|
44
|
) |
|
45
|
} |
|
46
|
|
|
47
|
interface GiftCardNoteFormProps { |
|
48
|
giftCard: AdminGiftCard |
|
49
|
} |
|
50
|
|
|
51
|
const GiftCardNoteForm = ({ giftCard }: GiftCardNoteFormProps) => { |
|
52
|
const form = useForm<z.infer<typeof schema>>({ |
|
53
|
defaultValues: { |
|
54
|
note: giftCard.note ?? "", |
|
55
|
}, |
|
56
|
resolver: zodResolver(schema), |
|
57
|
}) |
|
58
|
|
|
59
|
const { mutateAsync, isPending } = useUpdateGiftCard(giftCard.id) |
|
60
|
const { handleSuccess } = useRouteModal() |
|
61
|
|
|
62
|
const onSubmit = form.handleSubmit(async (data) => { |
|
63
|
await mutateAsync( |
|
64
|
{ note: data.note }, |
|
65
|
{ |
|
66
|
onSuccess: () => handleSuccess(), |
|
67
|
onError: (error) => toast.error(error.message), |
|
68
|
} |
|
69
|
) |
|
70
|
}) |
|
71
|
|
|
72
|
return ( |
|
73
|
<RouteDrawer.Form form={form}> |
|
74
|
<KeyboundForm |
|
75
|
className="flex flex-1 flex-col overflow-hidden" |
|
76
|
onSubmit={onSubmit} |
|
77
|
> |
|
78
|
<RouteDrawer.Body className="flex flex-col gap-y-6 overflow-y-auto"> |
|
79
|
<Form.Field |
|
80
|
control={form.control} |
|
81
|
name="note" |
|
82
|
render={({ field }) => ( |
|
83
|
<Form.Item> |
|
84
|
<Form.Label>Note</Form.Label> |
|
85
|
<Form.Control> |
|
86
|
<Textarea {...field} /> |
|
87
|
</Form.Control> |
|
88
|
<Form.ErrorMessage /> |
|
89
|
</Form.Item> |
|
90
|
)} |
|
91
|
/> |
|
92
|
</RouteDrawer.Body> |
|
93
|
|
|
94
|
<RouteDrawer.Footer> |
|
95
|
<div className="flex justify-end gap-2"> |
|
96
|
<RouteDrawer.Close asChild> |
|
97
|
<Button size="small" variant="secondary"> |
|
98
|
Cancel |
|
99
|
</Button> |
|
100
|
</RouteDrawer.Close> |
|
101
|
|
|
102
|
<Button size="small" type="submit" isLoading={isPending}> |
|
103
|
Save |
|
104
|
</Button> |
|
105
|
</div> |
|
106
|
</RouteDrawer.Footer> |
|
107
|
</KeyboundForm> |
|
108
|
</RouteDrawer.Form> |
|
109
|
) |
|
110
|
} |
|
111
|
|
|
112
|
const schema = z.object({ |
|
113
|
note: z.string().optional(), |
|
114
|
}) |
|
115
|
|
|
116
|
export default Note |