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