corpediaslice.js
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import qs from "qs";
export const fetchCorpediaList = createAsyncThunk(
"CorpediaListSlice/fetchCorpediaList",
async () => {
const query = {
populate: [
"Image"
],
};
const queryString = qs.stringify(query, {
encodeValuesOnly: true,
});
const endpoint = `${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/corpedias?${queryString}`;
const response = await axios.get(endpoint);
const data = response.data.data;
return data;
}
);
const CorpediaListSlice = createSlice({
name: "corpedia",
initialState: {
status: "idle",
data: [],
error: null,
},
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchCorpediaList.pending, (state) => {
state.status = "loading";
})
.addCase(fetchCorpediaList.fulfilled, (state, action) => {
state.status = "succeeded";
state.data = action.payload;
})
.addCase(fetchCorpediaList.rejected, (state, action) => {
state.status = "failed";
state.error = action.error.message;
});
},
});
export default CorpediaListSlice.reducer;