mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-25 10:51:26 +01:00
Updating environments!
This commit is contained in:
29
src-web/hooks/useUpdateEnvironment.ts
Normal file
29
src-web/hooks/useUpdateEnvironment.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import type { Environment } from '../lib/models';
|
||||
import { getEnvironment } from '../lib/store';
|
||||
import { environmentsQueryKey } from './useEnvironments';
|
||||
|
||||
export function useUpdateEnvironment(id: string | null) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<void, unknown, Partial<Environment> | ((r: Environment) => Environment)>({
|
||||
mutationFn: async (v) => {
|
||||
const environment = await getEnvironment(id);
|
||||
if (environment == null) {
|
||||
throw new Error("Can't update a null environment");
|
||||
}
|
||||
|
||||
const newEnvironment = typeof v === 'function' ? v(environment) : { ...environment, ...v };
|
||||
await invoke('update_environment', { environment: newEnvironment });
|
||||
},
|
||||
onMutate: async (v) => {
|
||||
const environment = await getEnvironment(id);
|
||||
if (environment === null) return;
|
||||
|
||||
const newEnvironment = typeof v === 'function' ? v(environment) : { ...environment, ...v };
|
||||
queryClient.setQueryData<Environment[]>(environmentsQueryKey(environment), (environments) =>
|
||||
(environments ?? []).map((r) => (r.id === newEnvironment.id ? newEnvironment : r)),
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user