Component evidence

Create Order Mutation Hook (orders/useCreateOrder.ts)

BookWorm · src/Clients/packages/api-hooks/src/ordering/orders/useCreateOrder.ts · typescript · MIT
pospaymentsfw:tanstack-queryfw:tanstacktag:mutation

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 preview public preview
// 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,
  });
}