Projekt

Obecné

Profil

« Předchozí | Další » 

Revize a7a9a204

Přidáno uživatelem Fantič před téměř 2 roky(ů)

re #10454: ItemView: refactor setIndex

Zobrazit rozdíly:

src/components/item/ItemTabBar.tsx
1 1
import { HStack, CircleIcon, View, ScrollView, Button } from "native-base";
2 2
import { CertaintyWithColors } from "../../stores/reducers/itemSlice";
3 3
import { Concordance } from "../../types/item";
4
import LoadingBox from "../loading/loadingBox";
4
import LoadingBox from "../loading/LoadingBox";
5 5

  
6 6
// custom render Tab
7
const ItemTabBar = (props: { navigationState: { routes: any[] }, concordances : Concordance[], index: number, onIndexChange : any }) => {
7
const ItemTabBar = (props: { navigationState: { routes: any[] }, concordances: Concordance[], index: number, onIndexChange: any }) => {
8 8

  
9
    const {concordances, onIndexChange, index} = props 
9
    const { concordances, onIndexChange, index } = props
10 10

  
11 11
    if (concordances && concordances.length > 0)
12
      return (
13
        <View h="16">
14
          <ScrollView horizontal={true} >
15
            <HStack>
16
              {props.navigationState.routes.map((route, i) => {
17
                return (
18
                  <Button size="lg" key={i}
19
                    variant={index == i ? "subtle" : "outline"}
20
                    rightIcon={<CircleIcon size="2" color={CertaintyWithColors[concordances[i].cert].color} />}
21
                    onPress={() => onIndexChange(i)}
22
                  >
23
                    {concordances[i].id}
24
                  </Button>
25
                );
26
              })}
27
            </HStack>
28
          </ScrollView>
29
        </View>
30
      )
12
        return (
13
            <View h="16">
14
                <ScrollView horizontal={true} >
15
                    <HStack>
16
                        {props.navigationState.routes.map((route, i) => {
17
                            return (
18
                                <Button size="lg" key={i}
19
                                    variant={index == i ? "subtle" : "outline"}
20
                                    rightIcon={<CircleIcon size="2" color={CertaintyWithColors[concordances[i].cert].color} />}
21
                                    onPress={() => onIndexChange(i)}
22
                                >
23
                                    {concordances[i].id}
24
                                </Button>
25
                            );
26
                        })}
27
                    </HStack>
28
                </ScrollView>
29
            </View>
30
        )
31 31
    else {
32
      return <LoadingBox text={`Loading concordances...`}></LoadingBox>
32
        return <LoadingBox text={`Loading concordances...`}></LoadingBox>
33 33
    }
34
  };
34
};
35 35

  
36
  export default ItemTabBar
36
export default ItemTabBar
src/components/item/ItemView.tsx
2 2
import React from "react"
3 3
import { Item } from "../../types/item"
4 4
import { Note } from "../../types/note"
5
import LoadingBox from "../loading/loadingBox"
5
import LoadingBox from "../loading/LoadingBox"
6 6

  
7 7
interface ItemDetailProps {
8 8
    item: Item,
src/components/loading/loadingBox.tsx
2 2
import React from "react"
3 3

  
4 4

  
5
interface loadingBoxProps {
5
interface LoadingBoxProps {
6 6
    text: string
7 7
}
8 8

  
9
const LoadingBox = (props: loadingBoxProps) => {
9
const LoadingBox = (props: LoadingBoxProps) => {
10 10
    const { text } = props;
11 11

  
12 12
    return (
src/pages/ItemViewPage.tsx
7 7
import ItemView from "../components/item/ItemView"
8 8
import { Button, Pressable, View, Text, Icon, CircleIcon, ScrollView, HStack, VStack } from "native-base"
9 9
import { useWindowDimensions } from "react-native"
10
import LoadingBox from "../components/loading/loadingBox"
10
import LoadingBox from "../components/loading/LoadingBox"
11 11
import { CertaintyWithColors } from "../stores/reducers/itemSlice"
12 12
import ItemTabBar from "../components/item/ItemTabBar"
13 13

  
......
64 64
    }
65 65
  }, [concordances]);
66 66

  
67
  // selected concordance changes
68
  useEffect(() => {
69
    console.log("index changed");
70
    if (concordances && concordances.length > 0) {
71
      dispatch(getItem(concordances[index].id));
72
    }
73
  }, [index]);
74

  
75 67
  // item changes
76 68
  useEffect(() => {
77 69
    if (!itemLoading && item && item.id) {
......
85 77
    }
86 78
  }, [item]);
87 79

  
80
  const handleIndexChanged = (index: number) => {
81
    console.log("index changed");
82
    setIndex(index);
83
    if (concordances && concordances.length > 0) {
84
      dispatch(getItem(concordances[index].id));
85
    }
86
  }
87

  
88 88
  return (
89 89
    !concordances || concordances.length < 1 ?
90 90
      <LoadingBox text={`Loading item ${props.itemId}...`}></LoadingBox>
......
96 96
          },
97 97
          concordances: concordances,
98 98
          index: index,
99
          onIndexChange: setIndex
99
          onIndexChange: handleIndexChanged
100 100
        })}
101 101

  
102 102
        navigationState={{ index, routes }}
......
104 104

  
105 105
        initialLayout={{ width: layout.width }}
106 106
        // prevent default action on index change
107
        onIndexChange={setIndex}
107
        onIndexChange={handleIndexChanged}
108 108

  
109 109
      />
110 110
  );

Také k dispozici: Unified diff