Projekt

Obecné

Profil

Stáhnout (2.91 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

    
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
const initialState: ItemViewState = {
14
    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
        setSelectedConcordance: (state, action) => {
26
            state.selectedConcordance = action.payload.selectedConcordance;
27
        },
28
        setConcordances: (state, action) => {
29
            state.concordances = action.payload.concordances;
30
        }
31
    },
32
    extraReducers: (builder) => {
33
        // getItem
34
        builder.addCase(getItem.fulfilled, (state, action) => {
35

    
36
            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
            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
            else {
54
                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
        })
63
        builder.addCase(getItem.rejected, (state, action) => {
64
            state.lastError = action.error.message
65
        })
66

    
67
        // 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
    }
76
})
77

    
78
export default itemSlice.reducer
(1-1/2)