diff --git a/src/frontend/apps/impress/src/features/pads/pad/api/useUpdatePad.tsx b/src/frontend/apps/impress/src/features/pads/pad/api/useUpdatePad.tsx new file mode 100644 index 00000000..40212921 --- /dev/null +++ b/src/frontend/apps/impress/src/features/pads/pad/api/useUpdatePad.tsx @@ -0,0 +1,45 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; + +import { APIError, errorCauses, fetchAPI } from '@/api'; +import { Pad } from '@/features/pads'; + +export type PadParams = Pick & + Partial>; + +export const updatePad = async ({ id, ...params }: PadParams): Promise => { + const response = await fetchAPI(`documents/${id}/`, { + method: 'PATCH', + body: JSON.stringify({ + ...params, + }), + }); + + if (!response.ok) { + throw new APIError('Failed to update the pad', await errorCauses(response)); + } + + return response.json() as Promise; +}; + +interface UpdatePadProps { + onSuccess?: (data: Pad) => void; + listInvalideQueries?: string[]; +} + +export function useUpdatePad({ + onSuccess, + listInvalideQueries, +}: UpdatePadProps = {}) { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: updatePad, + onSuccess: (data) => { + listInvalideQueries?.forEach((queryKey) => { + void queryClient.invalidateQueries({ + queryKey: [queryKey], + }); + }); + onSuccess?.(data); + }, + }); +}