Projekt

Obecné

Profil

Stáhnout (1.07 KB) Statistiky
| Větev: | Tag: | Revize:
1 4da7b143 Schwobik
import { HomePageData } from "../../types/homePageTypes"
2
import { createSlice } from "@reduxjs/toolkit"
3
import { fetchData } from "../actions/homePageThunks"
4
5
export interface HomePageState {
6
    lastError?: string
7
    loading: boolean
8
    data: HomePageData
9
}
10
11
const initialState: HomePageState = {
12
    lastError: "",
13
    loading: false,
14
    data: []
15
}
16
17
export const homePageSlice = createSlice({
18
    name: "homePage",
19
    initialState: initialState,
20
    reducers: {
21
        resetHomePage: () => initialState,
22
    },
23
    extraReducers: (builder) => {
24
        builder.addCase(fetchData.pending, (state) => {
25
            state.loading = true
26
        })
27
        builder.addCase(fetchData.fulfilled, (state, action) => {
28
            state.loading = false
29
            state.data = action.payload
30
            state.lastError = ""
31
        })
32
        builder.addCase(fetchData.rejected, (state, action) => {
33
            state.loading = false
34
            state.lastError = action.error.message
35
        })
36
    }
37
})
38
39
export const { resetHomePage } = homePageSlice.actions
40
export default homePageSlice.reducer