import { useDataSource } from "@erp/core/hooks";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { User } from "./types";

export const useUsers = () => {
  const client = useDataSource();

  return useQuery({
    queryKey: ["users"],
    queryFn: () => client.getList<User>("/users"),
  });
};

export const useCreateUser = () => {
  const client = useDataSource();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (user: Partial<User>) => client.createOne<User>("/users", user),
    onSuccess: () => queryClient.invalidateQueries({ queryKey: ["users"] }),
  });
};
