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