diff --git a/dashboard/src/features/users/dialogs/mutation/fields/services.tsx b/dashboard/src/features/users/dialogs/mutation/fields/services.tsx index e5c43f4..a526351 100644 --- a/dashboard/src/features/users/dialogs/mutation/fields/services.tsx +++ b/dashboard/src/features/users/dialogs/mutation/fields/services.tsx @@ -4,9 +4,8 @@ import { FormItem, FormLabel, FormMessage, - ToggleGroup, - ToggleGroupItem, - ScrollArea + ScrollArea, + Toggle } from '@marzneshin/components'; import { useServicesQuery } from '@marzneshin/features/services'; import { ServiceCard } from '@marzneshin/features/users'; @@ -14,54 +13,49 @@ import { FC } from 'react' import { useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; -interface ServicesFieldProps { - services: number[] - setServices: (s: number[]) => void -} +interface ServicesFieldProps { } -export const ServicesField: FC = ({ - services, - setServices -}) => { +export const ServicesField: FC = () => { const form = useFormContext(); const { data } = useServicesQuery({ page: 1, size: 100 }); const { t } = useTranslation(); - const handleToggle = (selectedServiceIds: string[]) => { - setServices(selectedServiceIds.map(Number)); - }; - return ( - ( - - {t('services')} - - - - {data.entity.map(service => ( - + {t('services')} + + {data.entity.map(service => ( + ( + + + { + return checked + ? field.onChange([...field.value, service.id]) + : field.onChange( + field.value?.filter( + (value: number) => value !== service.id + ) + ) + }} + {...form.register(`services.${service.id}`, { shouldUnregister: true })} className="px-0 mb-1 w-full" > - - ))} - - - - - - )} - /> + + + + + )} + /> + ))} + + ); }; diff --git a/dashboard/src/features/users/dialogs/mutation/index.tsx b/dashboard/src/features/users/dialogs/mutation/index.tsx index 9087c0b..becdb6b 100644 --- a/dashboard/src/features/users/dialogs/mutation/index.tsx +++ b/dashboard/src/features/users/dialogs/mutation/index.tsx @@ -55,7 +55,6 @@ export const UsersMutationDialog: FC = ({ on_hold_timeout: null, }), []); - const [services, setServices] = useState(entity ? entity.services : []) const { form, handleSubmit } = useMutationDialog({ entity, @@ -66,20 +65,15 @@ export const UsersMutationDialog: FC = ({ getDefaultValue: getDefaultValues, }) - useEffect(() => { - form.setValue("services", services.map(Number).filter(item => typeof item === 'number')) - }, [services, form]) - - const [selectedTab, setSelectedTab] = useState<'determined' | 'onhold' | 'unlimited' | string>('determined'); - + const expire = form.watch().expire useEffect(() => { + form.setValue("status", "active") if (selectedTab === 'onhold') { form.setValue("status", "on_hold") form.setValue("expire", null); form.clearErrors("expire"); } else if (selectedTab === "unlimited") { - form.setValue("status", "active") form.setValue("expire", 0); form.setValue("on_hold_expire_duration", 0); form.setValue("on_hold_timeout", null); @@ -87,13 +81,12 @@ export const UsersMutationDialog: FC = ({ form.clearErrors("on_hold_expire_duration"); form.clearErrors("on_hold_timeout"); } else { - form.setValue("status", "active") form.setValue("on_hold_expire_duration", 0); form.setValue("on_hold_timeout", null); form.clearErrors("on_hold_expire_duration"); form.clearErrors("on_hold_timeout"); } - }, [selectedTab, form]); + }, [selectedTab, form, expire]); return ( @@ -146,7 +139,7 @@ export const UsersMutationDialog: FC = ({
- +