Projekt

Obecné

Profil

Stáhnout (2.91 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
6
export const CertaintyWithColors: Record<Certainty, { certainty: Certainty; color: string }> = {
7
    same_as: { certainty: Certainty.SameAs, color: "#000000" },
8
    high: { certainty: Certainty.High, color: "#FF0000" },
9
    medium: { certainty: Certainty.Medium, color: "#FFFF00" },
10
    low: { certainty: Certainty.Low, color: "#00FF00" },
11
};
12
13 a7264b57 Fantič
const initialState: ItemViewState = {
14 97deff21 Fantič
    item: {} as Item,
15
    selectedConcordance: 0,
16
    concordances: [],
17
    notes: [],
18
    lastError: ""
19
}
20
21
export const itemSlice = createSlice({
22
    name: "item",
23
    initialState: initialState,
24
    reducers: {
25 c03fd43c Fantič
        setSelectedConcordance: (state, action) => {
26
            state.selectedConcordance = action.payload.selectedConcordance;
27
        },
28
        setConcordances: (state, action) => {
29
            state.concordances = action.payload.concordances;
30
        }
31 97deff21 Fantič
    },
32
    extraReducers: (builder) => {
33
        // getItem
34 fedb75d6 Fantič
        builder.addCase(getItem.fulfilled, (state, action) => {
35 97deff21 Fantič
36 c03fd43c Fantič
            state.item.id = action.payload.id;
37
            state.item.authorDisplayName = action.payload.authorDisplayName;
38
            state.item.workName = action.payload.workName;
39
            state.item.concordances = action.payload.concordances;
40
            state.item.searchSubjects = action.payload.searchSubjects;
41
            state.item.inventoryItem = action.payload.inventoryItem;
42
            state.item.fullView = action.payload.fullView;
43
44 fedb75d6 Fantič
            if (action.payload.fullView) {
45
                state.item.institution = action.payload.institution;
46
                state.item.authorName = action.payload.authorName;
47
                state.item.imageUrl = action.payload.imageUrl;
48
                state.item.title = action.payload.title;
49
                state.item.repository = action.payload.repository;
50
                state.item.provenance = action.payload.provenance;
51
                state.item.description = action.payload.description;
52
            }
53 c03fd43c Fantič
            else {
54 fedb75d6 Fantič
                state.item.institution = undefined;
55
                state.item.authorName = undefined;
56
                state.item.imageUrl = undefined;
57
                state.item.title = undefined;
58
                state.item.repository = undefined;
59
                state.item.provenance = undefined;
60
                state.item.description = undefined;
61
            }
62 97deff21 Fantič
        })
63
        builder.addCase(getItem.rejected, (state, action) => {
64
            state.lastError = action.error.message
65
        })
66
67 88d2df9a Fantič
        // getItemNotes
68
        builder.addCase(getItemNotes.fulfilled, (state, action) => {
69
            state.notes = action.payload.notes;
70
        })
71
        builder.addCase(getItemNotes.rejected, (state, action) => {
72
            state.lastError = action.error.message
73
        })
74
75 97deff21 Fantič
    }
76
})
77
78
export default itemSlice.reducer