-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ lerna-debug.log* | |
node_modules | ||
dist | ||
dist-ssr | ||
.env | ||
*.local | ||
|
||
# Editor directories and files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; | ||
import getBaseUrl from "../../../utils/baseURL"; | ||
|
||
const baseQuery = fetchBaseQuery({ | ||
baseUrl: `${getBaseUrl()}/api/books`, | ||
credentials: "include", | ||
prepareHeaders: (Headers) => { | ||
const token = localStorage.getItem("token"); | ||
if (token) { | ||
Headers.set("Authorization", `Bearer ${token}`); | ||
} | ||
return Headers; | ||
}, | ||
}); | ||
|
||
const booksApi = createApi({ | ||
reducerPath: "booksApi", | ||
baseQuery, | ||
tagTypes: ["Books"], | ||
endpoints: (builder) => ({ | ||
fetchAllBooks: builder.query({ | ||
query: () => "/", | ||
providesTags: ["Books"], | ||
}), | ||
fetchBookById: builder.query({ | ||
query: (id) => `/${id}`, | ||
providesTags: (result, error, id) => [{ type: "Books", id }], | ||
}), | ||
addBook: builder.mutation({ | ||
query: (newBook) => ({ | ||
url: `/create-book`, | ||
method: "POST", | ||
body: newBook, | ||
}), | ||
invalidatesTags: ["Books"], | ||
}), | ||
updateBook: builder.mutation({ | ||
query: ({ id, ...rest }) => ({ | ||
url: `/edit/${id}`, | ||
method: "PUT", | ||
body: rest, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}), | ||
invalidatesTags: ["Books"], | ||
}), | ||
deleteBook: builder.mutation({ | ||
query: (id) => ({ | ||
url: `/${id}`, | ||
method: "DELETE", | ||
}), | ||
invalidatesTags: ["Books"], | ||
}), | ||
}), | ||
}); | ||
|
||
export const { | ||
useFetchAllBooksQuery, | ||
useFetchBookByIdQuery, | ||
useAddBookMutation, | ||
useUpdateBookMutation, | ||
useDeleteBookMutation, | ||
} = booksApi; | ||
export default booksApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { configureStore } from "@reduxjs/toolkit"; | ||
import cartReducer from "./features/cart/cartSlice"; | ||
import booksApi from "./features/books/booksApi"; | ||
import ordersApi from "./features/orders/ordersApi"; | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
cart: cartReducer, | ||
[booksApi.reducerPath]: booksApi.reducer, | ||
[ordersApi.reducerPath]: ordersApi.reducer, | ||
}, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware().concat(booksApi.middleware, ordersApi.middleware), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getBaseUrl = () => { | ||
return "http://localhost:5000"; | ||
}; | ||
|
||
export default getBaseUrl; |