Projekt

Obecné

Profil

Stáhnout (5.11 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 { RootStackParamList } from "./Navigation"
14
import { useToast } from "native-base"
15
import { ErrorToast } from "../components/toast/ErrorToast"
16

    
17

    
18
const ItemViewPage = ({ route, navigation }: DrawerScreenProps<RootStackParamList, '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, swipe?: boolean) => {
100
    // if(swipe && item){
101
    //   log.debug("swipe change")
102
    //   console.log(newIndex)
103
    //   console.log(index)
104
    //   if(newIndex > index){
105
    //     // next items
106
    //     if(item.nextItem){
107
    //       navigation.navigate("Item", { itemId: item.nextItem.toString() })
108
    //     }
109
    //     else{
110
    //       log.debug("no next item")
111
    //     }
112
    //   }
113
    //   else if(newIndex < index){
114
    //     // previous items
115
    //     if(item.prevItem){
116
    //       navigation.navigate("Item", { itemId: item.prevItem.toString() })
117
    //     }
118
    //     else{
119
    //       log.debug("no prev item")
120
    //     }
121
    //   }
122
    // }
123
    // else 
124
    if (newIndex >= 0 && newIndex < concordances.length && index != newIndex) {
125
      console.log("index changed");
126
      setIndex(newIndex);
127
      if (concordances && concordances.length > 0) {
128
        dispatch(getItem(concordances[newIndex].id ?? ""));
129
      }
130
    }
131
  }, [concordances, index]);
132

    
133
  const onBackPressed = () => {
134
    log.debug("back pressed")
135
    navigation.goBack();
136
  }
137

    
138
  return (
139
    !concordances || concordances.length < 1 ?
140
      <LoadingBox text={`Loading item ${route.params.itemId}...`} />
141
      :
142
      <TabView
143
        renderTabBar={() => ItemTabBar({
144
          navigationState: {
145
            routes: routes,
146
          },
147
          concordances: concordances,
148
          index: index,
149
          onIndexChange: handleIndexChanged,
150
          onBackPressed: onBackPressed,
151
          navigation: navigation,
152
          prevItem: item.prevItem,
153
          nextItem: item.nextItem,
154
        })}
155

    
156
        navigationState={{ index: index, routes }}
157
        renderScene={() => <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} navigation={navigation} />}
158

    
159
        initialLayout={{ width: layout.width }}
160
        onIndexChange={(index: number) => handleIndexChanged(index, true)}
161
      />
162
  );
163
}
164

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