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
|
unknown : { certainty: Certainty.Unknown, color: "#000000"}
|
12
|
};
|
13
|
|
14
|
const initialState: ItemViewState = {
|
15
|
item: {} as Item,
|
16
|
selectedConcordance: 0,
|
17
|
concordances: [],
|
18
|
notes: [],
|
19
|
lastError: ""
|
20
|
}
|
21
|
|
22
|
export const itemSlice = createSlice({
|
23
|
name: "item",
|
24
|
initialState: initialState,
|
25
|
reducers: {
|
26
|
setSelectedConcordance: (state, action) => {
|
27
|
state.selectedConcordance = action.payload.selectedConcordance;
|
28
|
},
|
29
|
setConcordances: (state, action) => {
|
30
|
state.concordances = action.payload.concordances;
|
31
|
}
|
32
|
},
|
33
|
extraReducers: (builder) => {
|
34
|
// getItem
|
35
|
builder.addCase(getItem.fulfilled, (state, action) => {
|
36
|
|
37
|
state.item = {
|
38
|
id : action.payload.id,
|
39
|
authorDisplayName : action.payload.authorDisplayName,
|
40
|
workName : action.payload.workName,
|
41
|
concordances : action.payload.concordances,
|
42
|
searchSubjects : action.payload.searchSubjects,
|
43
|
inventoryItem : action.payload.inventoryItem,
|
44
|
fullView : action.payload.fullView,
|
45
|
}
|
46
|
|
47
|
if (action.payload.fullView) {
|
48
|
state.item.institution = action.payload.institution;
|
49
|
state.item.authorName = action.payload.authorName;
|
50
|
state.item.imageUrl = action.payload.imageUrl;
|
51
|
state.item.title = action.payload.title;
|
52
|
state.item.repository = action.payload.repository;
|
53
|
state.item.provenance = action.payload.provenance;
|
54
|
state.item.description = action.payload.description;
|
55
|
}
|
56
|
else {
|
57
|
state.item.institution = undefined;
|
58
|
state.item.authorName = undefined;
|
59
|
state.item.imageUrl = undefined;
|
60
|
state.item.title = undefined;
|
61
|
state.item.repository = undefined;
|
62
|
state.item.provenance = undefined;
|
63
|
state.item.description = undefined;
|
64
|
}
|
65
|
})
|
66
|
builder.addCase(getItem.rejected, (state, action) => {
|
67
|
state.lastError = action.error.message
|
68
|
})
|
69
|
|
70
|
// getItemNotes
|
71
|
builder.addCase(getItemNotes.fulfilled, (state, action) => {
|
72
|
state.notes = action.payload.notes;
|
73
|
})
|
74
|
builder.addCase(getItemNotes.rejected, (state, action) => {
|
75
|
state.lastError = action.error.message
|
76
|
})
|
77
|
|
78
|
}
|
79
|
})
|
80
|
|
81
|
export default itemSlice.reducer
|