Projekt

Obecné

Profil

Stáhnout (3.64 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 04928342 Schwobik
import { log } from "../logging/logger"
12
import { DrawerScreenProps } from "@react-navigation/drawer"
13 4da7b143 Schwobik
import { RootDrawerParamList } from "./Navigation"
14 b88520f8 Fantič
15 b5599ba1 Fantič
16 04928342 Schwobik
const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList, 'Item'>) => {
17 97deff21 Fantič
18 3a4bf532 Fantič
  const layout = useWindowDimensions();
19 d281786c Fantič
20 3a4bf532 Fantič
  const [routes, setRoutes] = React.useState([
21
    // initial tab
22
    { key: 'loading', title: 'Loading item...' },
23
  ]);
24
25 d281786c Fantič
  // // TODO vyřešit, aby nebyly freezy při přepínání
26
  // const [renderSceneDict, setRenderSceneDict] = React.useState({});
27 97deff21 Fantič
28 d281786c Fantič
  const [index, setIndex] = React.useState(0);
29 97deff21 Fantič
30 852de08e Fantič
  const dispatch = useDispatch<AppDispatch>();
31
32 d281786c Fantič
  const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
33 97deff21 Fantič
34 356c8bce Fantič
  // initial: load main item
35 fedb75d6 Fantič
  useEffect(() => {
36 1c70def7 Fantič
    dispatch(getItem(route.params.itemId));
37
    log.debug("ItemViewPage", "useEffect", "getItem", route.params.itemId);
38 04928342 Schwobik
  }, [])
39 3a4bf532 Fantič
40 97deff21 Fantič
41 356c8bce Fantič
  // concordances are loaded
42 88d2df9a Fantič
  useEffect(() => {
43 04928342 Schwobik
    log.debug("ItemViewPage", "useEffect", "concordances", concordances)
44
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 04928342 Schwobik
    log.debug("ItemViewPage", "useEffect", "item", item)
67
68 852de08e Fantič
    if (!itemLoading && item && item.id) {
69 d281786c Fantič
      if (index == 0) {
70 3a4bf532 Fantič
        dispatch(setConcordances(item.concordances));
71
      }
72
      // set item notes if item is loaded
73
      if (item && item.id) {
74
        dispatch(getItemNotes(item.id));
75
      }
76 356c8bce Fantič
    }
77
  }, [item]);
78 97deff21 Fantič
79 cb25df10 Fantič
  const handleIndexChanged = (newIndex: number) => {
80
    if(newIndex >= 0 && newIndex < concordances.length && index != newIndex){
81
      console.log("index changed");
82
      setIndex(newIndex);
83 1054fe09 Fantič
      if (concordances && concordances.length > 0) {
84 cb25df10 Fantič
        dispatch(getItem(concordances[newIndex].id));
85 1054fe09 Fantič
      }
86 a7a9a204 Fantič
    }
87
  }
88
89 04928342 Schwobik
  const onBackPressed = () => {
90
    log.debug("back pressed")
91
    navigation.goBack();
92
  }
93
94 97deff21 Fantič
  return (
95 852de08e Fantič
    !concordances || concordances.length < 1 ?
96 04928342 Schwobik
      <LoadingBox text={`Loading item ${route.params.itemId}...`}/>
97 852de08e Fantič
      :
98
      <TabView
99 d281786c Fantič
        renderTabBar={() => ItemTabBar({
100
          navigationState: {
101
            routes: routes,
102
          },
103
          concordances: concordances,
104
          index: index,
105 1054fe09 Fantič
          onIndexChange: handleIndexChanged,
106 04928342 Schwobik
          onBackPressed: onBackPressed,
107 d281786c Fantič
        })}
108
109
        navigationState={{ index, routes }}
110 1c70def7 Fantič
        renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} navigation={navigation}/>}
111 852de08e Fantič
112 d281786c Fantič
        initialLayout={{ width: layout.width }}
113 a7a9a204 Fantič
        onIndexChange={handleIndexChanged}
114 852de08e Fantič
      />
115 97deff21 Fantič
  );
116
}
117
118 3a4bf532 Fantič
export default ItemViewPage;