Refactored to use thunk for Axios request
This commit is contained in:
1 parent
fbe357091a
commit
9926f60c09
2 files changed
+35
-18
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
import { createSlice } from "@reduxjs/toolkit";
|
import axios from "axios";
|
||||||
|
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
@@ -7,13 +8,39 @@ const initialState = {
|
|||||||
msg: null,
|
msg: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getMonitors = createAsyncThunk(
|
||||||
|
"monitors/getMonitors",
|
||||||
|
async (token, thunkApi) => {
|
||||||
|
try {
|
||||||
|
const res = await axios.get("http://localhost:5000/api/v1/monitors");
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
return thunkApi.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const monitorsSlice = createSlice({
|
const monitorsSlice = createSlice({
|
||||||
name: "monitors",
|
name: "monitors",
|
||||||
initialState,
|
initialState,
|
||||||
reducers: {
|
reducers: {},
|
||||||
setMonitors: (state, action) => {
|
extraReducers: (builder) => {
|
||||||
state.monitors = action.payload;
|
builder
|
||||||
},
|
.addCase(getMonitors.pending, (state, action) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(getMonitors.fulfilled, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
console.log(action.payload);
|
||||||
|
state.monitors = action.payload.data;
|
||||||
|
})
|
||||||
|
.addCase(getMonitors.rejected, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import Divider from "@mui/material/Divider";
|
|||||||
// Redux
|
// Redux
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
import { setMonitors } from "../../Features/Monitors/monitorsSlice";
|
import { getMonitors } from "../../Features/Monitors/monitorsSlice";
|
||||||
|
|
||||||
const cols = [
|
const cols = [
|
||||||
{
|
{
|
||||||
@@ -211,18 +211,8 @@ const Demo = () => {
|
|||||||
<Button
|
<Button
|
||||||
level="primary"
|
level="primary"
|
||||||
label="Get Monitors"
|
label="Get Monitors"
|
||||||
onClick={async () => {
|
onClick={() => {
|
||||||
try {
|
dispatch(getMonitors());
|
||||||
const response = await axios.get(
|
|
||||||
"http://localhost:5000/api/v1/monitors"
|
|
||||||
);
|
|
||||||
const monitorsResponse = response.data;
|
|
||||||
const monitors = monitorsResponse.data;
|
|
||||||
console.log(monitors);
|
|
||||||
dispatch(setMonitors(monitors));
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in new issue
Block a user