Projekt

Obecné

Profil

Stáhnout (3.3 KB) Statistiky
| Větev: | Tag: | Revize:
1 1054fe09 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 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 b88520f8 Fantič
12 97deff21 Fantič
interface ItemViewProps {
13 1054fe09 Fantič
  itemId: string,
14
  backPressed: any
15 b5599ba1 Fantič
}
16
17 97deff21 Fantič
const ItemViewPage = (props: ItemViewProps) => {
18
19 3a4bf532 Fantič
  const layout = useWindowDimensions();
20 d281786c Fantič
21 3a4bf532 Fantič
  const [routes, setRoutes] = React.useState([
22
    // initial tab
23
    { key: 'loading', title: 'Loading item...' },
24
  ]);
25
26 d281786c Fantič
  // // TODO vyřešit, aby nebyly freezy při přepínání
27
  // const [renderSceneDict, setRenderSceneDict] = React.useState({});
28 97deff21 Fantič
29 d281786c Fantič
  const [index, setIndex] = React.useState(0);
30 97deff21 Fantič
31 852de08e Fantič
  const dispatch = useDispatch<AppDispatch>();
32
33 d281786c Fantič
  const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
34 97deff21 Fantič
35 356c8bce Fantič
  // initial: load main item
36 fedb75d6 Fantič
  useEffect(() => {
37
    // TODO remove login from here
38 7c4cd2cd Fantič
    dispatch(login({ username: "Viktorie", password: "Golem123." }));
39
    dispatch(getItem(props.itemId));
40 852de08e Fantič
  }, []);
41 3a4bf532 Fantič
42 97deff21 Fantič
43 356c8bce Fantič
  // concordances are loaded
44 88d2df9a Fantič
  useEffect(() => {
45 3a4bf532 Fantič
    if (item && concordances && concordances.length > 0) {
46
      let concordanceRoutes = [];
47 88d2df9a Fantič
48 d281786c Fantič
      // let _renderSceneDict: any = {};
49
50 3a4bf532 Fantič
      for (let i = 0; i < concordances.length; i++) {
51
        let concordance = concordances[i];
52
        concordanceRoutes.push({
53
          key: concordance.id,
54
          title: concordance.id
55
        })
56 d281786c Fantič
        // _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
57 3a4bf532 Fantič
      }
58 d281786c Fantič
59 3a4bf532 Fantič
      setRoutes(concordanceRoutes);
60 d281786c Fantič
      // setRenderSceneDict(_renderSceneDict);
61 88d2df9a Fantič
    }
62 3a4bf532 Fantič
  }, [concordances]);
63 88d2df9a Fantič
64 356c8bce Fantič
  // item changes
65
  useEffect(() => {
66 852de08e Fantič
    if (!itemLoading && item && item.id) {
67 d281786c Fantič
      if (index == 0) {
68 3a4bf532 Fantič
        dispatch(setConcordances(item.concordances));
69
      }
70
      // set item notes if item is loaded
71
      if (item && item.id) {
72
        dispatch(getItemNotes(item.id));
73
      }
74 356c8bce Fantič
    }
75
  }, [item]);
76 97deff21 Fantič
77 cb25df10 Fantič
  const handleIndexChanged = (newIndex: number) => {
78
    if(newIndex >= 0 && newIndex < concordances.length && index != newIndex){
79
      console.log("index changed");
80
      setIndex(newIndex);
81 1054fe09 Fantič
      if (concordances && concordances.length > 0) {
82 cb25df10 Fantič
        dispatch(getItem(concordances[newIndex].id));
83 1054fe09 Fantič
      }
84 a7a9a204 Fantič
    }
85
  }
86
87 97deff21 Fantič
  return (
88 852de08e Fantič
    !concordances || concordances.length < 1 ?
89
      <LoadingBox text={`Loading item ${props.itemId}...`}></LoadingBox>
90
      :
91
      <TabView
92 d281786c Fantič
        renderTabBar={() => ItemTabBar({
93
          navigationState: {
94
            routes: routes,
95
          },
96
          concordances: concordances,
97
          index: index,
98 1054fe09 Fantič
          onIndexChange: handleIndexChanged,
99
          onBackPressed: props.backPressed,
100 d281786c Fantič
        })}
101
102
        navigationState={{ index, routes }}
103
        renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} />}
104 852de08e Fantič
105 d281786c Fantič
        initialLayout={{ width: layout.width }}
106 a7a9a204 Fantič
        onIndexChange={handleIndexChanged}
107 852de08e Fantič
      />
108 97deff21 Fantič
  );
109
}
110
111 3a4bf532 Fantič
export default ItemViewPage;