44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
// ============================================================
|
|
// hooks تأمینکنندهها و دستهها
|
|
// ============================================================
|
|
|
|
import { useMutation } from "@tanstack/react-query";
|
|
import type { Supplier } from "../lib/types";
|
|
import { api } from "../lib/api";
|
|
import { useAppStore } from "../store";
|
|
import { useInvalidateStore } from "./useStoreData";
|
|
|
|
export function useSuppliers(): Supplier[] {
|
|
return useAppStore((s) => s.suppliers);
|
|
}
|
|
|
|
export function useAddSupplier() {
|
|
const addSupplier = useAppStore((s) => s.addSupplier);
|
|
const invalidate = useInvalidateStore();
|
|
return useMutation({
|
|
mutationFn: (s: Supplier) => api.suppliers.create(s),
|
|
onMutate: (s) => addSupplier(s),
|
|
onSettled: () => invalidate(),
|
|
});
|
|
}
|
|
|
|
export function useUpdateSupplier() {
|
|
const updateSupplier = useAppStore((s) => s.updateSupplier);
|
|
const invalidate = useInvalidateStore();
|
|
return useMutation({
|
|
mutationFn: (args: { id: string; patch: Partial<Supplier> }) => api.suppliers.update(args.id, args.patch),
|
|
onMutate: (args) => updateSupplier(args.id, args.patch),
|
|
onSettled: () => invalidate(),
|
|
});
|
|
}
|
|
|
|
export function useRemoveSupplier() {
|
|
const deleteSupplier = useAppStore((s) => s.deleteSupplier);
|
|
const invalidate = useInvalidateStore();
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.suppliers.delete(id),
|
|
onMutate: (id) => deleteSupplier(id),
|
|
onSettled: () => invalidate(),
|
|
});
|
|
}
|