Projekt

Obecné

Profil

Stáhnout (3.88 KB) Statistiky
| Větev: | Tag: | Revize:
1 c2c8470e Fantič
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2
import { PlanViewState } from "../../types/plan";
3 aac857cf Fantič
import { getFloorList, getPlanFloorImage, getPlanInventories, getPlanItems } from "../actions/planThunks";
4 c2c8470e Fantič
5
const initialState: PlanViewState = {
6
    lastError: "",
7 f8083631 Fantič
8 9641b62f Fantič
    // TODO backend -> retrieve list of floors, not static -> not implemented at the time of coding
9
    floorList: [
10
    ],
11
    floorListLoading: false,
12
13 160b7073 Fantič
    itemInventories: [],
14 a6999074 Fantič
    itemInventoriesLoading: false,
15 9641b62f Fantič
16 a6999074 Fantič
    // reseting itemList
17
    itemListLoading: {},
18
    itemList: {},
19
    itemListPagination: {},
20 c2c8470e Fantič
}
21
22
export const planSlice = createSlice({
23
    name: "plan",
24
    initialState: initialState,
25
    reducers: {},
26
    extraReducers: (builder) => {
27 f8083631 Fantič
28 160b7073 Fantič
        // getFloorList
29 f8083631 Fantič
        builder.addCase(getFloorList.fulfilled, (state, action) => {
30
            state.floorList = action.payload
31
            state.floorListLoading = false;
32
        })
33
        builder.addCase(getFloorList.pending, (state, action) => {
34
            state.floorListLoading = true;
35
        })
36
        builder.addCase(getFloorList.rejected, (state, action) => {
37
            state.floorListLoading = false;
38
            state.lastError = action.error.message
39
        })
40 160b7073 Fantič
41
        // getPlanItems
42
        builder.addCase(getPlanItems.fulfilled, (state, action) => {
43 a6999074 Fantič
            if (!state.itemList) {
44
                state.itemList = {}
45
            }
46
47
            if (action.meta.arg.inventory in state.itemList) {
48
                state.itemList[action.meta.arg.inventory] = state.itemList[action.meta.arg.inventory].concat(action.payload.items)
49
            }
50
            else {
51
                state.itemList[action.meta.arg.inventory] = action.payload.items
52
            }
53
54
            if (!state.itemListLoading) {
55
                state.itemListLoading = {}
56
            }
57
            state.itemListLoading[action.meta.arg.inventory] = false;
58
59
            if (!state.itemListPagination) {
60
                state.itemListPagination = {}
61
            }
62
            state.itemListPagination[action.meta.arg.inventory] = action.payload.inventoryPagination;
63 160b7073 Fantič
        })
64 a6999074 Fantič
65 160b7073 Fantič
        builder.addCase(getPlanItems.pending, (state, action) => {
66 a6999074 Fantič
67
            if (!state.itemListLoading) {
68
                state.itemListLoading = {}
69
            }
70
            state.itemListLoading[action.meta.arg.inventory] = true;
71 160b7073 Fantič
        })
72
        builder.addCase(getPlanItems.rejected, (state, action) => {
73 a6999074 Fantič
74
            if (!state.itemListLoading) {
75
                state.itemListLoading = {}
76
            }
77
            state.itemListLoading[action.meta.arg.inventory] = false;
78 160b7073 Fantič
            state.lastError = action.error.message
79
        })
80
81
        // getPlanInventories
82
        builder.addCase(getPlanInventories.fulfilled, (state, action) => {
83
            state.itemInventories = action.payload.inventories
84
            state.totalItemsCount = action.payload.allInventoriesRecords
85
            state.itemInventoriesLoading = false;
86 a6999074 Fantič
87
            // reseting itemList
88
            state.itemListLoading = {}
89
            state.itemList = {}
90
            state.itemListPagination = {}
91 160b7073 Fantič
        })
92
        builder.addCase(getPlanInventories.pending, (state, action) => {
93
            state.itemInventoriesLoading = true;
94
        })
95
        builder.addCase(getPlanInventories.rejected, (state, action) => {
96
            state.itemInventoriesLoading = false;
97
            state.lastError = action.error.message
98
        })
99 aac857cf Fantič
100
        // getPlanFloorImage
101
        builder.addCase(getPlanFloorImage.fulfilled, (state, action) => {
102
            state.mapImage = action.payload
103
            state.mapImageLoading = false
104
        })
105
        builder.addCase(getPlanFloorImage.pending, (state, action) => {
106
            state.mapImageLoading = true
107
        })
108
        builder.addCase(getPlanFloorImage.rejected, (state, action) => {
109
            state.mapImageLoading = false;
110
            state.lastError = action.error.message
111
        })
112 c2c8470e Fantič
    }
113
})
114
115
export default planSlice.reducer