Projekt

Obecné

Profil

Stáhnout (3.42 KB) Statistiky
| Větev: | Tag: | Revize:
1 d281786c Fantič
import React, { ReactNode, 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 d281786c Fantič
import { NavigationState, SceneMap, SceneRendererProps, TabBar, TabView } from "react-native-tab-view"
7 b88520f8 Fantič
import ItemView from "../components/item/ItemView"
8 852de08e Fantič
import { Button, Pressable, View, Text, Icon, CircleIcon, ScrollView, HStack, VStack } from "native-base"
9
import { useWindowDimensions } from "react-native"
10 a7a9a204 Fantič
import LoadingBox from "../components/loading/LoadingBox"
11 852de08e Fantič
import { CertaintyWithColors } from "../stores/reducers/itemSlice"
12 d281786c Fantič
import ItemTabBar from "../components/item/ItemTabBar"
13 b88520f8 Fantič
14 97deff21 Fantič
15
interface ItemViewProps {
16
  itemId: string
17 b5599ba1 Fantič
}
18
19 d281786c Fantič
20 97deff21 Fantič
const ItemViewPage = (props: ItemViewProps) => {
21
22 3a4bf532 Fantič
  const layout = useWindowDimensions();
23 d281786c Fantič
24 3a4bf532 Fantič
  const [routes, setRoutes] = React.useState([
25
    // initial tab
26
    { key: 'loading', title: 'Loading item...' },
27
  ]);
28
29 d281786c Fantič
  // // TODO vyřešit, aby nebyly freezy při přepínání
30
  // const [renderSceneDict, setRenderSceneDict] = React.useState({});
31 97deff21 Fantič
32 d281786c Fantič
  const [index, setIndex] = React.useState(0);
33 97deff21 Fantič
34 852de08e Fantič
  const dispatch = useDispatch<AppDispatch>();
35
36 d281786c Fantič
  const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
37 97deff21 Fantič
38 356c8bce Fantič
  // initial: load main item
39 fedb75d6 Fantič
  useEffect(() => {
40
    // TODO remove login from here
41 7c4cd2cd Fantič
    dispatch(login({ username: "Viktorie", password: "Golem123." }));
42
    dispatch(getItem(props.itemId));
43 852de08e Fantič
  }, []);
44 3a4bf532 Fantič
45 97deff21 Fantič
46 356c8bce Fantič
  // concordances are loaded
47 88d2df9a Fantič
  useEffect(() => {
48 3a4bf532 Fantič
    if (item && concordances && concordances.length > 0) {
49
      let concordanceRoutes = [];
50 88d2df9a Fantič
51 d281786c Fantič
      // let _renderSceneDict: any = {};
52
53 3a4bf532 Fantič
      for (let i = 0; i < concordances.length; i++) {
54
        let concordance = concordances[i];
55
        concordanceRoutes.push({
56
          key: concordance.id,
57
          title: concordance.id
58
        })
59 d281786c Fantič
        // _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
60 3a4bf532 Fantič
      }
61 d281786c Fantič
62 3a4bf532 Fantič
      setRoutes(concordanceRoutes);
63 d281786c Fantič
      // setRenderSceneDict(_renderSceneDict);
64 88d2df9a Fantič
    }
65 3a4bf532 Fantič
  }, [concordances]);
66 88d2df9a Fantič
67 356c8bce Fantič
  // item changes
68
  useEffect(() => {
69 852de08e Fantič
    if (!itemLoading && item && item.id) {
70 d281786c Fantič
      if (index == 0) {
71 3a4bf532 Fantič
        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
  }, [item]);
79 97deff21 Fantič
80 a7a9a204 Fantič
  const handleIndexChanged = (index: number) => {
81
    console.log("index changed");
82
    setIndex(index);
83
    if (concordances && concordances.length > 0) {
84
      dispatch(getItem(concordances[index].id));
85
    }
86
  }
87
88 97deff21 Fantič
  return (
89 852de08e Fantič
    !concordances || concordances.length < 1 ?
90
      <LoadingBox text={`Loading item ${props.itemId}...`}></LoadingBox>
91
      :
92
      <TabView
93 d281786c Fantič
        renderTabBar={() => ItemTabBar({
94
          navigationState: {
95
            routes: routes,
96
          },
97
          concordances: concordances,
98
          index: index,
99 a7a9a204 Fantič
          onIndexChange: handleIndexChanged
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 852de08e Fantič
        // prevent default action on index change
107 a7a9a204 Fantič
        onIndexChange={handleIndexChanged}
108 852de08e Fantič
109
      />
110 97deff21 Fantič
  );
111
}
112
113 852de08e Fantič
114
115 3a4bf532 Fantič
export default ItemViewPage;