Projekt

Obecné

Profil

Stáhnout (3.92 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, setSelectedConcordance } from "../stores/actions/itemThunks"
5
import { login } from "../stores/actions/userThunks"
6
import { NavigationState, SceneRendererProps, TabBar, TabView } from "react-native-tab-view"
7
import ItemView from "../components/item/ItemView"
8
import { Button, Pressable, View, Text, Icon, CircleIcon, ScrollView, HStack, VStack } from "native-base"
9
import { useWindowDimensions } from "react-native"
10
import LoadingBox from "../components/loading/loadingBox"
11
import { CertaintyWithColors } from "../stores/reducers/itemSlice"
12

    
13

    
14
interface ItemViewProps {
15
  itemId: string
16
}
17

    
18
const ItemViewPage = (props: ItemViewProps) => {
19

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

    
27

    
28

    
29
  const dispatch = useDispatch<AppDispatch>();
30

    
31
  const { item, itemLoading, notes, notesLoading, concordances, selectedConcordance } = useSelector((state: RootState) => state.itemViewState)
32

    
33
  // initial: load main item
34
  useEffect(() => {
35
    // TODO remove login from here
36
    dispatch(login({ username: "Viktorie", password: "Golem123." }));
37
    dispatch(getItem(props.itemId));
38
  }, []);
39

    
40

    
41
  // concordances are loaded
42
  useEffect(() => {
43
    if (item && concordances && concordances.length > 0) {
44
      let concordanceRoutes = [];
45

    
46
      for (let i = 0; i < concordances.length; i++) {
47
        let concordance = concordances[i];
48
        concordanceRoutes.push({
49
          key: concordance.id,
50
          title: concordance.id
51
        })
52
      }
53
      setRoutes(concordanceRoutes);
54
    }
55
  }, [concordances]);
56

    
57
  const handleTabChange = (index: number) => {
58
  
59
    dispatch(setSelectedConcordance(index));
60
  };
61

    
62
  // selected concordance changes
63
  useEffect(() => {
64
    if (concordances && concordances.length > 0) {
65
      dispatch(getItem(concordances[selectedConcordance].id));
66
    }
67

    
68
  }, [selectedConcordance]);
69

    
70
  // item changes
71
  useEffect(() => {
72
    if (!itemLoading && item && item.id) {
73
      if (selectedConcordance == 0) {
74
        dispatch(setConcordances(item.concordances));
75
      }
76
      // set item notes if item is loaded
77
      if (item && item.id) {
78
        dispatch(getItemNotes(item.id));
79
      }
80
    }
81
  }, [item]);
82

    
83
  // custom render Tab
84
  const _renderTabBar = (props: { navigationState: { routes: any[] } }) => {
85
    if (concordances && concordances.length > 0)
86
      return (
87
        <View h="16">
88
          <ScrollView horizontal={true} >
89
            <HStack>
90
              {props.navigationState.routes.map((route, i) => {
91
                return (
92
                  <Button size="lg" key={i}
93
                    variant={selectedConcordance == i ? "subtle" : "outline"}
94
                    rightIcon={<CircleIcon size="2" color={CertaintyWithColors[concordances[i].cert].color} />}
95
                    onPress={() => handleTabChange(i)}
96
                  >
97
                    {concordances[i].id}
98
                  </Button>
99
                );
100
              })}
101
            </HStack>
102

    
103
          </ScrollView>
104
        </View>
105
      )
106
    else {
107
      return <LoadingBox text={`Loading concordances...`}></LoadingBox>
108
    }
109
  };
110

    
111
  return (
112
    !concordances || concordances.length < 1 ?
113
      <LoadingBox text={`Loading item ${props.itemId}...`}></LoadingBox>
114
      :
115
      <TabView
116
        renderTabBar={_renderTabBar}
117
        navigationState={{ index: selectedConcordance, routes }}
118
        renderScene={() => (
119
          <ItemView item={item} notes={notes} itemLoading={itemLoading} notesLoading={notesLoading} />
120
        )}
121
        initialLayout={{ width: layout.width }}
122

    
123
        // prevent default action on index change
124
        onIndexChange={handleTabChange}
125

    
126
      />
127
  );
128
}
129

    
130

    
131

    
132
export default ItemViewPage;
(1-1/2)