add new endpoints, add tag types to base api

This commit is contained in:
2023-10-09 13:23:57 +02:00
parent 27da2dc299
commit 46aab2f67f
2 changed files with 41 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
export const baseApi = createApi({ export const baseApi = createApi({
reducerPath: 'baseApi', reducerPath: 'baseApi',
tagTypes: ['Decks'],
baseQuery: fetchBaseQuery({ baseQuery: fetchBaseQuery({
baseUrl: 'https://api.flashcards.andrii.es', baseUrl: 'https://api.flashcards.andrii.es',
credentials: 'include', credentials: 'include',

View File

@@ -1,4 +1,11 @@
import { CardsResponse, DeckResponse, DecksResponse, GetDecksArgs } from './decks.types' import {
CardsResponse,
CreateDeckArgs,
DeckResponse,
DecksResponse,
GetDecksArgs,
UpdateDeckArgs,
} from './decks.types'
import { baseApi } from '@/services' import { baseApi } from '@/services'
@@ -11,6 +18,7 @@ const decksService = baseApi.injectEndpoints({
params: args ?? undefined, params: args ?? undefined,
} }
}, },
providesTags: ['Decks'],
}), }),
getDeckById: builder.query<DeckResponse, { id: string }>({ getDeckById: builder.query<DeckResponse, { id: string }>({
query: ({ id }) => `v1/decks/${id}`, query: ({ id }) => `v1/decks/${id}`,
@@ -18,7 +26,37 @@ const decksService = baseApi.injectEndpoints({
getDeckCards: builder.query<CardsResponse, { id: string }>({ getDeckCards: builder.query<CardsResponse, { id: string }>({
query: ({ id }) => `v1/decks/${id}/cards`, query: ({ id }) => `v1/decks/${id}/cards`,
}), }),
createDeck: builder.mutation<DeckResponse, CreateDeckArgs>({
query: body => ({
url: `v1/decks`,
method: 'POST',
body,
}),
invalidatesTags: ['Decks'],
}),
deleteDeck: builder.mutation<void, { id: string }>({
query: ({ id }) => ({
url: `v1/decks/${id}`,
method: 'DELETE',
}),
invalidatesTags: ['Decks'],
}),
updateDeck: builder.mutation<DeckResponse, UpdateDeckArgs>({
query: ({ id, ...body }) => ({
url: `v1/decks/${id}`,
method: 'PATCH',
body,
}),
invalidatesTags: ['Decks'],
}),
}), }),
}) })
export const { useGetDecksQuery, useGetDeckByIdQuery, useGetDeckCardsQuery } = decksService export const {
useGetDecksQuery,
useGetDeckByIdQuery,
useGetDeckCardsQuery,
useCreateDeckMutation,
useDeleteDeckMutation,
useUpdateDeckMutation,
} = decksService