Component evidence
React Query mutation that creates an order from a basket id and invalidates the orders list cache on success. Reuse it when checkout submits a basket as a new order.
// Source: https://github.com/foxminchan/BookWorm | License: MIT | Copyright Nhan Nguyen (nguyenxuannhan407@gmail.com)
// Provided via justclaw.it.com; keep this notice with the code.
import {
type UseMutationOptions,
useMutation,
useQueryClient,
} from "@tanstack/react-query";
import ordersApiClient from "@workspace/api-client/ordering/orders";
import { orderingKeys } from "../../keys";
export default function useCreateOrder(
options?: UseMutationOptions<string, Error, string>,
) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (basketId) => ordersApiClient.create(basketId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: orderingKeys.orders.lists() });
},
...options,
});
}