| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import { zodResolver } from '@hookform/resolvers/zod'
- import { useForm } from 'react-hook-form'
- import { z } from 'zod'
- import { Button } from '@/components/custom/button'
- import { Checkbox } from '@/components/ui/checkbox'
- import {
- Form,
- FormControl,
- FormDescription,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
- } from '@/components/ui/form'
- import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
- import { Switch } from '@/components/ui/switch'
- import { toast } from '@/components/ui/use-toast'
- import { Link } from 'react-router-dom'
- const notificationsFormSchema = z.object({
- type: z.enum(['all', 'mentions', 'none'], {
- required_error: 'You need to select a notification type.',
- }),
- mobile: z.boolean().default(false).optional(),
- communication_emails: z.boolean().default(false).optional(),
- social_emails: z.boolean().default(false).optional(),
- marketing_emails: z.boolean().default(false).optional(),
- security_emails: z.boolean(),
- })
- type NotificationsFormValues = z.infer<typeof notificationsFormSchema>
- // This can come from your database or API.
- const defaultValues: Partial<NotificationsFormValues> = {
- communication_emails: false,
- marketing_emails: false,
- social_emails: true,
- security_emails: true,
- }
- export function NotificationsForm() {
- const form = useForm<NotificationsFormValues>({
- resolver: zodResolver(notificationsFormSchema),
- defaultValues,
- })
- function onSubmit(data: NotificationsFormValues) {
- toast({
- title: 'You submitted the following values:',
- description: (
- <pre className='mt-2 w-[340px] rounded-md bg-slate-950 p-4'>
- <code className='text-white'>{JSON.stringify(data, null, 2)}</code>
- </pre>
- ),
- })
- }
- return (
- <Form {...form}>
- <form onSubmit={form.handleSubmit(onSubmit)} className='space-y-8'>
- <FormField
- control={form.control}
- name='type'
- render={({ field }) => (
- <FormItem className='space-y-3'>
- <FormLabel>Notify me about...</FormLabel>
- <FormControl>
- <RadioGroup
- onValueChange={field.onChange}
- defaultValue={field.value}
- className='flex flex-col space-y-1'
- >
- <FormItem className='flex items-center space-x-3 space-y-0'>
- <FormControl>
- <RadioGroupItem value='all' />
- </FormControl>
- <FormLabel className='font-normal'>
- All new messages
- </FormLabel>
- </FormItem>
- <FormItem className='flex items-center space-x-3 space-y-0'>
- <FormControl>
- <RadioGroupItem value='mentions' />
- </FormControl>
- <FormLabel className='font-normal'>
- Direct messages and mentions
- </FormLabel>
- </FormItem>
- <FormItem className='flex items-center space-x-3 space-y-0'>
- <FormControl>
- <RadioGroupItem value='none' />
- </FormControl>
- <FormLabel className='font-normal'>Nothing</FormLabel>
- </FormItem>
- </RadioGroup>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <div>
- <h3 className='mb-4 text-lg font-medium'>Email Notifications</h3>
- <div className='space-y-4'>
- <FormField
- control={form.control}
- name='communication_emails'
- render={({ field }) => (
- <FormItem className='flex flex-row items-center justify-between rounded-lg border p-4'>
- <div className='space-y-0.5'>
- <FormLabel className='text-base'>
- Communication emails
- </FormLabel>
- <FormDescription>
- Receive emails about your account activity.
- </FormDescription>
- </div>
- <FormControl>
- <Switch
- checked={field.value}
- onCheckedChange={field.onChange}
- />
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name='marketing_emails'
- render={({ field }) => (
- <FormItem className='flex flex-row items-center justify-between rounded-lg border p-4'>
- <div className='space-y-0.5'>
- <FormLabel className='text-base'>
- Marketing emails
- </FormLabel>
- <FormDescription>
- Receive emails about new products, features, and more.
- </FormDescription>
- </div>
- <FormControl>
- <Switch
- checked={field.value}
- onCheckedChange={field.onChange}
- />
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name='social_emails'
- render={({ field }) => (
- <FormItem className='flex flex-row items-center justify-between rounded-lg border p-4'>
- <div className='space-y-0.5'>
- <FormLabel className='text-base'>Social emails</FormLabel>
- <FormDescription>
- Receive emails for friend requests, follows, and more.
- </FormDescription>
- </div>
- <FormControl>
- <Switch
- checked={field.value}
- onCheckedChange={field.onChange}
- />
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name='security_emails'
- render={({ field }) => (
- <FormItem className='flex flex-row items-center justify-between rounded-lg border p-4'>
- <div className='space-y-0.5'>
- <FormLabel className='text-base'>Security emails</FormLabel>
- <FormDescription>
- Receive emails about your account activity and security.
- </FormDescription>
- </div>
- <FormControl>
- <Switch
- checked={field.value}
- onCheckedChange={field.onChange}
- disabled
- aria-readonly
- />
- </FormControl>
- </FormItem>
- )}
- />
- </div>
- </div>
- <FormField
- control={form.control}
- name='mobile'
- render={({ field }) => (
- <FormItem className='flex flex-row items-start space-x-3 space-y-0'>
- <FormControl>
- <Checkbox
- checked={field.value}
- onCheckedChange={field.onChange}
- />
- </FormControl>
- <div className='space-y-1 leading-none'>
- <FormLabel>
- Use different settings for my mobile devices
- </FormLabel>
- <FormDescription>
- You can manage your mobile notifications in the{' '}
- <Link
- to='/settings'
- className='underline decoration-dashed underline-offset-4 hover:decoration-solid'
- >
- mobile settings
- </Link>{' '}
- page.
- </FormDescription>
- </div>
- </FormItem>
- )}
- />
- <Button type='submit'>Update notifications</Button>
- </form>
- </Form>
- )
- }
|