Revize a7264b57
Přidáno uživatelem Fantič před asi 2 roky(ů)
src/pages/ItemViewPage.tsx | ||
---|---|---|
3 | 3 |
import { useDispatch, useSelector } from "react-redux" |
4 | 4 |
import { AppDispatch } from "../stores/store" |
5 | 5 |
import { getPreviousConcordance, getNextConcordance, getItem } from "../stores/actions/itemThunks" |
6 |
import { Item, ItemState } from "../types/item" |
|
6 |
import { Item, ItemViewState } from "../types/item"
|
|
7 | 7 |
import { login } from "../stores/actions/userThunks" |
8 | 8 |
|
9 | 9 |
interface ItemViewProps { |
... | ... | |
14 | 14 |
|
15 | 15 |
const dispatch = useDispatch<AppDispatch>(); |
16 | 16 |
|
17 |
const itemViewState = useSelector((itemState: ItemState) => itemState) |
|
17 |
const itemViewState = useSelector((itemState: ItemViewState) => itemState)
|
|
18 | 18 |
|
19 | 19 |
const handleGetPreviousConcordance = async () => { |
20 | 20 |
dispatch(getPreviousConcordance()); |
... | ... | |
29 | 29 |
dispatch(getItem(props.itemId)); |
30 | 30 |
}, []); |
31 | 31 |
|
32 |
console.log(itemViewState.item);
|
|
32 |
console.log(itemViewState.item) |
|
33 | 33 |
|
34 | 34 |
return ( |
35 | 35 |
<VStack> |
src/stores/actions/itemThunks.ts | ||
---|---|---|
1 | 1 |
import { createAsyncThunk } from "@reduxjs/toolkit" |
2 | 2 |
import { getItemRequest } from "../../api/itemservice" |
3 | 3 |
import { getItemNotesRequest } from "../../api/notesservice" |
4 |
import { ItemState } from "../../types/item"; |
|
4 |
import { ItemViewState } from "../../types/item";
|
|
5 | 5 |
|
6 | 6 |
|
7 | 7 |
export const getItem = createAsyncThunk( |
... | ... | |
17 | 17 |
// data with image |
18 | 18 |
if (response.status === 200 && response.data.object.length > 1) { |
19 | 19 |
return { |
20 |
id: itemId, |
|
20 | 21 |
fullView: true, |
21 | 22 |
|
22 | 23 |
concordances: response.data.concordances, |
... | ... | |
47 | 48 |
// data without image |
48 | 49 |
else if (response.status === 200 && response.data.object.length == 1) { |
49 | 50 |
return { |
51 |
id: itemId, |
|
50 | 52 |
fullView: false, |
51 |
|
|
52 | 53 |
concordances: response.data.concordances, |
53 | 54 |
|
54 |
authorName: response.data.object[0].name[0].getty_data.display_name, |
|
55 |
authorDisplayName: response.data.object[0].name[0].getty_data.display_name,
|
|
55 | 56 |
workName: response.data.object[0].caption, |
57 |
inventoryItem: response.data.text, |
|
58 |
searchSubjects: response.data.search_subject, |
|
56 | 59 |
} |
57 | 60 |
} { |
58 | 61 |
return Promise.reject(response.data ? response.data : "Error") |
... | ... | |
66 | 69 |
export const getNextConcordance = createAsyncThunk( |
67 | 70 |
"item/getNextConcordance", |
68 | 71 |
async (_, { getState, dispatch }) => { |
69 |
const state = getState() as ItemState; |
|
72 |
const state = getState() as ItemViewState;
|
|
70 | 73 |
|
71 | 74 |
const nextIndex = (state.selectedConcordance + 1) % state.concordances.length; |
72 | 75 |
|
... | ... | |
81 | 84 |
export const getPreviousConcordance = createAsyncThunk( |
82 | 85 |
"item/getPreviousConcordance", |
83 | 86 |
async (_, { getState, dispatch }) => { |
84 |
const state = getState() as ItemState; |
|
87 |
const state = getState() as ItemViewState;
|
|
85 | 88 |
const previousIndex = (state.selectedConcordance - 1 + state.concordances.length) % state.concordances.length; |
86 | 89 |
await dispatch(getItem(state.concordances[previousIndex].id)); |
87 | 90 |
return previousIndex; |
src/stores/reducers/itemSlice.ts | ||
---|---|---|
1 | 1 |
import { PayloadAction, createSlice } from "@reduxjs/toolkit" |
2 | 2 |
import { getItem, getItemNotes, getNextConcordance, getPreviousConcordance } from "../actions/itemThunks" |
3 |
import { Certainty, ItemState, Item } from "../../types/item"; |
|
3 |
import { Certainty, ItemViewState, Item } from "../../types/item";
|
|
4 | 4 |
|
5 | 5 |
|
6 | 6 |
export const CertaintyWithColors: Record<Certainty, { certainty: Certainty; color: string }> = { |
... | ... | |
10 | 10 |
low: { certainty: Certainty.Low, color: "#00FF00" }, |
11 | 11 |
}; |
12 | 12 |
|
13 |
const initialState: ItemState = { |
|
13 |
const initialState: ItemViewState = {
|
|
14 | 14 |
item: {} as Item, |
15 | 15 |
selectedConcordance: 0, |
16 | 16 |
concordances: [], |
... | ... | |
32 | 32 |
state.concordances = action.payload.concordances; |
33 | 33 |
} |
34 | 34 |
|
35 |
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 |
} as Item |
|
44 |
|
|
35 | 45 |
if(action.payload.fullView){ |
36 | 46 |
state.item = { |
37 |
fullView: true, |
|
38 |
|
|
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 |
|
|
47 |
...state.item, |
|
45 | 48 |
// additional |
46 | 49 |
institution: action.payload.institution, |
47 | 50 |
authorName: action.payload.authorName, |
... | ... | |
52 | 55 |
description: action.payload.description |
53 | 56 |
} |
54 | 57 |
} |
55 |
else{ |
|
56 |
state.item = { |
|
57 |
fullView: false, |
|
58 |
authorDisplayName: action.payload.authorDisplayName, |
|
59 |
workName: action.payload.workName, |
|
60 |
concordances: action.payload.concordances, |
|
61 |
searchSubjects: action.payload.searchSubjects, |
|
62 |
inventoryItem: action.payload.inventoryItem |
|
63 |
} |
|
64 |
} |
|
65 | 58 |
}) |
66 | 59 |
builder.addCase(getItem.rejected, (state, action) => { |
67 | 60 |
state.lastError = action.error.message |
src/types/item.ts | ||
---|---|---|
1 | 1 |
import { Note } from "./note" |
2 | 2 |
|
3 | 3 |
export type Item = { |
4 |
id: string |
|
5 |
|
|
4 | 6 |
concordances: Concordance[] |
5 | 7 |
|
6 | 8 |
authorDisplayName: string |
7 | 9 |
workName: string |
8 |
|
|
9 | 10 |
inventoryItem: string |
10 | 11 |
searchSubjects: string[] |
11 | 12 |
|
... | ... | |
13 | 14 |
|
14 | 15 |
authorName?: string |
15 | 16 |
imageUrl?: string |
16 |
|
|
17 | 17 |
title?: string |
18 | 18 |
institution?: { |
19 | 19 |
name :string, |
... | ... | |
26 | 26 |
description?: string |
27 | 27 |
} |
28 | 28 |
|
29 |
export type ItemState = { |
|
29 |
export type ItemViewState = {
|
|
30 | 30 |
item: Item |
31 | 31 |
concordances: Concordance[] |
32 | 32 |
selectedConcordance: number |
Také k dispozici: Unified diff
re #10454: ItemView: minor api call update