shoc-frontend-new/src/domain/accounts/use-cases/use-create-account.ts
2026-07-13 03:10:41 -03:00

24 lines
1,017 B
TypeScript

import { useMutation, useQueryClient, type UseMutationResult } from "@tanstack/react-query";
import { useNavigate } from "react-router";
import { toast } from "react-toastify";
import { accountsApi } from "@/domain/accounts/api/accounts-api";
import type { AccountFormValues } from "@/domain/accounts/schemas/account-schema";
import type { Account } from "@/domain/accounts/types/account";
import { queryKeys } from "@/infra/query-key/query-key";
export function useCreateAccount(): UseMutationResult<Account, Error, AccountFormValues> {
const navigate = useNavigate();
const queryClient = useQueryClient();
return useMutation({
mutationFn: (values: AccountFormValues) => accountsApi.create(values),
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: queryKeys.accounts.all });
toast.success("Account created successfully!");
navigate("/accounts");
},
onError: (error: Error) => {
toast.error(error.message || "Failed to create account");
},
});
}