Projekt

Obecné

Profil

Stáhnout (3.42 KB) Statistiky
| Větev: | Tag: | Revize:
1 97deff21 Fantič
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2 c03fd43c Fantič
import { getItem, getItemNotes } from "../actions/itemThunks"
3 a7264b57 Fantič
import { Certainty, ItemViewState, Item } from "../../types/item";
4 97deff21 Fantič
5 852de08e Fantič
// TODO set colors
6 97deff21 Fantič
export const CertaintyWithColors: Record<Certainty, { certainty: Certainty; color: string }> = {
7 852de08e Fantič
    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 97deff21 Fantič
};
13
14 a7264b57 Fantič
const initialState: ItemViewState = {
15 97deff21 Fantič
    item: {} as Item,
16
    selectedConcordance: 0,
17
    concordances: [],
18
    notes: [],
19 852de08e Fantič
    lastError: "",
20
    itemLoading: true,
21
    notesLoading: true
22 97deff21 Fantič
}
23
24
export const itemSlice = createSlice({
25
    name: "item",
26
    initialState: initialState,
27
    reducers: {
28 c03fd43c Fantič
        setSelectedConcordance: (state, action) => {
29
            state.selectedConcordance = action.payload.selectedConcordance;
30
        },
31
        setConcordances: (state, action) => {
32
            state.concordances = action.payload.concordances;
33
        }
34 97deff21 Fantič
    },
35
    extraReducers: (builder) => {
36
        // getItem
37 fedb75d6 Fantič
        builder.addCase(getItem.fulfilled, (state, action) => {
38 97deff21 Fantič
39 356c8bce Fantič
            state.item = {
40
                id : action.payload.id,
41
                authorDisplayName : action.payload.authorDisplayName,
42
                workName : action.payload.workName,
43
                concordances : action.payload.concordances,
44
                searchSubjects : action.payload.searchSubjects,
45
                inventoryItem : action.payload.inventoryItem,
46
                fullView : action.payload.fullView,
47
            }
48 c03fd43c Fantič
49 fedb75d6 Fantič
            if (action.payload.fullView) {
50
                state.item.institution = action.payload.institution;
51
                state.item.authorName = action.payload.authorName;
52
                state.item.imageUrl = action.payload.imageUrl;
53
                state.item.title = action.payload.title;
54
                state.item.repository = action.payload.repository;
55
                state.item.provenance = action.payload.provenance;
56
                state.item.description = action.payload.description;
57
            }
58 c03fd43c Fantič
            else {
59 fedb75d6 Fantič
                state.item.institution = undefined;
60
                state.item.authorName = undefined;
61
                state.item.imageUrl = undefined;
62
                state.item.title = undefined;
63
                state.item.repository = undefined;
64
                state.item.provenance = undefined;
65
                state.item.description = undefined;
66
            }
67 852de08e Fantič
68
            state.itemLoading = false;
69
        })
70
        builder.addCase(getItem.pending, (state, action) => {
71
            state.itemLoading = true;
72 97deff21 Fantič
        })
73
        builder.addCase(getItem.rejected, (state, action) => {
74 852de08e Fantič
            state.itemLoading = false;
75 97deff21 Fantič
            state.lastError = action.error.message
76
        })
77
78 88d2df9a Fantič
        // getItemNotes
79
        builder.addCase(getItemNotes.fulfilled, (state, action) => {
80 852de08e Fantič
            state.notesLoading = false;
81 88d2df9a Fantič
            state.notes = action.payload.notes;
82
        })
83 852de08e Fantič
        builder.addCase(getItemNotes.pending, (state, action) => {
84
            state.notesLoading = true;
85
        })
86 88d2df9a Fantič
        builder.addCase(getItemNotes.rejected, (state, action) => {
87 852de08e Fantič
            state.notesLoading = false;
88 88d2df9a Fantič
            state.lastError = action.error.message
89
        })
90
91 97deff21 Fantič
    }
92
})
93
94
export default itemSlice.reducer