From 4d1c8eae8fbbdedea19ff473faafc9e5c1f1d6df Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Wed, 11 Dec 2024 10:05:30 +0100 Subject: [PATCH] feat(users): user detail can be edited now properly (#6135) --- ui/actions/users/users.ts | 32 ++++++++++++++++--------- ui/components/users/forms/edit-form.tsx | 24 ++++++++++++------- ui/types/formSchemas.ts | 19 ++------------- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/ui/actions/users/users.ts b/ui/actions/users/users.ts index cf3397a940..5f8cfa4b25 100644 --- a/ui/actions/users/users.ts +++ b/ui/actions/users/users.ts @@ -51,14 +51,28 @@ export const updateUser = async (formData: FormData) => { const session = await auth(); const keyServer = process.env.API_BASE_URL; - const userId = formData.get("userId"); - const userName = formData.get("name"); - const userPassword = formData.get("password"); - const userEmail = formData.get("email"); - const userCompanyName = formData.get("company_name"); + const userId = formData.get("userId") as string; // Ensure userId is a string + const userName = formData.get("name") as string | null; + const userPassword = formData.get("password") as string | null; + const userEmail = formData.get("email") as string | null; + const userCompanyName = formData.get("company_name") as string | null; const url = new URL(`${keyServer}/users/${userId}`); + // Prepare attributes to send based on changes + const attributes: Record = {}; + + // Add only changed fields + if (userName !== null) attributes.name = userName; + if (userEmail !== null) attributes.email = userEmail; + if (userCompanyName !== null) attributes.company_name = userCompanyName; + if (userPassword !== null) attributes.password = userPassword; + + // If no fields have changed, don't send the request + if (Object.keys(attributes).length === 0) { + return { error: "No changes detected" }; + } + try { const response = await fetch(url.toString(), { method: "PATCH", @@ -71,15 +85,11 @@ export const updateUser = async (formData: FormData) => { data: { type: "users", id: userId, - attributes: { - name: userName, - password: userPassword, - email: userEmail, - company_name: userCompanyName, - }, + attributes: attributes, }, }), }); + const data = await response.json(); revalidatePath("/users"); return parseStringify(data); diff --git a/ui/components/users/forms/edit-form.tsx b/ui/components/users/forms/edit-form.tsx index ece7080b5a..8f6788ac8f 100644 --- a/ui/components/users/forms/edit-form.tsx +++ b/ui/components/users/forms/edit-form.tsx @@ -25,11 +25,7 @@ export const EditForm = ({ userCompanyName?: string; setIsOpen: Dispatch>; }) => { - const formSchema = editUserFormSchema( - userName ?? "", - userEmail ?? "", - userCompanyName ?? "", - ); + const formSchema = editUserFormSchema(); const form = useForm>({ resolver: zodResolver(formSchema), @@ -48,16 +44,26 @@ export const EditForm = ({ const onSubmitClient = async (values: z.infer) => { const formData = new FormData(); - Object.entries(values).forEach( - ([key, value]) => value !== undefined && formData.append(key, value), - ); + // Check if the value is not undefined before appending to FormData + if (values.name !== undefined) { + formData.append("name", values.name); + } + if (values.email !== undefined) { + formData.append("email", values.email); + } + if (values.company_name !== undefined) { + formData.append("company_name", values.company_name); + } + + // Always include userId + formData.append("userId", userId); const data = await updateUser(formData); if (data?.errors && data.errors.length > 0) { const error = data.errors[0]; const errorMessage = `${error.detail}`; - // show error + // Show error toast({ variant: "destructive", title: "Oops! Something went wrong", diff --git a/ui/types/formSchemas.ts b/ui/types/formSchemas.ts index cc0221add7..0b503fe26b 100644 --- a/ui/types/formSchemas.ts +++ b/ui/types/formSchemas.ts @@ -160,36 +160,21 @@ export const editInviteFormSchema = z.object({ expires_at: z.string().optional(), }); -export const editUserFormSchema = ( - currentName: string, - currentEmail: string, - currentCompanyName: string, -) => +export const editUserFormSchema = () => z.object({ name: z .string() .min(3, { message: "The name must have at least 3 characters." }) .max(150, { message: "The name cannot exceed 150 characters." }) - .refine((val) => val !== currentName, { - message: "The new name must be different from the current one.", - }) .optional(), email: z .string() .email({ message: "Please enter a valid email address." }) - .refine((val) => val !== currentEmail, { - message: "The new email must be different from the current one.", - }) .optional(), password: z .string() .min(1, { message: "The password cannot be empty." }) .optional(), - company_name: z - .string() - .refine((val) => val !== currentCompanyName, { - message: "The new company name must be different from the current one.", - }) - .optional(), + company_name: z.string().optional(), userId: z.string(), });