taxwireslice.js
1.25 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
47
48
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import qs from "qs";
export const fetchTaxwireList = createAsyncThunk(
  "TaxwireListSlice/fetchTaxwireList",
  async () => {
 const query = {
  populate: { Image: { populate: "*" } },
  pagination: { page: 1, pageSize: 1000 },
  sort: ["createdAt:desc"], 
};
    
    const queryString = qs.stringify(query, {
      encodeValuesOnly: true,
    });
    const endpoint = `${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/taxwires?${queryString}`;
    const response = await axios.get(endpoint);
    const data = response.data.data;
    return data;
  }
);
const TaxwireListSlice = createSlice({
  name: "taxwire",
  initialState: {
    status: "idle",
    data: [],
    error: null,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchTaxwireList.pending, (state) => {
        state.status = "loading";
      })
      .addCase(fetchTaxwireList.fulfilled, (state, action) => {
        state.status = "succeeded";
        state.data = action.payload;
      })
      .addCase(fetchTaxwireList.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.error.message;
      });
  },
});
export default TaxwireListSlice.reducer;