score:0

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { TODOS_URL } from '../config';

export const todosApi = createApi({
  reducerPath: 'todos',
  tagTypes: ['TODO'],
  baseQuery: fetchBaseQuery({ baseUrl: TODOS_URL }),
  endpoints: (builder) => ({
    getTodos: builder.query({
      query: (list_id = '') => '?list_id=' + list_id,
      // providesTags: (result, error) => {
      //   if (error || !result || !result.data) {
      //     return [{ type: 'TODO' }];
      //   }
      //   return result.data.map((ele) => ({
      //     type: 'TODO',
      //     id: ele.id,
      //   }));
      // },
      providesTags: (result) =>
        result
          ? [...result.data.map(({ id }) => ({ type: 'TODO', id })), 'TODO']
          : ['TODO'],
    }),
    deleteTodo: builder.mutation({
      query: (id) => ({
        url: '/' + id,
        method: 'DELETE',
      }),
      invalidatesTags: ['TODO'], // (result, error, id) =>  {type:'TODO', id:id})
    }),
    addTodo: builder.mutation({
      query: (todo) => ({
        url: '',
        method: 'POST',
        body: todo,
      }),
      invalidatesTags: ['TODO'],
    }),
    updateTodo: builder.mutation({
      query: ({ id, ...body }) => ({
        url: '/' + id,
        method: 'PATCH',
        body,
      }),
      invalidatesTags: ['TODO'],
    }),
  }),
});

export const {
  useAddTodoMutation,
  useUpdateTodoMutation,
  useGetTodosQuery,
  useDeleteTodoMutation,
} = todosApi;

Related Query

More Query from same tag