Projekt

Obecné

Profil

Stáhnout (4.22 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 f386a4fe Fantič
import { useToast } from "native-base"
15
import { ErrorToast } from "../components/toast/ErrorToast"
16 b88520f8 Fantič
17 b5599ba1 Fantič
18 04928342 Schwobik
const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList, 'Item'>) => {
19 97deff21 Fantič
20 3a4bf532 Fantič
  const layout = useWindowDimensions();
21 d281786c Fantič
22 3a4bf532 Fantič
  const [routes, setRoutes] = React.useState([
23
    // initial tab
24
    { key: 'loading', title: 'Loading item...' },
25
  ]);
26
27 d281786c Fantič
  // // TODO vyřešit, aby nebyly freezy při přepínání
28
  // const [renderSceneDict, setRenderSceneDict] = React.useState({});
29 97deff21 Fantič
30 d281786c Fantič
  const [index, setIndex] = React.useState(0);
31 97deff21 Fantič
32 852de08e Fantič
  const dispatch = useDispatch<AppDispatch>();
33
34 d281786c Fantič
  const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
35 97deff21 Fantič
36 f386a4fe Fantič
  const lastError = useSelector((state: RootState) => state.itemViewState.lastError)
37
  const toast = useToast();
38
39
  useEffect(() => {
40
      if (lastError) {
41
          toast.closeAll()
42
          toast.show({
43
              render: ({
44
                  id
45
              }) => {
46
                  return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
47
              },
48
              duration: 3000
49
          });
50
      }
51
  }, [lastError])
52
53 356c8bce Fantič
  // initial: load main item
54 fedb75d6 Fantič
  useEffect(() => {
55 1c70def7 Fantič
    dispatch(getItem(route.params.itemId));
56
    log.debug("ItemViewPage", "useEffect", "getItem", route.params.itemId);
57 04928342 Schwobik
  }, [])
58 3a4bf532 Fantič
59 97deff21 Fantič
60 356c8bce Fantič
  // concordances are loaded
61 88d2df9a Fantič
  useEffect(() => {
62 04928342 Schwobik
    log.debug("ItemViewPage", "useEffect", "concordances", concordances)
63
64 3a4bf532 Fantič
    if (item && concordances && concordances.length > 0) {
65
      let concordanceRoutes = [];
66 88d2df9a Fantič
67 d281786c Fantič
      // let _renderSceneDict: any = {};
68
69 3a4bf532 Fantič
      for (let i = 0; i < concordances.length; i++) {
70
        let concordance = concordances[i];
71
        concordanceRoutes.push({
72
          key: concordance.id,
73
          title: concordance.id
74
        })
75 d281786c Fantič
        // _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
76 3a4bf532 Fantič
      }
77 d281786c Fantič
78 3a4bf532 Fantič
      setRoutes(concordanceRoutes);
79 d281786c Fantič
      // setRenderSceneDict(_renderSceneDict);
80 88d2df9a Fantič
    }
81 3a4bf532 Fantič
  }, [concordances]);
82 88d2df9a Fantič
83 356c8bce Fantič
  // item changes
84
  useEffect(() => {
85 04928342 Schwobik
    log.debug("ItemViewPage", "useEffect", "item", item)
86
87 852de08e Fantič
    if (!itemLoading && item && item.id) {
88 d281786c Fantič
      if (index == 0) {
89 3a4bf532 Fantič
        dispatch(setConcordances(item.concordances));
90
      }
91
      // set item notes if item is loaded
92
      if (item && item.id) {
93 d3c12593 Fantič
        dispatch(getItemNotes({item: item, relatedComments: true}));
94 3a4bf532 Fantič
      }
95 356c8bce Fantič
    }
96
  }, [item]);
97 97deff21 Fantič
98 cb25df10 Fantič
  const handleIndexChanged = (newIndex: number) => {
99
    if(newIndex >= 0 && newIndex < concordances.length && index != newIndex){
100
      console.log("index changed");
101
      setIndex(newIndex);
102 1054fe09 Fantič
      if (concordances && concordances.length > 0) {
103 cb25df10 Fantič
        dispatch(getItem(concordances[newIndex].id));
104 1054fe09 Fantič
      }
105 a7a9a204 Fantič
    }
106
  }
107
108 04928342 Schwobik
  const onBackPressed = () => {
109
    log.debug("back pressed")
110
    navigation.goBack();
111
  }
112
113 97deff21 Fantič
  return (
114 852de08e Fantič
    !concordances || concordances.length < 1 ?
115 04928342 Schwobik
      <LoadingBox text={`Loading item ${route.params.itemId}...`}/>
116 852de08e Fantič
      :
117
      <TabView
118 d281786c Fantič
        renderTabBar={() => ItemTabBar({
119
          navigationState: {
120
            routes: routes,
121
          },
122
          concordances: concordances,
123
          index: index,
124 1054fe09 Fantič
          onIndexChange: handleIndexChanged,
125 04928342 Schwobik
          onBackPressed: onBackPressed,
126 d281786c Fantič
        })}
127
128
        navigationState={{ index, routes }}
129 1c70def7 Fantič
        renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} navigation={navigation}/>}
130 852de08e Fantič
131 d281786c Fantič
        initialLayout={{ width: layout.width }}
132 a7a9a204 Fantič
        onIndexChange={handleIndexChanged}
133 852de08e Fantič
      />
134 97deff21 Fantič
  );
135
}
136
137 3a4bf532 Fantič
export default ItemViewPage;