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