Loading...
-
{note.title}
+
{note.title}
{note.content}
-
+
diff --git a/src/store/api/NoteSlice.js b/src/store/api/NoteSlice.js
index 4927eea..abe1b0d 100644
--- a/src/store/api/NoteSlice.js
+++ b/src/store/api/NoteSlice.js
@@ -1,62 +1,50 @@
-import {createSlice, createAsyncThunk} from "@reduxjs/toolkit";
-import axios from "axios";
-
-const initialState = {
- notes: [],
- status: "idle",
- error: null
-};
-
-export const fetchNotes = createAsyncThunk("note/fetchNotes", async () => {
- const response = await axios.get("http://localhost:9000/notes");
- return response.data;
-});
-
-export const addNote = createAsyncThunk("note/addNote", async (newNote) => {
- const response = await axios.post("http://localhost:9000/create_note", newNote);
- return response.data;
-});
-
-export const editNote = createAsyncThunk("note/editNote", async ({noteId, updateNote}) => {
- const response = await axios.put(`http://localhost:9000/update_note/${noteId}`, updateNote);
- return response.data;
-});
-
-export const deleteNote = createAsyncThunk("note/deleteNote", async (noteId) => {
- await axios.delete(`http://localhost:9000/delete_note/${noteId}`);
- return noteId;
-});
-
-export const noteSlice = createSlice({
- name: "notes",
- initialState,
- reducers: {},
- extraReducers: (builder) => {
- builder.addCase(fetchNotes.pending, (state) => {
- state.status = "loading";
- state.error = null;
- }).addCase(fetchNotes.fulfilled, (state, action) => {
- state.status = "succeeded";
- state.notes = action.payload;
- }).addCase(fetchNotes.rejected, (state, action) => {
- state.status = "failed";
- state.error = action.error.message;
- }).addCase(addNote.fulfilled, (state, action) => {
- state.notes.push(action.payload);
- }).addCase(editNote.fulfilled, (state, action) => {
- const {id, title, content} = action.payload;
- const existingNote = state.notes.find((note) => Number(note.id) === Number(id));
- if (existingNote) {
- existingNote.title = title;
- existingNote.content = content;
+import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
+// const Base_Url = 'http://localhost:9000';
+export const NoteSlice = createApi({
+ reducerPath: "notesApi",
+ baseQuery: fetchBaseQuery({
+ baseUrl : 'http://localhost:9002'
+ }),
+
+ tagTypes : ["notesApi"],
+
+ endpoints: (builder) => ({
+ fetchNotes: builder.query({
+ query: () => {
+ return {
+ url: "notes",
+ method: "GET"
+ }
}
-
- }).addCase(deleteNote.fulfilled, (state, action) => {
- const noteId = action.payload;
- state.notes = state.notes.filter((note) => note.id !== noteId);
- });
- }
-});
-
-
-export default noteSlice.reducer;
+ ,
+ providesTags : ["notesApi"]
+ })
+ ,
+ addNotes: builder.mutation({
+ query : (newBook) => ({
+ url : 'create_note',
+ method : 'POST',
+ body : newBook
+ }),
+ invalidatesTags : ["notesApi"]
+ })
+ ,
+ deleteNotes: builder.mutation({
+ query : (id)=>({
+ url : `delete_note/${id}`,
+ method : 'DELETE',
+ }),
+ invalidatesTags : ["notesApi"]
+ }),
+ updateNotes: builder.mutation({
+ query : ({id , updateNote}) => ({
+ url : `update_note/${id}`,
+ method : 'PUT',
+ body : updateNote
+ }),
+ invalidatesTags : ["notesApi"]
+ })
+ })
+})
+// export { useFetchNotesQuery} from NoteSlice;
+export const { useFetchNotesQuery , useAddNotesMutation , useDeleteNotesMutation , useUpdateNotesMutation} = NoteSlice;
\ No newline at end of file
diff --git a/src/store/index.js b/src/store/index.js
index 3cebbe7..c1f59ce 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -1,9 +1,13 @@
import { configureStore } from '@reduxjs/toolkit'
-import NoteReducer from './api/NoteSlice'
-
+import { NoteSlice } from "./api/NoteSlice"
+import { setupListeners } from '@reduxjs/toolkit/dist/query'
export const store = configureStore({
reducer: {
- notes: NoteReducer
+ [NoteSlice.reducerPath] : NoteSlice.reducer
}
-})
\ No newline at end of file
+ ,
+ middleware : (getDefaultMiddleware)=>
+ getDefaultMiddleware().concat(NoteSlice.middleware)
+})
+setupListeners(store.dispatch);
\ No newline at end of file