Projekt

Obecné

Profil

Stáhnout (3.88 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2
import { PlanViewState } from "../../types/plan";
3
import { getFloorList, getPlanFloorImage, getPlanInventories, getPlanItems } from "../actions/planThunks";
4

    
5
const initialState: PlanViewState = {
6
    lastError: "",
7

    
8
    // TODO backend -> retrieve list of floors, not static -> not implemented at the time of coding
9
    floorList: [
10
    ],
11
    floorListLoading: false,
12

    
13
    itemInventories: [],
14
    itemInventoriesLoading: false,
15

    
16
    // reseting itemList
17
    itemListLoading: {},
18
    itemList: {},
19
    itemListPagination: {},
20
}
21

    
22
export const planSlice = createSlice({
23
    name: "plan",
24
    initialState: initialState,
25
    reducers: {},
26
    extraReducers: (builder) => {
27

    
28
        // getFloorList
29
        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

    
41
        // getPlanItems
42
        builder.addCase(getPlanItems.fulfilled, (state, action) => {
43
            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
        })
64

    
65
        builder.addCase(getPlanItems.pending, (state, action) => {
66

    
67
            if (!state.itemListLoading) {
68
                state.itemListLoading = {}
69
            }
70
            state.itemListLoading[action.meta.arg.inventory] = true;
71
        })
72
        builder.addCase(getPlanItems.rejected, (state, action) => {
73

    
74
            if (!state.itemListLoading) {
75
                state.itemListLoading = {}
76
            }
77
            state.itemListLoading[action.meta.arg.inventory] = false;
78
            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

    
87
            // reseting itemList
88
            state.itemListLoading = {}
89
            state.itemList = {}
90
            state.itemListPagination = {}
91
        })
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

    
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
    }
113
})
114

    
115
export default planSlice.reducer
(5-5/7)