|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Spinner } from "@/components/ui/Spinner/Spinner"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { Skeleton } from "@/components/ui/skeleton"; |
| 6 | +import { SupportForm_SelectInput } from "components/help/contact-forms/shared/SupportForm_SelectInput"; |
| 7 | +import dynamic from "next/dynamic"; |
| 8 | +import { type ReactElement, useEffect, useRef, useState } from "react"; |
| 9 | +import { useFormState, useFormStatus } from "react-dom"; |
| 10 | +import { toast } from "sonner"; |
| 11 | +import { createTicketAction } from "./create-ticket.action"; |
| 12 | + |
| 13 | +const ConnectSupportForm = dynamic( |
| 14 | + () => import("../../../../../components/help/contact-forms/connect"), |
| 15 | + { |
| 16 | + ssr: false, |
| 17 | + loading: () => <Skeleton className="h-12" />, |
| 18 | + }, |
| 19 | +); |
| 20 | +const EngineSupportForm = dynamic( |
| 21 | + () => import("../../../../../components/help/contact-forms/engine"), |
| 22 | + { |
| 23 | + ssr: false, |
| 24 | + loading: () => <Skeleton className="h-12" />, |
| 25 | + }, |
| 26 | +); |
| 27 | +const ContractSupportForm = dynamic( |
| 28 | + () => import("../../../../../components/help/contact-forms/contracts"), |
| 29 | + { |
| 30 | + ssr: false, |
| 31 | + loading: () => <Skeleton className="h-12" />, |
| 32 | + }, |
| 33 | +); |
| 34 | +const AccountSupportForm = dynamic( |
| 35 | + () => import("../../../../../components/help/contact-forms/account"), |
| 36 | + { |
| 37 | + ssr: false, |
| 38 | + loading: () => <Skeleton className="h-12" />, |
| 39 | + }, |
| 40 | +); |
| 41 | +const OtherSupportForm = dynamic( |
| 42 | + () => import("../../../../../components/help/contact-forms/other"), |
| 43 | + { |
| 44 | + ssr: false, |
| 45 | + loading: () => <Skeleton className="h-12" />, |
| 46 | + }, |
| 47 | +); |
| 48 | + |
| 49 | +const productOptions: { label: string; component: ReactElement }[] = [ |
| 50 | + { |
| 51 | + label: "Connect", |
| 52 | + component: <ConnectSupportForm />, |
| 53 | + }, |
| 54 | + { |
| 55 | + label: "Engine", |
| 56 | + component: <EngineSupportForm />, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "Contracts", |
| 60 | + component: <ContractSupportForm />, |
| 61 | + }, |
| 62 | + { |
| 63 | + label: "Account", |
| 64 | + component: <AccountSupportForm />, |
| 65 | + }, |
| 66 | + { |
| 67 | + label: "Other", |
| 68 | + component: <OtherSupportForm />, |
| 69 | + }, |
| 70 | +]; |
| 71 | + |
| 72 | +export function CreateTicket() { |
| 73 | + const formRef = useRef<HTMLFormElement>(null); |
| 74 | + const [productLabel, setProductLabel] = useState(""); |
| 75 | + |
| 76 | + const [state, formAction] = useFormState(createTicketAction, { |
| 77 | + message: "", |
| 78 | + success: false, |
| 79 | + }); |
| 80 | + // needed here |
| 81 | + // eslint-disable-next-line no-restricted-syntax |
| 82 | + useEffect(() => { |
| 83 | + if (!state.message) { |
| 84 | + return; |
| 85 | + } |
| 86 | + if (state.success) { |
| 87 | + toast.success(state.message); |
| 88 | + } else { |
| 89 | + toast.error(state.message); |
| 90 | + } |
| 91 | + }, [state.success, state.message]); |
| 92 | + |
| 93 | + return ( |
| 94 | + <form |
| 95 | + ref={formRef} |
| 96 | + action={formAction} |
| 97 | + className="rounded-lg border bg-muted/50" |
| 98 | + > |
| 99 | + <div className="px-4 py-6 lg:px-6"> |
| 100 | + <h2 className="font-semibold text-2xl tracking-tight">Get Support</h2> |
| 101 | + <p className="text-muted-foreground"> |
| 102 | + We are here to help. Ask product questions, report problems, or leave |
| 103 | + feedback. |
| 104 | + </p> |
| 105 | + |
| 106 | + <div className="h-7" /> |
| 107 | + |
| 108 | + <div className="flex flex-col gap-6"> |
| 109 | + <SupportForm_SelectInput |
| 110 | + formLabel="What do you need help with?" |
| 111 | + name="product" |
| 112 | + options={productOptions.map((o) => o.label)} |
| 113 | + promptText="Select a product" |
| 114 | + onValueChange={setProductLabel} |
| 115 | + value={productLabel} |
| 116 | + required={true} |
| 117 | + /> |
| 118 | + {productOptions.find((o) => o.label === productLabel)?.component} |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + |
| 122 | + <div className="h-7" /> |
| 123 | + |
| 124 | + <div className="flex justify-end gap-3 border-t px-4 py-6 lg:px-6"> |
| 125 | + <Button |
| 126 | + onClick={() => { |
| 127 | + formRef.current?.reset(); |
| 128 | + setProductLabel(""); |
| 129 | + }} |
| 130 | + variant="outline" |
| 131 | + > |
| 132 | + Reset |
| 133 | + </Button> |
| 134 | + <SubmitButton /> |
| 135 | + </div> |
| 136 | + </form> |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +function SubmitButton() { |
| 141 | + const { pending } = useFormStatus(); |
| 142 | + return ( |
| 143 | + <Button |
| 144 | + type="submit" |
| 145 | + disabled={pending} |
| 146 | + className="flex min-w-24 flex-row gap-2" |
| 147 | + > |
| 148 | + {pending && <Spinner className="size-4" />} |
| 149 | + {pending ? "Submitting" : "Submit"} |
| 150 | + </Button> |
| 151 | + ); |
| 152 | +} |
0 commit comments