Projekt

Obecné

Profil

Stáhnout (3.26 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2
import { getItem, getItemNotes } from "../actions/itemThunks"
3
import { Certainty, ItemViewState, Item } from "../../types/item";
4

    
5
// TODO set colors
6
export const CertaintyWithColors: Record<Certainty, { certainty: Certainty; color: string }> = {
7
    same_as: { certainty: Certainty.SameAs, color: "light.300" },
8
    high: { certainty: Certainty.High, color: "success.300" },
9
    medium: { certainty: Certainty.Medium, color: "warning.300" },
10
    low: { certainty: Certainty.Low, color: "danger.300" },
11
    unknown : { certainty: Certainty.Unknown, color: "light.300"}
12
};
13

    
14
const initialState: ItemViewState = {
15
    item: {} as Item,
16
    concordances: [],
17
    notes: [],
18
    lastError: "",
19
    itemLoading: true,
20
    notesLoading: true
21
}
22

    
23
export const itemSlice = createSlice({
24
    name: "item",
25
    initialState: initialState,
26
    reducers: {
27
        setConcordances: (state, action) => {
28
            state.concordances = action.payload.concordances;
29
        }
30
    },
31
    extraReducers: (builder) => {
32
        // getItem
33
        builder.addCase(getItem.fulfilled, (state, action) => {
34

    
35
            state.item = {
36
                id : action.payload.id,
37
                authorDisplayName : action.payload.authorDisplayName,
38
                workName : action.payload.workName,
39
                concordances : action.payload.concordances,
40
                searchSubjects : action.payload.searchSubjects,
41
                inventoryItem : action.payload.inventoryItem,
42
                fullView : action.payload.fullView,
43
            }
44

    
45
            if (action.payload.fullView) {
46
                state.item.institution = action.payload.institution;
47
                state.item.authorName = action.payload.authorName;
48
                state.item.imageUrl = action.payload.imageUrl;
49
                state.item.title = action.payload.title;
50
                state.item.repository = action.payload.repository;
51
                state.item.provenance = action.payload.provenance;
52
                state.item.description = action.payload.description;
53
            }
54
            else {
55
                state.item.institution = undefined;
56
                state.item.authorName = undefined;
57
                state.item.imageUrl = undefined;
58
                state.item.title = undefined;
59
                state.item.repository = undefined;
60
                state.item.provenance = undefined;
61
                state.item.description = undefined;
62
            }
63

    
64
            state.itemLoading = false;
65
        })
66
        builder.addCase(getItem.pending, (state, action) => {
67
            state.itemLoading = true;
68
        })
69
        builder.addCase(getItem.rejected, (state, action) => {
70
            state.itemLoading = false;
71
            state.lastError = action.error.message
72
        })
73

    
74
        // getItemNotes
75
        builder.addCase(getItemNotes.fulfilled, (state, action) => {
76
            state.notesLoading = false;
77
            state.notes = action.payload.notes;
78
        })
79
        builder.addCase(getItemNotes.pending, (state, action) => {
80
            state.notesLoading = true;
81
        })
82
        builder.addCase(getItemNotes.rejected, (state, action) => {
83
            state.notesLoading = false;
84
            state.lastError = action.error.message
85
        })
86

    
87
    }
88
})
89

    
90
export default itemSlice.reducer
(2-2/6)