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
    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 f4af30c8 Fantič
                room: action.payload.room
46 356c8bce Fantič
            }
47 c03fd43c Fantič
48 fedb75d6 Fantič
            if (action.payload.fullView) {
49
                state.item.institution = action.payload.institution;
50
                state.item.authorName = action.payload.authorName;
51 e0741b92 Fantič
                state.item.images = action.payload.images;
52
                state.item.title = action.payload.images?.[0].title ?? undefined;
53 fedb75d6 Fantič
                state.item.repository = action.payload.repository;
54
                state.item.provenance = action.payload.provenance;
55
                state.item.description = action.payload.description;
56
            }
57 c03fd43c Fantič
            else {
58 fedb75d6 Fantič
                state.item.institution = undefined;
59
                state.item.authorName = undefined;
60 e0741b92 Fantič
                state.item.images = undefined;
61 fedb75d6 Fantič
                state.item.title = undefined;
62
                state.item.repository = undefined;
63
                state.item.provenance = undefined;
64
                state.item.description = undefined;
65
            }
66 852de08e Fantič
67
            state.itemLoading = false;
68
        })
69
        builder.addCase(getItem.pending, (state, action) => {
70
            state.itemLoading = true;
71 97deff21 Fantič
        })
72
        builder.addCase(getItem.rejected, (state, action) => {
73 852de08e Fantič
            state.itemLoading = false;
74 97deff21 Fantič
            state.lastError = action.error.message
75
        })
76
77 88d2df9a Fantič
        // getItemNotes
78
        builder.addCase(getItemNotes.fulfilled, (state, action) => {
79 852de08e Fantič
            state.notesLoading = false;
80 88d2df9a Fantič
            state.notes = action.payload.notes;
81
        })
82 852de08e Fantič
        builder.addCase(getItemNotes.pending, (state, action) => {
83
            state.notesLoading = true;
84
        })
85 88d2df9a Fantič
        builder.addCase(getItemNotes.rejected, (state, action) => {
86 852de08e Fantič
            state.notesLoading = false;
87 88d2df9a Fantič
            state.lastError = action.error.message
88
        })
89
90 97deff21 Fantič
    }
91
})
92
93
export default itemSlice.reducer