From 531c436151debe4a9d50694c9aae20b85868587c Mon Sep 17 00:00:00 2001 From: nadavosa Date: Mon, 27 Apr 2026 18:30:01 +0200 Subject: [PATCH] fix: correct agent profile save mutations and cache invalidation (#392, #354) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - useUpdateAgentContact: invalidated ["agent", personId] instead of the agent entity ID, so the profile cache never refreshed after saving contact details. Now accepts agentId separately and invalidates ["agent", agentId]. - useUpdateOrganization: was hitting /organization/:operator where operator is a text name string (not a numeric ID), so every save was a 404. Also used DeepPartial (a person type) as the request body and had a double-underscore typo in the success message key. Fixed to hit /agent/:agentId (which accepts about/website) with the correct ApiAgentPatch type and proper cache invalidation. The org-level address field still needs the organization's numeric ID exposed in the API response — tracked separately. Co-Authored-By: Claude Sonnet 4.6 --- public/locales/de/translations.json | 1 + public/locales/en/translations.json | 1 + .../agent/AgentContactDetails.tsx | 2 +- .../OrganisationDetails.tsx | 15 +++++++++------ src/hooks/useUpdateAgentContact.ts | 4 ++-- src/hooks/useUpdateOrganizationDetails.ts | 18 ++++++++++-------- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/public/locales/de/translations.json b/public/locales/de/translations.json index 22136abf..13435b62 100644 --- a/public/locales/de/translations.json +++ b/public/locales/de/translations.json @@ -954,6 +954,7 @@ "clientLanguages": "Sprachen der Klient:innen", "cancel": "Abbrechen", "saveChanges": "Änderungen speichern", + "saveSuccess": "Organisationsdetails erfolgreich aktualisiert", "validation": { "required": "Dieses Feld ist erforderlich", "websiteInvalid": "Webseite muss mit http:// oder https:// beginnen", diff --git a/public/locales/en/translations.json b/public/locales/en/translations.json index fed0d78f..3675044a 100644 --- a/public/locales/en/translations.json +++ b/public/locales/en/translations.json @@ -953,6 +953,7 @@ "clientLanguages": "Client languages", "cancel": "Cancel", "saveChanges": "Save changes", + "saveSuccess": "Organisation details updated successfully", "validation": { "required": "This field is required", "websiteInvalid": "Website must start with http:// or https://", diff --git a/src/components/Dashboard/Profile/sections/ContactDetails/agent/AgentContactDetails.tsx b/src/components/Dashboard/Profile/sections/ContactDetails/agent/AgentContactDetails.tsx index 00a58ada..ec72e2e4 100644 --- a/src/components/Dashboard/Profile/sections/ContactDetails/agent/AgentContactDetails.tsx +++ b/src/components/Dashboard/Profile/sections/ContactDetails/agent/AgentContactDetails.tsx @@ -30,7 +30,7 @@ export const AgentContactDetails = forwardRef(function ref, ) { const { t } = useTranslation(); - const { mutate: updateAgent, isPending } = useUpdateAgentContact(String(agent?.representative?.id)); + const { mutate: updateAgent, isPending } = useUpdateAgentContact(String(agent?.representative?.id), String(agent?.id)); const [isEditing, setIsEditing] = useState(false); useEditingChangeNotifier(isEditing, onEditingChange); diff --git a/src/components/Dashboard/Profile/sections/OrganisationDetails/OrganisationDetails.tsx b/src/components/Dashboard/Profile/sections/OrganisationDetails/OrganisationDetails.tsx index def55a28..3148bff2 100644 --- a/src/components/Dashboard/Profile/sections/OrganisationDetails/OrganisationDetails.tsx +++ b/src/components/Dashboard/Profile/sections/OrganisationDetails/OrganisationDetails.tsx @@ -24,7 +24,7 @@ export const OrganisationDetails = forwardRef(functio ) { const { t, i18n } = useTranslation(); const [isEditing, setIsEditing] = useState(false); - const { mutate: updateOrganization /*, isPending */ } = useUpdateOrganization(String(agent?.operator)); + const { mutate: updateOrganization /*, isPending */ } = useUpdateOrganization(String(agent?.id)); useEditingChangeNotifier(isEditing, onEditingChange); const { data: apiLanguages } = useApiLanguages(); @@ -56,12 +56,15 @@ export const OrganisationDetails = forwardRef(functio }; const onSubmit = (values: OrganisationDetailsFormData) => { - updateOrganization(values as unknown as ApiAgentProfileGet, { - onSuccess: () => { - reset(values); - setIsEditing(false); + updateOrganization( + { about: values.about, website: values.website }, + { + onSuccess: () => { + reset(values); + setIsEditing(false); + }, }, - }); + ); }; return ( diff --git a/src/hooks/useUpdateAgentContact.ts b/src/hooks/useUpdateAgentContact.ts index 657932ab..e638f8b8 100644 --- a/src/hooks/useUpdateAgentContact.ts +++ b/src/hooks/useUpdateAgentContact.ts @@ -4,11 +4,11 @@ import { useMutationQuery } from "@/hooks"; import { ApiRepresentativeGet } from "need4deed-sdk"; import { DeepPartial } from "ts-type-safe"; -export const useUpdateAgentContact = (personId: string) => { +export const useUpdateAgentContact = (personId: string, agentId: string) => { return useMutationQuery, ApiAgentProfileGet>({ apiPath: `${apiPathPerson}${personId}`, method: "patch", successMessage: "dashboard.agentProfile.contactDetails.saveSuccess", - queryKeyToInvalidate: ["agent", personId], + queryKeyToInvalidate: ["agent", agentId], }); }; diff --git a/src/hooks/useUpdateOrganizationDetails.ts b/src/hooks/useUpdateOrganizationDetails.ts index ca845c95..e66f20bc 100644 --- a/src/hooks/useUpdateOrganizationDetails.ts +++ b/src/hooks/useUpdateOrganizationDetails.ts @@ -1,14 +1,16 @@ import { ApiAgentProfileGet } from "@/components/Dashboard/Profile/types"; -import { apiPathOrganization } from "@/config/constants"; +import { apiPathAgent } from "@/config/constants"; import { useMutationQuery } from "@/hooks"; -import { ApiRepresentativeGet } from "need4deed-sdk"; -import { DeepPartial } from "ts-type-safe"; +import { ApiAgentPatch } from "need4deed-sdk"; -export const useUpdateOrganization = (organizationId: string) => { - return useMutationQuery, ApiAgentProfileGet>({ - apiPath: `${apiPathOrganization}${organizationId}`, +// Patches agent-level org details (about, website) via the agent endpoint. +// Note: address and district require the organization's numeric ID which is not +// currently exposed in the agent API response — those fields need a BE change. +export const useUpdateOrganization = (agentId: string) => { + return useMutationQuery, ApiAgentProfileGet>({ + apiPath: `${apiPathAgent}/${agentId}`, method: "patch", - successMessage: "__dashboard.agentProfile.contactDetails.saveSuccess", - queryKeyToInvalidate: ["organization", organizationId], + successMessage: "dashboard.agentProfile.organisationDetails.saveSuccess", + queryKeyToInvalidate: ["agent", agentId], }); };