Skip to content

Commit

Permalink
feat: update tag handling in SelectTags component to trigger onChange…
Browse files Browse the repository at this point in the history
… with updated tag data
  • Loading branch information
elisfainstein committed Jan 14, 2025
1 parent a1b42a8 commit 0d03e29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type SelectTagsProps = Omit<SelectMultipleProps, 'options' | 'onChange'> & {
values: Tag[];
selectedValue: Tag;
}) => void;
onTagEdit?: (editedTag: Tag) => void;
};

const SelectTags = ({
Expand All @@ -30,7 +29,6 @@ const SelectTags = ({
userCreatedOptionsIds,
disabledOptionsIds,
refetchOptions,
onTagEdit,
...props
}: SelectTagsProps) => {
const collectiviteId = useCollectiviteId();
Expand Down Expand Up @@ -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,
Expand All @@ -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
// ***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0d03e29

Please sign in to comment.