Skip to content

What will happen in this case? #3

Description

@hozaifa4you

Full Code

import { Product } from "@prisma/client";
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const productsApiSlice = createApi({
   reducerPath: "productsApi",
   baseQuery: fetchBaseQuery({ baseUrl: "/api/products" }),
   tagTypes: ["Products"],
   endpoints: (builder) => ({
      getProducts: builder.query<Product[], void>({
         query: () => "/",
         providesTags: (result) =>
            result
               ? [
                    ...result.map(({ id }) => ({
                       type: "Products" as const,
                       id: id,
                    })),
                    { type: "Products", id: "LIST" },
                 ]
               : [{ type: "Products", id: "LIST" }],
      }),
      getProductById: builder.query<Product, number>({
         query: (id) => `products/${id}`,
      }),
      addProduct: builder.mutation<Product, Partial<Product>>({
         query: (product) => ({
            url: "products",
            method: "POST",
            body: product,
         }),
      }),
      updateProduct: builder.mutation<Product, Partial<Product>>({
         query: ({ id, ...patch }) => ({
            url: `products/${id}`,
            method: "PATCH",
            body: patch,
         }),
      }),
      deleteProduct: builder.mutation<void, number>({
         query: (id) => ({
            url: `/${id}`,
            method: "DELETE",
            invalidatesTags: ["Products", { type: "Products", id: id }],
         }),
      }),
   }),
});

export const {
   useGetProductsQuery,
   useGetProductByIdQuery,
   useAddProductMutation,
   useUpdateProductMutation,
   useDeleteProductMutation,
} = productsApiSlice;

Case

      getProducts: builder.query<Product[], void>({
         query: () => "/",
         providesTags: (result) =>
            result
               ? [
                    ...result.map(({ id }) => ({
                       type: "Products" as const,
                       id: id,
                    })),
                    { type: "Products", id: "LIST" },
                 ]
               : [{ type: "Products", id: "LIST" }],
      }),
      ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions