gitscope
← marzneshin / Diff · 2e6c8cb → 2e6c8cb
Changes 2e6c8cb..2e6c8cb
Patch commit
diff --git a/dashboard/src/features/users/constants.ts b/dashboard/src/features/users/constants.ts
new file mode 100644
index 0000000..3ecbc9a
--- /dev/null
+++ b/dashboard/src/features/users/constants.ts
@@ -0,0 +1,2 @@
+
+export const DATA_LIMIT_METRIC = 1024 * 1024 * 1024
diff --git a/dashboard/src/features/users/dialogs/mutation/fields/services.tsx b/dashboard/src/features/users/dialogs/mutation/fields/services.tsx
index bd2b341..bcfc081 100644
--- a/dashboard/src/features/users/dialogs/mutation/fields/services.tsx
+++ b/dashboard/src/features/users/dialogs/mutation/fields/services.tsx
@@ -21,7 +21,6 @@ export const ServicesField: FC<ServicesFieldProps> = () => {
const { data } = useServicesQuery({ page: 1, size: 100 });
const { t } = useTranslation();
- console.log(form.getValues().services)
return (
<FormField
control={form.control}
@@ -31,35 +30,35 @@ export const ServicesField: FC<ServicesFieldProps> = () => {
<FormLabel>{t('services')}</FormLabel>
<ScrollArea className="flex flex-col justify-start px-1 h-[22rem]">
{data.entity.map(service => (
- <FormField
- control={form.control}
- name='services'
- render={({ field }) => (
- <FormItem key={service.id}>
- <FormControl>
- <div
- className={cn("flex mb-2 flex-row gap-2 items-center p-3 rounded-md border", { "bg-secondary": field.value?.includes(service.id) })}>
- <Checkbox
- checked={field.value?.includes(service.id)}
- onCheckedChange={(checked) => {
- return checked
- ? field.onChange([...field.value, service.id])
- : field.onChange(
- field.value?.filter(
- (value: number) => value !== service.id
- )
+ <FormField
+ control={form.control}
+ name='services'
+ render={({ field }) => (
+ <FormItem key={service.id}>
+ <FormControl>
+ <div
+ className={cn("flex mb-2 flex-row gap-2 items-center p-3 rounded-md border", { "bg-secondary": field.value?.includes(service.id) })}>
+ <Checkbox
+ checked={field.value?.includes(service.id)}
+ onCheckedChange={(checked) => {
+ return checked
+ ? field.onChange([...field.value, service.id])
+ : field.onChange(
+ field.value?.filter(
+ (value: number) => value !== service.id
)
- }}
- />
- <FormLabel className="flex flex-row justify-between items-center w-full">
- <ServiceCard service={service} />
- </FormLabel>
- </div>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
+ )
+ }}
+ />
+ <FormLabel className="flex flex-row justify-between items-center w-full">
+ <ServiceCard service={service} />
+ </FormLabel>
+ </div>
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
))}
</ScrollArea>
</FormItem>
diff --git a/dashboard/src/features/users/dialogs/mutation/index.tsx b/dashboard/src/features/users/dialogs/mutation/index.tsx
index 4905a5e..7970702 100644
--- a/dashboard/src/features/users/dialogs/mutation/index.tsx
+++ b/dashboard/src/features/users/dialogs/mutation/index.tsx
@@ -13,6 +13,7 @@ import {
} from '@marzneshin/components';
import { useTranslation } from 'react-i18next';
import {
+ DATA_LIMIT_METRIC,
UserMutationType,
useUsersCreationMutation,
useUsersUpdateMutation
@@ -55,7 +56,6 @@ export const UsersMutationDialog: FC<UsersMutationDialogProps> = ({
on_hold_timeout: undefined,
}), []);
-
const { form, handleSubmit } = useMutationDialog({
entity,
onOpenChange,
@@ -63,6 +63,7 @@ export const UsersMutationDialog: FC<UsersMutationDialogProps> = ({
updateMutation: useUsersUpdateMutation(),
schema: UserSchema,
getDefaultValue: getDefaultValues,
+ loadFormtter: (d) => ({ ...d, data_limit: (d.data_limit ? d.data_limit : 0) / DATA_LIMIT_METRIC })
})
const [
@@ -98,7 +99,7 @@ export const UsersMutationDialog: FC<UsersMutationDialogProps> = ({
form.setValue("status", "active")
if (selectedExpirationMethodTab === 'onhold') {
form.setValue("status", "on_hold")
- form.setValue("expire", null);
+ form.setValue("expire", undefined);
form.clearErrors("expire");
} else if (selectedExpirationMethodTab === "unlimited") {
form.setValue("expire", undefined);
diff --git a/dashboard/src/features/users/dialogs/mutation/schema.ts b/dashboard/src/features/users/dialogs/mutation/schema.ts
index 56a7314..507c910 100644
--- a/dashboard/src/features/users/dialogs/mutation/schema.ts
+++ b/dashboard/src/features/users/dialogs/mutation/schema.ts
@@ -1,4 +1,5 @@
import { z } from 'zod';
+import { DATA_LIMIT_METRIC } from '@marzneshin/features/users';
export const UserSchema = z.object({
username: z.string().min(1, { message: 'Username is required' }),
@@ -10,7 +11,7 @@ export const UserSchema = z.object({
.nullable()
.optional()
.transform((str) => {
- if (str) return Number((parseFloat(String(str)) * 1073741824).toFixed(5));
+ if (str) return Number((parseFloat(String(str)) * DATA_LIMIT_METRIC).toFixed(5));
return 0;
}),
expire: z.string().or(z.number()).nullable().optional(),
diff --git a/dashboard/src/features/users/index.ts b/dashboard/src/features/users/index.ts
index c2ca7fa..cca1aef 100644
--- a/dashboard/src/features/users/index.ts
+++ b/dashboard/src/features/users/index.ts
@@ -3,3 +3,4 @@ export * from './types'
export * from './tables'
export * from './components'
export * from './dialogs'
+export * from './constants'
diff --git a/dashboard/src/hooks/use-mutation-dialog/index.ts b/dashboard/src/hooks/use-mutation-dialog/index.ts
index 00037d2..b8d61e0 100644
--- a/dashboard/src/hooks/use-mutation-dialog/index.ts
+++ b/dashboard/src/hooks/use-mutation-dialog/index.ts
@@ -12,6 +12,7 @@ interface IMutationDialog<TData, TError = unknown, TVariables = unknown> {
updateMutation: UseMutationResult<TData, TError, TVariables>
schema: ZodSchema
getDefaultValue: () => FieldValues
+ loadFormtter?: (d: FieldValues) => FieldValues
}
/** useMutationDialog - Dialog mutation hook
@@ -39,9 +40,11 @@ export const useMutationDialog = <TData, TError = unknown, TVariables = unknown>
updateMutation,
schema,
getDefaultValue,
+ loadFormtter = (s) => s,
}: IMutationDialog<TData, TError, TVariables>) => {
+
const form = useForm({
- defaultValues: entity ? (entity as FieldValues) : (getDefaultValue()),
+ defaultValues: entity ? loadFormtter(entity) : getDefaultValue(),
resolver: zodResolver(schema)
});
@@ -56,10 +59,11 @@ export const useMutationDialog = <TData, TError = unknown, TVariables = unknown>
useEffect(() => {
if (entity) {
- form.reset(entity)
+ form.reset(loadFormtter(entity))
} else {
form.reset(getDefaultValue());
}
+ // eslint-disable-next-line
}, [entity, form, getDefaultValue]);
const handleSubmit = form.handleSubmit(submit as SubmitHandler<FieldValues>);