Projekt

Obecné

Profil

Stáhnout (3.4 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: "white" },
8
    high: { certainty: Certainty.High, color: "#aedfb5" },
9
    medium: { certainty: Certainty.Medium, color: "#ffd8b3" },
10
    low: { certainty: Certainty.Low, color: "#faa" },
11
    unknown : { certainty: Certainty.Unknown, color: "white"}
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
                prevItem: action.payload.prevItem,
43
                nextItem: action.payload.nextItem,
44
                fullView : action.payload.fullView,
45
                room: action.payload.room
46
            }
47

    
48
            if (action.payload.fullView) {
49
                state.item.institution = action.payload.institution;
50
                state.item.authorName = action.payload.authorName;
51
                state.item.images = action.payload.images;
52
                state.item.title = action.payload.images?.[0].title ?? undefined;
53
                state.item.repository = action.payload.repository;
54
                state.item.provenance = action.payload.provenance;
55
                state.item.description = action.payload.description;
56
            }
57
            else {
58
                state.item.institution = undefined;
59
                state.item.authorName = undefined;
60
                state.item.images = undefined;
61
                state.item.title = undefined;
62
                state.item.repository = undefined;
63
                state.item.provenance = undefined;
64
                state.item.description = undefined;
65
            }
66

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

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

    
90
    }
91
})
92

    
93
export default itemSlice.reducer
(2-2/7)