diff --git a/ui/actions/invitations/invitation.ts b/ui/actions/invitations/invitation.ts index c20af1b80f..7c37ca7ad7 100644 --- a/ui/actions/invitations/invitation.ts +++ b/ui/actions/invitations/invitation.ts @@ -110,27 +110,33 @@ export const updateInvite = async (formData: FormData) => { const url = new URL(`${keyServer}/tenants/invitations/${invitationId}`); - const body = { + const body: any = { data: { type: "invitations", id: invitationId, - attributes: { - email: invitationEmail, - expires_at: expiresAt, - }, - relationships: { - roles: { - data: [ - { - id: roleId, - type: "role", - }, - ], - }, - }, + attributes: {}, + relationships: {}, }, }; + // Only add attributes that exist in the formData + if (invitationEmail) { + body.data.attributes.email = invitationEmail; + } + if (expiresAt) { + body.data.attributes.expires_at = expiresAt; + } + if (roleId) { + body.data.relationships.roles = { + data: [ + { + id: roleId, + type: "role", + }, + ], + }; + } + try { const response = await fetch(url.toString(), { method: "PATCH", diff --git a/ui/components/invitations/forms/edit-form.tsx b/ui/components/invitations/forms/edit-form.tsx index c2313905a8..293f5288e5 100644 --- a/ui/components/invitations/forms/edit-form.tsx +++ b/ui/components/invitations/forms/edit-form.tsx @@ -1,5 +1,6 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { Select, SelectItem } from "@nextui-org/react"; +import { MailIcon, ShieldIcon } from "lucide-react"; import { Dispatch, SetStateAction } from "react"; import { Controller, useForm } from "react-hook-form"; import * as z from "zod"; @@ -40,11 +41,35 @@ export const EditForm = ({ const isLoading = form.formState.isSubmitting; const onSubmitClient = async (values: z.infer) => { - const formData = new FormData(); + const changedFields: { [key: string]: any } = {}; - Object.entries(values).forEach( - ([key, value]) => value !== undefined && formData.append(key, value), - ); + // Check if the email changed + if (values.invitationEmail && values.invitationEmail !== invitationEmail) { + changedFields.invitationEmail = values.invitationEmail; + } + + // Check if the role changed + const currentRoleId = + roles.find((role) => role.name === currentRole)?.id || ""; + if (values.role && values.role !== currentRoleId) { + changedFields.role = values.role; + } + + // If there are no changes, avoid the request + if (Object.keys(changedFields).length === 0) { + toast({ + title: "No changes detected", + description: "Please modify at least one field before saving.", + }); + return; + } + + changedFields.invitationId = invitationId; // Always include the ID + + const formData = new FormData(); + Object.entries(changedFields).forEach(([key, value]) => { + formData.append(key, value); + }); const data = await updateInvite(formData); @@ -69,12 +94,23 @@ export const EditForm = ({ onSubmit={form.handleSubmit(onSubmitClient)} className="flex flex-col space-y-4" > -
- Current email: {invitationEmail} -
-
- Current role: {currentRole} +
+
+ + Email: + + {invitationEmail} + +
+
+ + Role: + + {currentRole} + +
+
({