mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 16:19:01 +00:00
24 lines
1,017 B
TypeScript
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");
|
|
},
|
|
});
|
|
}
|