Projekt

Obecné

Profil

Stáhnout (3.38 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
    concordances: [],
17
    notes: [],
18 852de08e Fantič
    lastError: "",
19
    itemLoading: true,
20
    notesLoading: true
21 97deff21 Fantič
}
22
23
export const itemSlice = createSlice({
24
    name: "item",
25
    initialState: initialState,
26
    reducers: {
27 c03fd43c Fantič
        setConcordances: (state, action) => {
28
            state.concordances = action.payload.concordances;
29
        }
30 97deff21 Fantič
    },
31
    extraReducers: (builder) => {
32
        // getItem
33 fedb75d6 Fantič
        builder.addCase(getItem.fulfilled, (state, action) => {
34 97deff21 Fantič
35 356c8bce Fantič
            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 8ff3394e Fantič
                prevItem: action.payload.prevItem,
43
                nextItem: action.payload.nextItem,
44 356c8bce Fantič
                fullView : action.payload.fullView,
45
            }
46 c03fd43c Fantič
47 fedb75d6 Fantič
            if (action.payload.fullView) {
48
                state.item.institution = action.payload.institution;
49
                state.item.authorName = action.payload.authorName;
50 e0741b92 Fantič
                state.item.images = action.payload.images;
51
                state.item.title = action.payload.images?.[0].title ?? undefined;
52 fedb75d6 Fantič
                state.item.repository = action.payload.repository;
53
                state.item.provenance = action.payload.provenance;
54
                state.item.description = action.payload.description;
55
            }
56 c03fd43c Fantič
            else {
57 fedb75d6 Fantič
                state.item.institution = undefined;
58
                state.item.authorName = undefined;
59 e0741b92 Fantič
                state.item.images = undefined;
60 fedb75d6 Fantič
                state.item.title = undefined;
61
                state.item.repository = undefined;
62
                state.item.provenance = undefined;
63
                state.item.description = undefined;
64
            }
65 852de08e Fantič
66
            state.itemLoading = false;
67
        })
68
        builder.addCase(getItem.pending, (state, action) => {
69
            state.itemLoading = true;
70 97deff21 Fantič
        })
71
        builder.addCase(getItem.rejected, (state, action) => {
72 852de08e Fantič
            state.itemLoading = false;
73 97deff21 Fantič
            state.lastError = action.error.message
74
        })
75
76 88d2df9a Fantič
        // getItemNotes
77
        builder.addCase(getItemNotes.fulfilled, (state, action) => {
78 852de08e Fantič
            state.notesLoading = false;
79 88d2df9a Fantič
            state.notes = action.payload.notes;
80
        })
81 852de08e Fantič
        builder.addCase(getItemNotes.pending, (state, action) => {
82
            state.notesLoading = true;
83
        })
84 88d2df9a Fantič
        builder.addCase(getItemNotes.rejected, (state, action) => {
85 852de08e Fantič
            state.notesLoading = false;
86 88d2df9a Fantič
            state.lastError = action.error.message
87
        })
88
89 97deff21 Fantič
    }
90
})
91
92
export default itemSlice.reducer