Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(users): user detail can be edited now properly #6135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions ui/actions/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> = {};

// 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",
Expand All @@ -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);
Expand Down
24 changes: 15 additions & 9 deletions ui/components/users/forms/edit-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export const EditForm = ({
userCompanyName?: string;
setIsOpen: Dispatch<SetStateAction<boolean>>;
}) => {
const formSchema = editUserFormSchema(
userName ?? "",
userEmail ?? "",
userCompanyName ?? "",
);
const formSchema = editUserFormSchema();

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
Expand All @@ -48,16 +44,26 @@ export const EditForm = ({
const onSubmitClient = async (values: z.infer<typeof formSchema>) => {
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",
Expand Down
19 changes: 2 additions & 17 deletions ui/types/formSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Loading