1 |
8ff3394e
|
Fantič
|
import React, { useCallback, useEffect } from "react"
|
2 |
97deff21
|
Fantič
|
import { useDispatch, useSelector } from "react-redux"
|
3 |
fedb75d6
|
Fantič
|
import { AppDispatch, RootState } from "../stores/store"
|
4 |
d281786c
|
Fantič
|
import { getItem, getItemNotes, setConcordances } from "../stores/actions/itemThunks"
|
5 |
7c4cd2cd
|
Fantič
|
import { login } from "../stores/actions/userThunks"
|
6 |
1054fe09
|
Fantič
|
import { TabView } from "react-native-tab-view"
|
7 |
b88520f8
|
Fantič
|
import ItemView from "../components/item/ItemView"
|
8 |
852de08e
|
Fantič
|
import { useWindowDimensions } from "react-native"
|
9 |
a7a9a204
|
Fantič
|
import LoadingBox from "../components/loading/LoadingBox"
|
10 |
d281786c
|
Fantič
|
import ItemTabBar from "../components/item/ItemTabBar"
|
11 |
04928342
|
Schwobik
|
import { log } from "../logging/logger"
|
12 |
|
|
import { DrawerScreenProps } from "@react-navigation/drawer"
|
13 |
4da7b143
|
Schwobik
|
import { RootDrawerParamList } from "./Navigation"
|
14 |
f386a4fe
|
Fantič
|
import { useToast } from "native-base"
|
15 |
|
|
import { ErrorToast } from "../components/toast/ErrorToast"
|
16 |
b88520f8
|
Fantič
|
|
17 |
b5599ba1
|
Fantič
|
|
18 |
8ff3394e
|
Fantič
|
const ItemViewPage = ({ route, navigation }: DrawerScreenProps<RootDrawerParamList, 'Item'>) => {
|
19 |
97deff21
|
Fantič
|
|
20 |
3a4bf532
|
Fantič
|
const layout = useWindowDimensions();
|
21 |
d281786c
|
Fantič
|
|
22 |
8ff3394e
|
Fantič
|
const [routes, setRoutes] = React.useState<{ key: string, title: string }[]>([
|
23 |
3a4bf532
|
Fantič
|
// initial tab
|
24 |
|
|
{ key: 'loading', title: 'Loading item...' },
|
25 |
|
|
]);
|
26 |
|
|
|
27 |
d281786c
|
Fantič
|
// // TODO vyřešit, aby nebyly freezy při přepínání
|
28 |
|
|
// const [renderSceneDict, setRenderSceneDict] = React.useState({});
|
29 |
97deff21
|
Fantič
|
|
30 |
d281786c
|
Fantič
|
const [index, setIndex] = React.useState(0);
|
31 |
97deff21
|
Fantič
|
|
32 |
852de08e
|
Fantič
|
const dispatch = useDispatch<AppDispatch>();
|
33 |
|
|
|
34 |
d281786c
|
Fantič
|
const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
|
35 |
97deff21
|
Fantič
|
|
36 |
f386a4fe
|
Fantič
|
const lastError = useSelector((state: RootState) => state.itemViewState.lastError)
|
37 |
|
|
const toast = useToast();
|
38 |
|
|
|
39 |
8ff3394e
|
Fantič
|
|
40 |
f386a4fe
|
Fantič
|
useEffect(() => {
|
41 |
8ff3394e
|
Fantič
|
if (lastError) {
|
42 |
|
|
toast.closeAll()
|
43 |
|
|
toast.show({
|
44 |
|
|
render: ({
|
45 |
|
|
id
|
46 |
|
|
}) => {
|
47 |
|
|
return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
|
48 |
|
|
},
|
49 |
|
|
duration: 3000
|
50 |
|
|
});
|
51 |
|
|
}
|
52 |
f386a4fe
|
Fantič
|
}, [lastError])
|
53 |
|
|
|
54 |
356c8bce
|
Fantič
|
// initial: load main item
|
55 |
fedb75d6
|
Fantič
|
useEffect(() => {
|
56 |
1c70def7
|
Fantič
|
dispatch(getItem(route.params.itemId));
|
57 |
|
|
log.debug("ItemViewPage", "useEffect", "getItem", route.params.itemId);
|
58 |
c1161e82
|
Fantič
|
}, [route.params.itemId])
|
59 |
97deff21
|
Fantič
|
|
60 |
356c8bce
|
Fantič
|
// concordances are loaded
|
61 |
88d2df9a
|
Fantič
|
useEffect(() => {
|
62 |
04928342
|
Schwobik
|
log.debug("ItemViewPage", "useEffect", "concordances", concordances)
|
63 |
|
|
|
64 |
3a4bf532
|
Fantič
|
if (item && concordances && concordances.length > 0) {
|
65 |
|
|
let concordanceRoutes = [];
|
66 |
88d2df9a
|
Fantič
|
|
67 |
d281786c
|
Fantič
|
// let _renderSceneDict: any = {};
|
68 |
|
|
|
69 |
3a4bf532
|
Fantič
|
for (let i = 0; i < concordances.length; i++) {
|
70 |
|
|
let concordance = concordances[i];
|
71 |
|
|
concordanceRoutes.push({
|
72 |
c1161e82
|
Fantič
|
key: concordance.id ?? "",
|
73 |
|
|
title: concordance.id ?? ""
|
74 |
3a4bf532
|
Fantič
|
})
|
75 |
d281786c
|
Fantič
|
// _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
|
76 |
3a4bf532
|
Fantič
|
}
|
77 |
d281786c
|
Fantič
|
|
78 |
3a4bf532
|
Fantič
|
setRoutes(concordanceRoutes);
|
79 |
d281786c
|
Fantič
|
// setRenderSceneDict(_renderSceneDict);
|
80 |
88d2df9a
|
Fantič
|
}
|
81 |
3a4bf532
|
Fantič
|
}, [concordances]);
|
82 |
88d2df9a
|
Fantič
|
|
83 |
356c8bce
|
Fantič
|
// item changes
|
84 |
|
|
useEffect(() => {
|
85 |
04928342
|
Schwobik
|
log.debug("ItemViewPage", "useEffect", "item", item)
|
86 |
|
|
|
87 |
852de08e
|
Fantič
|
if (!itemLoading && item && item.id) {
|
88 |
d281786c
|
Fantič
|
if (index == 0) {
|
89 |
c1161e82
|
Fantič
|
dispatch(setConcordances(item.concordances ?? []));
|
90 |
3a4bf532
|
Fantič
|
}
|
91 |
|
|
// set item notes if item is loaded
|
92 |
|
|
if (item && item.id) {
|
93 |
8ff3394e
|
Fantič
|
dispatch(getItemNotes({ item: item, relatedComments: true }));
|
94 |
3a4bf532
|
Fantič
|
}
|
95 |
356c8bce
|
Fantič
|
}
|
96 |
|
|
}, [item]);
|
97 |
97deff21
|
Fantič
|
|
98 |
8ff3394e
|
Fantič
|
|
99 |
|
|
const handleIndexChanged = useCallback((newIndex: number) => {
|
100 |
|
|
if (newIndex >= 0 && newIndex < concordances.length && index != newIndex) {
|
101 |
cb25df10
|
Fantič
|
console.log("index changed");
|
102 |
|
|
setIndex(newIndex);
|
103 |
1054fe09
|
Fantič
|
if (concordances && concordances.length > 0) {
|
104 |
c1161e82
|
Fantič
|
dispatch(getItem(concordances[newIndex].id ?? ""));
|
105 |
1054fe09
|
Fantič
|
}
|
106 |
a7a9a204
|
Fantič
|
}
|
107 |
8ff3394e
|
Fantič
|
}, [concordances, index]);
|
108 |
a7a9a204
|
Fantič
|
|
109 |
04928342
|
Schwobik
|
const onBackPressed = () => {
|
110 |
|
|
log.debug("back pressed")
|
111 |
|
|
navigation.goBack();
|
112 |
|
|
}
|
113 |
|
|
|
114 |
97deff21
|
Fantič
|
return (
|
115 |
852de08e
|
Fantič
|
!concordances || concordances.length < 1 ?
|
116 |
8ff3394e
|
Fantič
|
<LoadingBox text={`Loading item ${route.params.itemId}...`} />
|
117 |
852de08e
|
Fantič
|
:
|
118 |
|
|
<TabView
|
119 |
d281786c
|
Fantič
|
renderTabBar={() => ItemTabBar({
|
120 |
|
|
navigationState: {
|
121 |
|
|
routes: routes,
|
122 |
|
|
},
|
123 |
|
|
concordances: concordances,
|
124 |
|
|
index: index,
|
125 |
1054fe09
|
Fantič
|
onIndexChange: handleIndexChanged,
|
126 |
04928342
|
Schwobik
|
onBackPressed: onBackPressed,
|
127 |
8ff3394e
|
Fantič
|
navigation: navigation,
|
128 |
|
|
prevItem: item.prevItem,
|
129 |
|
|
nextItem: item.nextItem,
|
130 |
d281786c
|
Fantič
|
})}
|
131 |
|
|
|
132 |
|
|
navigationState={{ index, routes }}
|
133 |
8ff3394e
|
Fantič
|
renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} navigation={navigation} />}
|
134 |
852de08e
|
Fantič
|
|
135 |
d281786c
|
Fantič
|
initialLayout={{ width: layout.width }}
|
136 |
a7a9a204
|
Fantič
|
onIndexChange={handleIndexChanged}
|
137 |
852de08e
|
Fantič
|
/>
|
138 |
97deff21
|
Fantič
|
);
|
139 |
|
|
}
|
140 |
|
|
|
141 |
3a4bf532
|
Fantič
|
export default ItemViewPage;
|