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