From 0d03e293bf8e30966b4c88037398431f3691da5a Mon Sep 17 00:00:00 2001 From: Elisabeth Fainstein <139369258+elisfainstein@users.noreply.github.com> Date: Tue, 14 Jan 2025 12:38:23 +0100 Subject: [PATCH] feat: update tag handling in SelectTags component to trigger onChange with updated tag data --- .../src/ui/dropdownLists/tags/SelectTags.tsx | 25 +++++++++++++++---- .../src/ui/dropdownLists/tags/useTagUpdate.ts | 12 ++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app.territoiresentransitions.react/src/ui/dropdownLists/tags/SelectTags.tsx b/app.territoiresentransitions.react/src/ui/dropdownLists/tags/SelectTags.tsx index 4cf0160951..1648417a10 100644 --- a/app.territoiresentransitions.react/src/ui/dropdownLists/tags/SelectTags.tsx +++ b/app.territoiresentransitions.react/src/ui/dropdownLists/tags/SelectTags.tsx @@ -20,7 +20,6 @@ type SelectTagsProps = Omit & { values: Tag[]; selectedValue: Tag; }) => void; - onTagEdit?: (editedTag: Tag) => void; }; const SelectTags = ({ @@ -30,7 +29,6 @@ const SelectTags = ({ userCreatedOptionsIds, disabledOptionsIds, refetchOptions, - onTagEdit, ...props }: SelectTagsProps) => { const collectiviteId = useCollectiviteId(); @@ -88,7 +86,7 @@ const SelectTags = ({ // Mise à jour d'un tag de la liste d'options // *** - const { mutate: updateTag } = useTagUpdate({ + const { data: updatedTag, mutate: updateTag } = useTagUpdate({ key: [queryKey, collectiviteId], tagTableName, onSuccess: refetchOptions, @@ -100,11 +98,28 @@ const SelectTags = ({ id: parseInt(tagId as string), nom: tagName, }; - updateTag(editedTag); - onTagEdit?.(editedTag); }; + useEffect(() => { + if (updatedTag) { + const tag = { + collectiviteId: collectiviteId!, + nom: updatedTag.nom, + id: updatedTag.id, + }; + + const otherTags = getSelectedValues(props.values).filter( + (t) => t.id !== tag.id + ); + + props.onChange({ + values: [tag, ...otherTags], + selectedValue: tag, + }); + } + }, [updatedTag]); + // *** // Suppression d'un tag de la liste d'options // *** diff --git a/app.territoiresentransitions.react/src/ui/dropdownLists/tags/useTagUpdate.ts b/app.territoiresentransitions.react/src/ui/dropdownLists/tags/useTagUpdate.ts index 3f4b40a70f..29705303d2 100644 --- a/app.territoiresentransitions.react/src/ui/dropdownLists/tags/useTagUpdate.ts +++ b/app.territoiresentransitions.react/src/ui/dropdownLists/tags/useTagUpdate.ts @@ -21,11 +21,17 @@ export const useTagUpdate = ({ return useMutation( async (tag: TagUpdate) => { - if (tag.id) - await supabaseClient + if (tag.id) { + const { data, error } = await supabaseClient .from(tagTableName) .update(objectToSnake(tag)) - .eq('id', tag.id); + .eq('id', tag.id) + .select() + .single(); + + if (error) throw error; + return data; + } }, { mutationKey: 'update_tag',