Projekt

Obecné

Profil

Stáhnout (4.4 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React, { useCallback, 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<{ key: string, title: string }[]>([
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

    
40
  useEffect(() => {
41
    if (lastError) {
42
      toast.closeAll()
43
      toast.show({
44
        render: ({
45
          id
46
        }) => {
47
          return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
48
        },
49
        duration: 3000
50
      });
51
    }
52
  }, [lastError])
53

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

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

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

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

    
132
        navigationState={{ index, routes }}
133
        renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} navigation={navigation} />}
134

    
135
        initialLayout={{ width: layout.width }}
136
        onIndexChange={handleIndexChanged}
137
      />
138
  );
139
}
140

    
141
export default ItemViewPage;
(2-2/8)