1
|
import { Center, Box, Heading, VStack, FormControl, Link, Input, Button, HStack, Text, StatusBar, Container, Divider, Spacer, Row, View } from "native-base"
|
2
|
import React, { useState, useEffect } from "react"
|
3
|
import { useDispatch, useSelector } from "react-redux"
|
4
|
import { AppDispatch, RootState } from "../stores/store"
|
5
|
import { getItem, getItemNotes, setConcordances, setSelectedConcordance } from "../stores/actions/itemThunks"
|
6
|
import { Item, ItemViewState } from "../types/item"
|
7
|
import { login } from "../stores/actions/userThunks"
|
8
|
import { SceneRendererProps, TabView } from "react-native-tab-view"
|
9
|
import { useWindowDimensions } from "react-native"
|
10
|
import ItemDetail from "../components/item/ItemDetail"
|
11
|
|
12
|
interface ItemViewProps {
|
13
|
itemId: string
|
14
|
}
|
15
|
|
16
|
const ItemViewPage = (props: ItemViewProps) => {
|
17
|
|
18
|
const layout = useWindowDimensions();
|
19
|
|
20
|
const [routes, setRoutes] = React.useState([
|
21
|
// initial tab
|
22
|
{ key: 'loading', title: 'Loading item...' },
|
23
|
]);
|
24
|
|
25
|
const dispatch = useDispatch<AppDispatch>();
|
26
|
|
27
|
const { item, notes, concordances, selectedConcordance } = useSelector((state: RootState) => state.itemViewState)
|
28
|
|
29
|
|
30
|
// initial: load main item
|
31
|
useEffect(() => {
|
32
|
// TODO remove login from here
|
33
|
dispatch(login({ username: "Viktorie", password: "Golem123." }));
|
34
|
dispatch(getItem(props.itemId));
|
35
|
}, [props.itemId]);
|
36
|
|
37
|
|
38
|
// concordances are loaded
|
39
|
useEffect(() => {
|
40
|
if (item && concordances && concordances.length > 0) {
|
41
|
let concordanceRoutes = [];
|
42
|
|
43
|
for (let i = 0; i < concordances.length; i++) {
|
44
|
let concordance = concordances[i];
|
45
|
concordanceRoutes.push({
|
46
|
key: concordance.id,
|
47
|
title: concordance.id
|
48
|
})
|
49
|
}
|
50
|
setRoutes(concordanceRoutes);
|
51
|
}
|
52
|
}, [concordances]);
|
53
|
|
54
|
const handleTabChange = (index: number) => {
|
55
|
dispatch(setSelectedConcordance(index));
|
56
|
};
|
57
|
|
58
|
// selected concordance changes
|
59
|
useEffect(() => {
|
60
|
if (concordances && concordances.length > 0) {
|
61
|
dispatch(getItem(concordances[selectedConcordance].id));
|
62
|
}
|
63
|
|
64
|
}, [selectedConcordance]);
|
65
|
|
66
|
// item changes
|
67
|
useEffect(() => {
|
68
|
|
69
|
if (item && item.id) {
|
70
|
if (selectedConcordance == 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
|
|
79
|
}, [item]);
|
80
|
|
81
|
return (
|
82
|
<TabView
|
83
|
navigationState={{ index: selectedConcordance, routes }}
|
84
|
renderScene={() => <ItemDetail item={item} />}
|
85
|
onIndexChange={handleTabChange}
|
86
|
initialLayout={{ width: layout.width }}
|
87
|
/>
|
88
|
);
|
89
|
}
|
90
|
|
91
|
export default ItemViewPage;
|