Projekt

Obecné

Profil

Stáhnout (4.22 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React, { useEffect } from "react"
2
import { useDispatch, useSelector } from "react-redux"
3
import { AppDispatch, RootState } from "../stores/store"
4
import { getItem, getItemNotes, setConcordances } from "../stores/actions/itemThunks"
5
import { login } from "../stores/actions/userThunks"
6
import { TabView } from "react-native-tab-view"
7
import ItemView from "../components/item/ItemView"
8
import { useWindowDimensions } from "react-native"
9
import LoadingBox from "../components/loading/LoadingBox"
10
import ItemTabBar from "../components/item/ItemTabBar"
11
import { log } from "../logging/logger"
12
import { DrawerScreenProps } from "@react-navigation/drawer"
13
import { RootDrawerParamList } from "./Navigation"
14
import { useToast } from "native-base"
15
import { ErrorToast } from "../components/toast/ErrorToast"
16

    
17

    
18
const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList, 'Item'>) => {
19

    
20
  const layout = useWindowDimensions();
21

    
22
  const [routes, setRoutes] = React.useState([
23
    // initial tab
24
    { key: 'loading', title: 'Loading item...' },
25
  ]);
26

    
27
  // // TODO vyřešit, aby nebyly freezy při přepínání
28
  // const [renderSceneDict, setRenderSceneDict] = React.useState({});
29

    
30
  const [index, setIndex] = React.useState(0);
31

    
32
  const dispatch = useDispatch<AppDispatch>();
33

    
34
  const { item, itemLoading, notes, notesLoading, concordances } = useSelector((state: RootState) => state.itemViewState)
35

    
36
  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
  // initial: load main item
54
  useEffect(() => {
55
    dispatch(getItem(route.params.itemId));
56
    log.debug("ItemViewPage", "useEffect", "getItem", route.params.itemId);
57
  }, [])
58

    
59

    
60
  // concordances are loaded
61
  useEffect(() => {
62
    log.debug("ItemViewPage", "useEffect", "concordances", concordances)
63

    
64
    if (item && concordances && concordances.length > 0) {
65
      let concordanceRoutes = [];
66

    
67
      // let _renderSceneDict: any = {};
68

    
69
      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
        // _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
76
      }
77

    
78
      setRoutes(concordanceRoutes);
79
      // setRenderSceneDict(_renderSceneDict);
80
    }
81
  }, [concordances]);
82

    
83
  // item changes
84
  useEffect(() => {
85
    log.debug("ItemViewPage", "useEffect", "item", item)
86

    
87
    if (!itemLoading && item && item.id) {
88
      if (index == 0) {
89
        dispatch(setConcordances(item.concordances));
90
      }
91
      // set item notes if item is loaded
92
      if (item && item.id) {
93
        dispatch(getItemNotes({item: item, relatedComments: true}));
94
      }
95
    }
96
  }, [item]);
97

    
98
  const handleIndexChanged = (newIndex: number) => {
99
    if(newIndex >= 0 && newIndex < concordances.length && index != newIndex){
100
      console.log("index changed");
101
      setIndex(newIndex);
102
      if (concordances && concordances.length > 0) {
103
        dispatch(getItem(concordances[newIndex].id));
104
      }
105
    }
106
  }
107

    
108
  const onBackPressed = () => {
109
    log.debug("back pressed")
110
    navigation.goBack();
111
  }
112

    
113
  return (
114
    !concordances || concordances.length < 1 ?
115
      <LoadingBox text={`Loading item ${route.params.itemId}...`}/>
116
      :
117
      <TabView
118
        renderTabBar={() => ItemTabBar({
119
          navigationState: {
120
            routes: routes,
121
          },
122
          concordances: concordances,
123
          index: index,
124
          onIndexChange: handleIndexChanged,
125
          onBackPressed: onBackPressed,
126
        })}
127

    
128
        navigationState={{ index, routes }}
129
        renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} navigation={navigation}/>}
130

    
131
        initialLayout={{ width: layout.width }}
132
        onIndexChange={handleIndexChanged}
133
      />
134
  );
135
}
136

    
137
export default ItemViewPage;
(2-2/7)