|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useI18n } from "@/locales/client"; |
| 4 | +import { |
| 5 | + Card, |
| 6 | + CardContent, |
| 7 | + CardDescription, |
| 8 | + CardFooter, |
| 9 | + CardHeader, |
| 10 | + CardTitle, |
| 11 | +} from "@comp/ui/card"; |
| 12 | +import { |
| 13 | + Form, |
| 14 | + FormControl, |
| 15 | + FormDescription, |
| 16 | + FormField, |
| 17 | + FormItem, |
| 18 | + FormLabel, |
| 19 | + FormMessage, |
| 20 | +} from "@comp/ui/form"; |
| 21 | +import { Switch } from "@comp/ui/switch"; |
| 22 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 23 | +import { useAction } from "next-safe-action/hooks"; |
| 24 | +import { useForm } from "react-hook-form"; |
| 25 | +import { toast } from "sonner"; |
| 26 | +import { z } from "zod"; |
| 27 | +import { trustPortalSwitchAction } from "../actions/trust-portal-switch"; |
| 28 | + |
| 29 | +const trustPortalSwitchSchema = z.object({ |
| 30 | + enabled: z.boolean(), |
| 31 | +}); |
| 32 | + |
| 33 | +export function TrustPortalSwitch({ |
| 34 | + enabled, |
| 35 | + slug, |
| 36 | +}: { |
| 37 | + enabled: boolean; |
| 38 | + slug: string; |
| 39 | +}) { |
| 40 | + const t = useI18n(); |
| 41 | + |
| 42 | + const trustPortalSwitch = useAction(trustPortalSwitchAction, { |
| 43 | + onSuccess: () => { |
| 44 | + toast.success("Trust portal status updated"); |
| 45 | + }, |
| 46 | + onError: () => { |
| 47 | + toast.error("Failed to update trust portal status"); |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const form = useForm<z.infer<typeof trustPortalSwitchSchema>>({ |
| 52 | + resolver: zodResolver(trustPortalSwitchSchema), |
| 53 | + defaultValues: { |
| 54 | + enabled: enabled, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const onSubmit = async (data: z.infer<typeof trustPortalSwitchSchema>) => { |
| 59 | + await trustPortalSwitch.execute(data); |
| 60 | + }; |
| 61 | + |
| 62 | + const onChange = (checked: boolean) => { |
| 63 | + form.setValue("enabled", checked); |
| 64 | + trustPortalSwitch.execute({ enabled: checked }); |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <Form {...form}> |
| 69 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 70 | + <Card> |
| 71 | + <CardHeader> |
| 72 | + <CardTitle>Trust Portal Configuration</CardTitle> |
| 73 | + <CardDescription className="space-y-4 flex flex-row justify-between"> |
| 74 | + <div className="max-w-[600px]"> |
| 75 | + Enable the trust portal for your organization. |
| 76 | + </div> |
| 77 | + </CardDescription> |
| 78 | + </CardHeader> |
| 79 | + <CardContent> |
| 80 | + <FormField |
| 81 | + control={form.control} |
| 82 | + name="enabled" |
| 83 | + render={({ field }) => ( |
| 84 | + <FormItem className="flex flex-row items-center justify-between rounded-lg border p-3 shadow-sm"> |
| 85 | + <div className="space-y-0.5"> |
| 86 | + <FormLabel>Publish Trust Portal</FormLabel> |
| 87 | + <FormDescription> |
| 88 | + Enable the trust portal for your organization. |
| 89 | + </FormDescription> |
| 90 | + </div> |
| 91 | + <FormControl> |
| 92 | + <Switch |
| 93 | + checked={field.value} |
| 94 | + onCheckedChange={onChange} |
| 95 | + disabled={trustPortalSwitch.status === "executing"} |
| 96 | + /> |
| 97 | + </FormControl> |
| 98 | + </FormItem> |
| 99 | + )} |
| 100 | + /> |
| 101 | + </CardContent> |
| 102 | + <CardFooter className="flex justify-between"> |
| 103 | + <div className="text-xs text-muted-foreground"> |
| 104 | + Your trust portal will be live & accessible at https://trust.trycomp.ai/{slug} |
| 105 | + </div> |
| 106 | + </CardFooter> |
| 107 | + </Card> |
| 108 | + </form> |
| 109 | + </Form> |
| 110 | + ); |
| 111 | +} |
0 commit comments