Projekt

Obecné

Profil

Stáhnout (2.52 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2
import { getItem, getItemNotes, getNextConcordance, getPreviousConcordance } from "../actions/itemThunks"
3
import { Note } from "./notesSlice"
4

    
5

    
6
export interface Item {
7
    authorName: string
8
    caption: string
9

    
10

    
11
    concordances: Concordance[]
12
}
13

    
14
export interface ItemState {
15
    item: Item
16
    concordances: Concordance[]
17
    selectedConcordance: number
18
    notes: Note[]
19
    lastError?: string
20
}
21

    
22
export interface Concordance {
23
    id: string
24
    certainty: Certainty
25
}
26

    
27
enum Certainty {
28
    SameAs = "same_as",
29
    High = "high",
30
    Medium = "medium",
31
    Low = "low",
32
}
33

    
34
export const CertaintyWithColors: Record<Certainty, { certainty: Certainty; color: string }> = {
35
    same_as: { certainty: Certainty.SameAs, color: "#000000" },
36
    high: { certainty: Certainty.High, color: "#FF0000" },
37
    medium: { certainty: Certainty.Medium, color: "#FFFF00" },
38
    low: { certainty: Certainty.Low, color: "#00FF00" },
39
};
40

    
41
const initialState: ItemState = {
42
    item: {} as Item,
43
    selectedConcordance: 0,
44
    concordances: [],
45
    notes: [],
46
    lastError: ""
47
}
48

    
49
export const itemSlice = createSlice({
50
    name: "item",
51
    initialState: initialState,
52
    reducers: {
53
    },
54
    extraReducers: (builder) => {
55
        // getItem
56
        builder.addCase(getItem.fulfilled, (state, action) => {
57

    
58
            // set concordances from item, if selected concordance is 0
59
            if (state.selectedConcordance === 0) {
60
                state.concordances = action.payload.concordances;
61
            }
62

    
63
            state.item = {
64
                authorName: action.payload.authorName,
65
                caption: action.payload.caption,
66
                concordances: action.payload.concordances
67
            }
68

    
69
        })
70
        builder.addCase(getItem.rejected, (state, action) => {
71
            state.lastError = action.error.message
72
        })
73

    
74
        // getNextConcordance
75
        builder.addCase(getNextConcordance.fulfilled, (state, action) => {
76
            state.selectedConcordance = action.payload;
77
        });
78

    
79
        builder.addCase(getNextConcordance.rejected, (state, action) => {
80
            state.lastError = action.error.message;
81
        });
82

    
83
        // getPreviousConcordance
84
        builder.addCase(getPreviousConcordance.fulfilled, (state, action) => {
85
            state.selectedConcordance = action.payload;
86
        });
87

    
88
        builder.addCase(getPreviousConcordance.rejected, (state, action) => {
89
            state.lastError = action.error.message;
90
        });
91

    
92
    }
93
})
94

    
95
export default itemSlice.reducer
(1-1/3)