|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { updateOrganizationAdvancedModeAction } from '@/actions/organization/update-organization-advanced-mode-action'; |
| 4 | +import { organizationAdvancedModeSchema } from '@/actions/schema'; |
| 5 | +import { |
| 6 | + Card, |
| 7 | + CardContent, |
| 8 | + CardDescription, |
| 9 | + CardFooter, |
| 10 | + CardHeader, |
| 11 | + CardTitle, |
| 12 | +} from '@comp/ui/card'; |
| 13 | +import { Form, FormControl, FormField, FormItem, FormMessage } from '@comp/ui/form'; |
| 14 | +import { Switch } from '@comp/ui/switch'; |
| 15 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 16 | +import { Loader2 } from 'lucide-react'; |
| 17 | +import { useAction } from 'next-safe-action/hooks'; |
| 18 | +import { useForm } from 'react-hook-form'; |
| 19 | +import { toast } from 'sonner'; |
| 20 | +import type { z } from 'zod'; |
| 21 | + |
| 22 | +export function UpdateOrganizationAdvancedMode({ |
| 23 | + advancedModeEnabled, |
| 24 | +}: { |
| 25 | + advancedModeEnabled: boolean; |
| 26 | +}) { |
| 27 | + const updateAdvancedMode = useAction(updateOrganizationAdvancedModeAction, { |
| 28 | + onSuccess: () => { |
| 29 | + toast.success('Advanced mode setting updated'); |
| 30 | + }, |
| 31 | + onError: () => { |
| 32 | + toast.error('Error updating advanced mode setting'); |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + const form = useForm<z.infer<typeof organizationAdvancedModeSchema>>({ |
| 37 | + resolver: zodResolver(organizationAdvancedModeSchema), |
| 38 | + defaultValues: { |
| 39 | + advancedModeEnabled, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + const onSubmit = (data: z.infer<typeof organizationAdvancedModeSchema>) => { |
| 44 | + updateAdvancedMode.execute(data); |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <Form {...form}> |
| 49 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 50 | + <Card> |
| 51 | + <CardHeader> |
| 52 | + <CardTitle>Advanced Mode</CardTitle> |
| 53 | + <CardDescription> |
| 54 | + <div className="max-w-[600px]"> |
| 55 | + Enable advanced mode to access additional features like the Controls page. This |
| 56 | + setting is designed for users who need access to more detailed compliance management |
| 57 | + tools. |
| 58 | + </div> |
| 59 | + </CardDescription> |
| 60 | + </CardHeader> |
| 61 | + <CardContent> |
| 62 | + <FormField |
| 63 | + control={form.control} |
| 64 | + name="advancedModeEnabled" |
| 65 | + render={({ field }) => ( |
| 66 | + <FormItem className="flex flex-row items-center justify-between rounded-xs border p-3"> |
| 67 | + <div className="space-y-0.5"> |
| 68 | + <div className="text-base">Advanced Mode</div> |
| 69 | + <div className="text-muted-foreground text-sm"> |
| 70 | + Show advanced features and pages |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + <FormControl> |
| 74 | + <Switch |
| 75 | + checked={field.value} |
| 76 | + onCheckedChange={(checked) => { |
| 77 | + field.onChange(checked); |
| 78 | + // Auto-submit when switch is toggled |
| 79 | + form.handleSubmit(onSubmit)(); |
| 80 | + }} |
| 81 | + /> |
| 82 | + </FormControl> |
| 83 | + <FormMessage /> |
| 84 | + </FormItem> |
| 85 | + )} |
| 86 | + /> |
| 87 | + </CardContent> |
| 88 | + <CardFooter className="flex justify-between"> |
| 89 | + <div className="text-muted-foreground text-xs"> |
| 90 | + Changes are saved automatically when toggled. |
| 91 | + </div> |
| 92 | + {updateAdvancedMode.status === 'executing' && ( |
| 93 | + <div className="flex items-center text-muted-foreground text-sm"> |
| 94 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 95 | + Saving... |
| 96 | + </div> |
| 97 | + )} |
| 98 | + </CardFooter> |
| 99 | + </Card> |
| 100 | + </form> |
| 101 | + </Form> |
| 102 | + ); |
| 103 | +} |
0 commit comments