mirror of
https://github.com/suitenumerique/meet.git
synced 2026-08-14 20:53:26 +00:00
22a1713c60
Rapid toggles could persist a stale configuration: each PATCH replaces the full room config, and every call site built it from a render-time snapshot. A toggle issued before the previous one resolved therefore overwrote the newer value with an older one. Handle the cache centrally in usePatchRoom so the next toggle always reads an up-to-date configuration.
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { type ApiRoom } from './ApiRoom'
|
|
import { fetchApi } from '@/api/fetchApi'
|
|
import { useMutation, type UseMutationOptions } from '@tanstack/react-query'
|
|
import type { ApiError } from '@/api/ApiError'
|
|
import { queryClient } from '@/api/queryClient'
|
|
import { keys } from '@/api/queryKeys'
|
|
|
|
export type PatchRoomParams = {
|
|
roomId: string
|
|
room: Partial<Pick<ApiRoom, 'configuration' | 'access_level'>>
|
|
}
|
|
|
|
export const patchRoom = ({ roomId, room }: PatchRoomParams) => {
|
|
return fetchApi<ApiRoom>(`/rooms/${roomId}/`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(room),
|
|
})
|
|
}
|
|
|
|
export const patchRoomMutationKey = ['patchRoom']
|
|
|
|
export function usePatchRoom(
|
|
options?: UseMutationOptions<ApiRoom, ApiError, PatchRoomParams>
|
|
) {
|
|
return useMutation<ApiRoom, ApiError, PatchRoomParams>({
|
|
mutationKey: patchRoomMutationKey,
|
|
mutationFn: patchRoom,
|
|
onMutate: async ({ roomId, room: partialRoom }) => {
|
|
await queryClient.cancelQueries({ queryKey: [keys.room, roomId] })
|
|
queryClient.setQueryData<ApiRoom>([keys.room, roomId], (previous) =>
|
|
previous ? { ...previous, ...partialRoom } : previous
|
|
)
|
|
},
|
|
onSettled: (_data, _error, { roomId }) => {
|
|
if (queryClient.isMutating({ mutationKey: patchRoomMutationKey }) === 1) {
|
|
queryClient.invalidateQueries({ queryKey: [keys.room, roomId] })
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|