mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-21 00:49:17 +01:00
Nested sidebar ordering almost working
This commit is contained in:
28
src-web/hooks/useUpdateAnyFolder.ts
Normal file
28
src-web/hooks/useUpdateAnyFolder.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import type { Folder, HttpRequest } from '../lib/models';
|
||||
import { getFolder, getRequest } from '../lib/store';
|
||||
import { requestsQueryKey } from './useRequests';
|
||||
import { foldersQueryKey } from './useFolders';
|
||||
|
||||
export function useUpdateAnyFolder() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation<void, unknown, { id: string; update: (r: Folder) => Folder }>({
|
||||
mutationFn: async ({ id, update }) => {
|
||||
const folder = await getFolder(id);
|
||||
if (folder === null) {
|
||||
throw new Error("Can't update a null folder");
|
||||
}
|
||||
|
||||
await invoke('update_folder', { request: update(folder) });
|
||||
},
|
||||
onMutate: async ({ id, update }) => {
|
||||
const folder = await getFolder(id);
|
||||
if (folder === null) return;
|
||||
queryClient.setQueryData<Folder[]>(foldersQueryKey(folder), (folders) =>
|
||||
(folders ?? []).map((f) => (f.id === folder.id ? update(f) : f)),
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user