|
| 1 | +import { useMutation } from "@tanstack/react-query"; |
1 | 2 | import type { Product } from "@decocms/apps/commerce/types"; |
2 | | -import { AppContext } from "../../apps/site"; |
3 | | -import { useComponent } from "../../sections/Component"; |
| 3 | +import { invoke } from "../../runtime"; |
| 4 | +import { useUser } from "../../platform/user"; |
| 5 | +import type { NotifyMeResult } from "../../actions/notifyMe/subscribe"; |
4 | 6 |
|
5 | 7 | export interface Props { |
6 | 8 | productID: Product["productID"]; |
7 | 9 | } |
8 | 10 |
|
9 | | -export const action = async (props: Props, req: Request, ctx: AppContext) => { |
10 | | - const form = await req.formData(); |
11 | | - |
12 | | - const name = `${form.get("name") ?? ""}`; |
13 | | - const email = `${form.get("email") ?? ""}`; |
14 | | - await (ctx as any).invoke("vtex/actions/notifyme.ts", { |
15 | | - skuId: props.productID, |
16 | | - name, |
17 | | - email, |
| 11 | +export default function OutOfStock({ productID }: Props) { |
| 12 | + const { user } = useUser(); |
| 13 | + const notify = useMutation({ |
| 14 | + mutationFn: async (input: { email: string; name?: string }) => { |
| 15 | + const result = (await invoke.site.actions.notifyMe.subscribe({ |
| 16 | + skuId: productID, |
| 17 | + email: input.email, |
| 18 | + name: input.name, |
| 19 | + })) as NotifyMeResult | undefined; |
| 20 | + if (!result?.success) throw new Error("Notify request failed"); |
| 21 | + return result; |
| 22 | + }, |
18 | 23 | }); |
19 | 24 |
|
20 | | - return props; |
21 | | -}; |
| 25 | + if (notify.isSuccess) { |
| 26 | + return ( |
| 27 | + <div className="form-control gap-2"> |
| 28 | + <span className="text-base">You're on the list!</span> |
| 29 | + <span className="text-sm text-base-400"> |
| 30 | + We'll email you when this product is back in stock. |
| 31 | + </span> |
| 32 | + </div> |
| 33 | + ); |
| 34 | + } |
22 | 35 |
|
23 | | -export default function Notify({ productID }: Props) { |
24 | 36 | return ( |
25 | 37 | <form |
26 | 38 | className="form-control justify-start gap-2" |
27 | | - hx-sync="this:replace" |
28 | | - hx-indicator="this" |
29 | | - hx-swap="none" |
30 | | - hx-post={useComponent<Props>(import.meta.url, { productID })} |
| 39 | + onSubmit={(e) => { |
| 40 | + e.preventDefault(); |
| 41 | + const data = new FormData(e.currentTarget); |
| 42 | + const email = `${data.get("email") ?? ""}`.trim(); |
| 43 | + const name = `${data.get("name") ?? ""}`.trim(); |
| 44 | + if (email) notify.mutate({ email, name }); |
| 45 | + }} |
31 | 46 | > |
32 | 47 | <span className="text-base">This product is currently unavailable</span> |
33 | 48 | <span className="text-sm">Notify me when it's back in stock</span> |
34 | 49 |
|
35 | 50 | <input placeholder="Name" className="input input-bordered" name="name" /> |
36 | | - <input placeholder="Email" className="input input-bordered" name="email" /> |
| 51 | + <input |
| 52 | + placeholder="Email" |
| 53 | + type="email" |
| 54 | + required |
| 55 | + className="input input-bordered" |
| 56 | + name="email" |
| 57 | + defaultValue={user?.email ?? ""} |
| 58 | + /> |
37 | 59 |
|
38 | | - <button type="button" className="btn btn-primary no-animation"> |
39 | | - <span className="[.htmx-request_&]:hidden inline">Submit</span> |
40 | | - <span className="[.htmx-request_&]:inline hidden loading loading-spinner loading-xs" /> |
| 60 | + <button |
| 61 | + type="submit" |
| 62 | + className="btn btn-primary no-animation" |
| 63 | + disabled={notify.isPending} |
| 64 | + > |
| 65 | + {notify.isPending |
| 66 | + ? <span className="loading loading-spinner loading-xs" /> |
| 67 | + : <span>Submit</span>} |
41 | 68 | </button> |
| 69 | + |
| 70 | + {notify.isError && ( |
| 71 | + <span className="text-sm text-error"> |
| 72 | + Something went wrong. Please try again. |
| 73 | + </span> |
| 74 | + )} |
42 | 75 | </form> |
43 | 76 | ); |
44 | 77 | } |
0 commit comments