app/apiHandler/localCenterApiSlice.js

/**
 * @file
 * File : localCenterApiSlice.js\
 * Defines API endpoints for managing local centers using RTK Query.
 * Extends the base API slice with related endpoints.
 *
 * @author  Pierre-Yves Léglise <contact@axialdata.net>
 * @name localCenterApiSlice
 */
import { apiSlice } from './apiSlice'

export const localCenterApiSlice = apiSlice.injectEndpoints({
  /**
   * Injects additional endpoints related to centers into the base API slice.
   * Defines endpoints for querying orders based on order groups.
   *
   * @category ApiSlice
   * @function
   * @author  Pierre-Yves Léglise <contact@axialdata.net>
   * @param {Object} builder - RTK Query endpoints builder.
   * @example
   *  const { data, isLoading, isFetching } = useGetOrdersListQuery({
   *    ordersGroupNb: selectedOrdersGroupNb,
   *  })
   * @returns {Object} Object containing order-related endpoint definitions.
   */
  endpoints: (builder) => ({
    /**
     * Endpoint for retrieving a list of orders within a specific orders group.
     * Sends a GET request to the `/ordersgroups/{ordersGroupNb}/orders` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name listCenters
     * @param {Object} params - Query parameters for the request.
     * @param {string} params.ordersGroupNb - The identifier of the orders group.
     * @param {Object} [params.params] - Additional query parameters for filtering or pagination.
     * @example
     *  const { data, isLoading, isFetching } = useGetOrdersListQuery({
     *    ordersGroupNb: selectedOrdersGroupNb,
     *  })
     * @returns {Object} Result of the API request.
     */
    listCenters: builder.query({
      query: (params) => ({
        url: `/centers`,
        method: 'get',
        withCredentials: true,
        params: { ...params },
      }),
    }),

    updateCenter: builder.mutation({
      query: ({ id, data }) => ({
        url: `/center/${id}`,
        method: 'patch',
        withCredentials: true,
        data: { ...data },
      }),
      keepUnusedDataFor: 0,
    }),

    createCenter: builder.mutation({
      query: ({ data }) => ({
        url: `/center`,
        method: 'put',
        withCredentials: true,
        data: { ...data },
      }),
      keepUnusedDataFor: 0,
    }),

    deleteCenter: builder.mutation({
      query: ({ id }) => ({
        url: `/center/${id}`,
        method: 'delete',
        withCredentials: true,
      }),
      keepUnusedDataFor: 0,
    }),
  }),
})

/**
 * Hooks for interacting with the orders-related endpoints.
 *
 * @typedef {Object} Hooks
 * @author  Pierre-Yves Léglise <contact@axialdata.net>
 * @property {Function} useGetOrdersListQuery - Hook to trigger the getOrdersList query.
 * @example
 *  const { data, isLoading, isFetching } = useGetOrdersListQuery({
 *    ordersGroupNb: selectedOrdersGroupNb,
 *  })
 * @returns {Object} Object containing order-related endpoint hooks.
 */
export const {
  useListCentersQuery,
  useUpdateCenterMutation,
  useDeleteCenterMutation,
  useCreateCenterMutation,
} = localCenterApiSlice