1
|
import React, { ReactNode, useEffect } from "react"
|
2
|
import { useDispatch, useSelector } from "react-redux"
|
3
|
import { AppDispatch, RootState } from "../stores/store"
|
4
|
import { getItem, getItemNotes, setConcordances } from "../stores/actions/itemThunks"
|
5
|
import { login } from "../stores/actions/userThunks"
|
6
|
import { NavigationState, SceneMap, SceneRendererProps, TabBar, TabView } from "react-native-tab-view"
|
7
|
import ItemView from "../components/item/ItemView"
|
8
|
import { Button, Pressable, View, Text, Icon, CircleIcon, ScrollView, HStack, VStack } from "native-base"
|
9
|
import { useWindowDimensions } from "react-native"
|
10
|
import LoadingBox from "../components/loading/LoadingBox"
|
11
|
import { CertaintyWithColors } from "../stores/reducers/itemSlice"
|
12
|
import ItemTabBar from "../components/item/ItemTabBar"
|
13
|
|
14
|
|
15
|
interface ItemViewProps {
|
16
|
itemId: string
|
17
|
}
|
18
|
|
19
|
|
20
|
const ItemViewPage = (props: ItemViewProps) => {
|
21
|
|
22
|
const layout = useWindowDimensions();
|
23
|
|
24
|
const [routes, setRoutes] = React.useState([
|
25
|
// initial tab
|
26
|
{ key: 'loading', title: 'Loading item...' },
|
27
|
]);
|
28
|
|
29
|
// // TODO vyřešit, aby nebyly freezy při přepínání
|
30
|
// const [renderSceneDict, setRenderSceneDict] = React.useState({});
|
31
|
|
32
|
const [index, setIndex] = React.useState(0);
|
33
|
|
34
|
const dispatch = useDispatch<AppDispatch>();
|
35
|
|
36
|
const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
|
37
|
|
38
|
// initial: load main item
|
39
|
useEffect(() => {
|
40
|
// TODO remove login from here
|
41
|
dispatch(login({ username: "Viktorie", password: "Golem123." }));
|
42
|
dispatch(getItem(props.itemId));
|
43
|
}, []);
|
44
|
|
45
|
|
46
|
// concordances are loaded
|
47
|
useEffect(() => {
|
48
|
if (item && concordances && concordances.length > 0) {
|
49
|
let concordanceRoutes = [];
|
50
|
|
51
|
// let _renderSceneDict: any = {};
|
52
|
|
53
|
for (let i = 0; i < concordances.length; i++) {
|
54
|
let concordance = concordances[i];
|
55
|
concordanceRoutes.push({
|
56
|
key: concordance.id,
|
57
|
title: concordance.id
|
58
|
})
|
59
|
// _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
|
60
|
}
|
61
|
|
62
|
setRoutes(concordanceRoutes);
|
63
|
// setRenderSceneDict(_renderSceneDict);
|
64
|
}
|
65
|
}, [concordances]);
|
66
|
|
67
|
// item changes
|
68
|
useEffect(() => {
|
69
|
if (!itemLoading && item && item.id) {
|
70
|
if (index == 0) {
|
71
|
dispatch(setConcordances(item.concordances));
|
72
|
}
|
73
|
// set item notes if item is loaded
|
74
|
if (item && item.id) {
|
75
|
dispatch(getItemNotes(item.id));
|
76
|
}
|
77
|
}
|
78
|
}, [item]);
|
79
|
|
80
|
const handleIndexChanged = (index: number) => {
|
81
|
console.log("index changed");
|
82
|
setIndex(index);
|
83
|
if (concordances && concordances.length > 0) {
|
84
|
dispatch(getItem(concordances[index].id));
|
85
|
}
|
86
|
}
|
87
|
|
88
|
return (
|
89
|
!concordances || concordances.length < 1 ?
|
90
|
<LoadingBox text={`Loading item ${props.itemId}...`}></LoadingBox>
|
91
|
:
|
92
|
<TabView
|
93
|
renderTabBar={() => ItemTabBar({
|
94
|
navigationState: {
|
95
|
routes: routes,
|
96
|
},
|
97
|
concordances: concordances,
|
98
|
index: index,
|
99
|
onIndexChange: handleIndexChanged
|
100
|
})}
|
101
|
|
102
|
navigationState={{ index, routes }}
|
103
|
renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} />}
|
104
|
|
105
|
initialLayout={{ width: layout.width }}
|
106
|
// prevent default action on index change
|
107
|
onIndexChange={handleIndexChanged}
|
108
|
|
109
|
/>
|
110
|
);
|
111
|
}
|
112
|
|
113
|
|
114
|
|
115
|
export default ItemViewPage;
|