34 lines
961 B
TypeScript
Raw Normal View History

2024-11-12 17:17:10 +00:00
import api from "@/lib/api";
2024-11-09 18:57:36 +00:00
import { useMutation, useQuery } from "@tanstack/react-query";
import { FormSchema } from "../schema/form";
2024-11-12 17:17:10 +00:00
import queryClient from "@/lib/queryClient";
import { useTeamId } from "@/stores/auth";
export const useKeychains = (params?: any) => {
const teamId = useTeamId();
const query = { teamId, ...params };
2024-11-09 18:57:36 +00:00
return useQuery({
queryKey: ["keychains", query],
queryFn: () => api("/keychains", { query }),
select: (i) => i.rows,
});
};
export const useSaveKeychain = () => {
2024-11-12 17:17:10 +00:00
const teamId = useTeamId();
2024-11-09 18:57:36 +00:00
return useMutation({
2024-11-12 17:17:10 +00:00
mutationFn: async (payload: FormSchema) => {
const body = { teamId, ...payload };
2024-11-09 18:57:36 +00:00
return body.id
? api(`/keychains/${body.id}`, { method: "PUT", body })
: api(`/keychains`, { method: "POST", body });
},
onError: (e) => console.error(e),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["keychains"] });
},
});
};