1 |
852de08e
|
Fantič
|
import React, { useEffect } from "react"
|
2 |
97deff21
|
Fantič
|
import { useDispatch, useSelector } from "react-redux"
|
3 |
fedb75d6
|
Fantič
|
import { AppDispatch, RootState } from "../stores/store"
|
4 |
88d2df9a
|
Fantič
|
import { getItem, getItemNotes, setConcordances, setSelectedConcordance } from "../stores/actions/itemThunks"
|
5 |
7c4cd2cd
|
Fantič
|
import { login } from "../stores/actions/userThunks"
|
6 |
852de08e
|
Fantič
|
import { NavigationState, SceneRendererProps, TabBar, TabView } from "react-native-tab-view"
|
7 |
b88520f8
|
Fantič
|
import ItemView from "../components/item/ItemView"
|
8 |
852de08e
|
Fantič
|
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 |
b88520f8
|
Fantič
|
|
13 |
97deff21
|
Fantič
|
|
14 |
|
|
interface ItemViewProps {
|
15 |
|
|
itemId: string
|
16 |
b5599ba1
|
Fantič
|
}
|
17 |
|
|
|
18 |
97deff21
|
Fantič
|
const ItemViewPage = (props: ItemViewProps) => {
|
19 |
|
|
|
20 |
3a4bf532
|
Fantič
|
const layout = useWindowDimensions();
|
21 |
|
|
const [routes, setRoutes] = React.useState([
|
22 |
|
|
// initial tab
|
23 |
|
|
{ key: 'loading', title: 'Loading item...' },
|
24 |
|
|
]);
|
25 |
852de08e
|
Fantič
|
const [loadedScenes, setLoadedScenes] = React.useState({});
|
26 |
3a4bf532
|
Fantič
|
|
27 |
97deff21
|
Fantič
|
|
28 |
|
|
|
29 |
852de08e
|
Fantič
|
const dispatch = useDispatch<AppDispatch>();
|
30 |
|
|
|
31 |
|
|
const { item, itemLoading, notes, notesLoading, concordances, selectedConcordance } = useSelector((state: RootState) => state.itemViewState)
|
32 |
97deff21
|
Fantič
|
|
33 |
356c8bce
|
Fantič
|
// initial: load main item
|
34 |
fedb75d6
|
Fantič
|
useEffect(() => {
|
35 |
|
|
// TODO remove login from here
|
36 |
7c4cd2cd
|
Fantič
|
dispatch(login({ username: "Viktorie", password: "Golem123." }));
|
37 |
|
|
dispatch(getItem(props.itemId));
|
38 |
852de08e
|
Fantič
|
}, []);
|
39 |
3a4bf532
|
Fantič
|
|
40 |
97deff21
|
Fantič
|
|
41 |
356c8bce
|
Fantič
|
// concordances are loaded
|
42 |
88d2df9a
|
Fantič
|
useEffect(() => {
|
43 |
3a4bf532
|
Fantič
|
if (item && concordances && concordances.length > 0) {
|
44 |
|
|
let concordanceRoutes = [];
|
45 |
88d2df9a
|
Fantič
|
|
46 |
3a4bf532
|
Fantič
|
for (let i = 0; i < concordances.length; i++) {
|
47 |
|
|
let concordance = concordances[i];
|
48 |
|
|
concordanceRoutes.push({
|
49 |
|
|
key: concordance.id,
|
50 |
|
|
title: concordance.id
|
51 |
|
|
})
|
52 |
|
|
}
|
53 |
|
|
setRoutes(concordanceRoutes);
|
54 |
88d2df9a
|
Fantič
|
}
|
55 |
3a4bf532
|
Fantič
|
}, [concordances]);
|
56 |
88d2df9a
|
Fantič
|
|
57 |
356c8bce
|
Fantič
|
const handleTabChange = (index: number) => {
|
58 |
852de08e
|
Fantič
|
|
59 |
3a4bf532
|
Fantič
|
dispatch(setSelectedConcordance(index));
|
60 |
356c8bce
|
Fantič
|
};
|
61 |
88d2df9a
|
Fantič
|
|
62 |
356c8bce
|
Fantič
|
// selected concordance changes
|
63 |
3a4bf532
|
Fantič
|
useEffect(() => {
|
64 |
|
|
if (concordances && concordances.length > 0) {
|
65 |
|
|
dispatch(getItem(concordances[selectedConcordance].id));
|
66 |
|
|
}
|
67 |
356c8bce
|
Fantič
|
|
68 |
3a4bf532
|
Fantič
|
}, [selectedConcordance]);
|
69 |
88d2df9a
|
Fantič
|
|
70 |
356c8bce
|
Fantič
|
// item changes
|
71 |
|
|
useEffect(() => {
|
72 |
852de08e
|
Fantič
|
if (!itemLoading && item && item.id) {
|
73 |
3a4bf532
|
Fantič
|
if (selectedConcordance == 0) {
|
74 |
|
|
dispatch(setConcordances(item.concordances));
|
75 |
|
|
}
|
76 |
|
|
// set item notes if item is loaded
|
77 |
|
|
if (item && item.id) {
|
78 |
|
|
dispatch(getItemNotes(item.id));
|
79 |
|
|
}
|
80 |
356c8bce
|
Fantič
|
}
|
81 |
|
|
}, [item]);
|
82 |
97deff21
|
Fantič
|
|
83 |
852de08e
|
Fantič
|
// custom render Tab
|
84 |
|
|
const _renderTabBar = (props: { navigationState: { routes: any[] } }) => {
|
85 |
|
|
if (concordances && concordances.length > 0)
|
86 |
|
|
return (
|
87 |
|
|
<View h="16">
|
88 |
|
|
<ScrollView horizontal={true} >
|
89 |
|
|
<HStack>
|
90 |
|
|
{props.navigationState.routes.map((route, i) => {
|
91 |
|
|
return (
|
92 |
|
|
<Button size="lg" key={i}
|
93 |
|
|
variant={selectedConcordance == i ? "subtle" : "outline"}
|
94 |
|
|
rightIcon={<CircleIcon size="2" color={CertaintyWithColors[concordances[i].cert].color} />}
|
95 |
|
|
onPress={() => handleTabChange(i)}
|
96 |
|
|
>
|
97 |
|
|
{concordances[i].id}
|
98 |
|
|
</Button>
|
99 |
|
|
);
|
100 |
|
|
})}
|
101 |
|
|
</HStack>
|
102 |
|
|
|
103 |
|
|
</ScrollView>
|
104 |
|
|
</View>
|
105 |
|
|
)
|
106 |
|
|
else {
|
107 |
|
|
return <LoadingBox text={`Loading concordances...`}></LoadingBox>
|
108 |
|
|
}
|
109 |
|
|
};
|
110 |
|
|
|
111 |
97deff21
|
Fantič
|
return (
|
112 |
852de08e
|
Fantič
|
!concordances || concordances.length < 1 ?
|
113 |
|
|
<LoadingBox text={`Loading item ${props.itemId}...`}></LoadingBox>
|
114 |
|
|
:
|
115 |
|
|
<TabView
|
116 |
|
|
renderTabBar={_renderTabBar}
|
117 |
|
|
navigationState={{ index: selectedConcordance, routes }}
|
118 |
|
|
renderScene={() => (
|
119 |
|
|
<ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} />
|
120 |
|
|
)}
|
121 |
|
|
initialLayout={{ width: layout.width }}
|
122 |
|
|
|
123 |
|
|
// prevent default action on index change
|
124 |
|
|
onIndexChange={handleTabChange}
|
125 |
|
|
|
126 |
|
|
/>
|
127 |
97deff21
|
Fantič
|
);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
852de08e
|
Fantič
|
|
131 |
|
|
|
132 |
3a4bf532
|
Fantič
|
export default ItemViewPage;
|