|
| 1 | +import { useForm } from 'react-hook-form' |
| 2 | +import { |
| 3 | + Form, |
| 4 | + FormControl, |
| 5 | + FormDescription, |
| 6 | + FormField, |
| 7 | + FormItem, |
| 8 | + FormLabel, |
| 9 | + FormMessage, |
| 10 | + Input, |
| 11 | + Label, |
| 12 | + MultipleSelect, |
| 13 | + Select, |
| 14 | + SelectContent, |
| 15 | + SelectItem, |
| 16 | + SelectTrigger, |
| 17 | + SelectValue, |
| 18 | +} from '../ui' |
| 19 | +import { useErrorsLngChange } from '@/hooks/use-errors-lng-change' |
| 20 | +import { useTranslation } from 'react-i18next' |
| 21 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 22 | +import { TFlowInput, useFlowInputSchema } from '@/lib/schema/flow-input' |
| 23 | +import { queryChannelsForSelectOption } from '@/lib/query-options/channel' |
| 24 | +import { useParams } from 'react-router-dom' |
| 25 | +import { useSuspenseQuery } from '@tanstack/react-query' |
| 26 | +import i18n from '@/i18n' |
| 27 | + |
| 28 | +const LANGS: Record<string, string> = { |
| 29 | + vi: i18n.t('common:langs.vi'), |
| 30 | + en: i18n.t('common:langs.en'), |
| 31 | +} |
| 32 | + |
| 33 | +type Props = { |
| 34 | + id?: string |
| 35 | + onSubmit?: (data: TFlowInput) => void |
| 36 | + defaultValues?: TFlowInput |
| 37 | +} |
| 38 | + |
| 39 | +export const GeneralSettingForm = ({ |
| 40 | + id = 'general-setting-form', |
| 41 | + onSubmit, |
| 42 | + defaultValues, |
| 43 | +}: Props) => { |
| 44 | + const { id: flowId } = useParams() |
| 45 | + const { data } = useSuspenseQuery( |
| 46 | + queryChannelsForSelectOption(flowId as string), |
| 47 | + ) |
| 48 | + const { t } = useTranslation('forms') |
| 49 | + const schema = useFlowInputSchema() |
| 50 | + const form = useForm<TFlowInput>({ |
| 51 | + resolver: zodResolver(schema), |
| 52 | + mode: 'onChange', |
| 53 | + defaultValues: { |
| 54 | + ...defaultValues, |
| 55 | + channelIds: data |
| 56 | + .filter((item) => item.isSelected) |
| 57 | + .map((item) => item.value), |
| 58 | + }, |
| 59 | + }) |
| 60 | + |
| 61 | + useErrorsLngChange(form) |
| 62 | + |
| 63 | + const handleSubmit = (data: TFlowInput) => { |
| 64 | + if (onSubmit) { |
| 65 | + onSubmit(data) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <Form {...form}> |
| 71 | + <form |
| 72 | + className='space-y-3' |
| 73 | + id={id} |
| 74 | + onSubmit={form.handleSubmit(handleSubmit)} |
| 75 | + > |
| 76 | + <FormField |
| 77 | + name='channelIds' |
| 78 | + control={form.control} |
| 79 | + render={({ field }) => ( |
| 80 | + <FormItem> |
| 81 | + <FormLabel>{t('channels.label')}</FormLabel> |
| 82 | + <FormControl> |
| 83 | + <MultipleSelect |
| 84 | + options={ |
| 85 | + data?.map((item) => ({ |
| 86 | + label: item.label, |
| 87 | + value: item.value, |
| 88 | + })) || [] |
| 89 | + } |
| 90 | + onChange={field.onChange} |
| 91 | + values={field.value} |
| 92 | + placeholder={t('channels.placeholder')} |
| 93 | + /> |
| 94 | + </FormControl> |
| 95 | + <FormMessage /> |
| 96 | + </FormItem> |
| 97 | + )} |
| 98 | + /> |
| 99 | + <FormField |
| 100 | + control={form.control} |
| 101 | + name='settings' |
| 102 | + render={({ field }) => ( |
| 103 | + <FormItem> |
| 104 | + <FormLabel> |
| 105 | + <Label>{t('bot_lang.label')}</Label> |
| 106 | + </FormLabel> |
| 107 | + |
| 108 | + <Select |
| 109 | + onValueChange={(value) => { |
| 110 | + field.onChange([ |
| 111 | + { |
| 112 | + type: 'language', |
| 113 | + value, |
| 114 | + default: true, |
| 115 | + label: 'Language', |
| 116 | + }, |
| 117 | + ]) |
| 118 | + }} |
| 119 | + defaultValue={ |
| 120 | + field.value?.find((setting) => setting.type === 'language') |
| 121 | + ?.value |
| 122 | + } |
| 123 | + > |
| 124 | + <FormControl> |
| 125 | + <SelectTrigger> |
| 126 | + <SelectValue placeholder={t('bot_lang.placeholder')} /> |
| 127 | + </SelectTrigger> |
| 128 | + </FormControl> |
| 129 | + <SelectContent> |
| 130 | + {Object.keys(LANGS).map((lang) => { |
| 131 | + return ( |
| 132 | + <SelectItem key={lang} value={lang}> |
| 133 | + {LANGS[lang]} |
| 134 | + </SelectItem> |
| 135 | + ) |
| 136 | + })} |
| 137 | + </SelectContent> |
| 138 | + </Select> |
| 139 | + <FormDescription>{t('bot_lang.description')}</FormDescription> |
| 140 | + </FormItem> |
| 141 | + )} |
| 142 | + /> |
| 143 | + </form> |
| 144 | + </Form> |
| 145 | + ) |
| 146 | +} |
| 147 | + |
| 148 | +export default GeneralSettingForm |
0 commit comments