Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2b71499c

Přidáno uživatelem Fantič před více než 1 rok

re #10871: ListView refactor

Zobrazit rozdíly:

src/components/listView/ListView.tsx
3 3
import { useSelector } from "react-redux"
4 4
import { RootState } from "../../stores/store"
5 5
import ItemPreview from "./ItemPreview"
6
import { ItemPreviewType } from "../../types/listViewTypes"
6
import { ItemPreviewType, LoadedPagesOfInventories } from "../../types/listViewTypes"
7 7
import { log } from "../../logging/logger"
8 8
import ListViewInventoryGroup from "./ListViewInventoryGroup"
9 9
import { ErrorToast } from "../toast/ErrorToast"
10
import { Inventory } from "../../types/searchFormTypes"
10 11

  
11 12
type ListViewProps = {
12
    navigation: any
13
    navigation: any,
14

  
15
    inventories: Inventory[],
16
    numOfResults?: number,
17

  
18
    lastError?: string
19

  
20
    data: {[key: string]: ItemPreviewType[]}
21
    loadedPages: LoadedPagesOfInventories
22
    inventoriesDataLoading: {[key: string]: boolean}
23

  
24
    handleLoadMoreItems: (inventoryName: string, cursor: number) => void; 
13 25
}
14 26

  
15 27
const ListView = (props: ListViewProps) => {
16 28

  
17
    const inventories = useSelector((state: RootState) => state.listView.inventories)
18
    const numberOfResults = useSelector((state: RootState) => state.listView.numOfResults)
29
    const inventories = props.inventories
30
    const numberOfResults = props.numOfResults
19 31

  
32
    const data = props.data
33
    const loadedPages = props.loadedPages
34
    const inventoriesDataLoading = props.inventoriesDataLoading
20 35

  
21 36
    const lastError = useSelector((state: RootState) => state.listView.lastError)
22 37
    const toast = useToast();
......
44 59
                        <ListViewInventoryGroup
45 60
                            inventoryName={inventory.name}
46 61
                            inventoryLabel={inventory.label}
62

  
47 63
                            navigation={props.navigation}
48
                        />
64

  
65
                            // items
66
                            items={data[inventory.name]}
67
                            loading={inventoriesDataLoading[inventory.name]}
68
                            loadedPages={loadedPages[inventory.name]} 
69
                            
70
                            handleLoadMoreItems={props.handleLoadMoreItems } 
71
                            />
49 72
                    )
50 73
                    ) : (
51 74
                        <ListViewInventoryGroup
......
53 76
                            inventoryLabel={inventories[0].label}
54 77
                            navigation={props.navigation}
55 78
                            defaultOpen={true}
56
                        />
79

  
80
                            // items
81
                            items={data[inventories[0].name]}
82
                            loading={inventoriesDataLoading[inventories[0].name]}
83
                            loadedPages={loadedPages[inventories[0].name]} 
84
                            
85
                            handleLoadMoreItems={props.handleLoadMoreItems }                         />
57 86
                    )) : (
58 87
                    <Text alignSelf={"center"}>No results found</Text>
59 88
                )
src/components/listView/ListViewInventoryGroup.tsx
11 11
    inventoryLabel: string
12 12
    navigation: any
13 13
    defaultOpen?: boolean
14

  
15
    items: ItemPreviewType[]
16
    loading: boolean
17
    loadedPages: {
18
        cursor: number
19
        items: number
20
        records: number
21
    }
22

  
23
    handleLoadMoreItems: (inventoryName: string, cursor: number) => void; 
14 24
}
15 25

  
16 26
const ListViewInventoryGroup = (props: ListViewInventoryGroupProps) => {
17
    const items = useSelector((state: RootState) => state.listView.data[props.inventoryName])
18
    const loading = useSelector((state: RootState) => state.listView.loading)
19
    const [isExpanded, setIsExpanded] = useState(props.defaultOpen ? props.defaultOpen : false)
20
    const pagination = useSelector((state: RootState) => state.listView.loadedPages[props.inventoryName])
21 27

  
22
    const dispatch = useDispatch<AppDispatch>()
28
    const items = props.items
29
    const loading = props.loading
30
    const [isExpanded, setIsExpanded] = useState(false)
31
    const pagination = props.loadedPages
32

  
33
    const handleLoadMoreItems = props.handleLoadMoreItems
34

  
35
    useEffect(() => {
36
        if(props.defaultOpen != undefined){
37
            setIsExpanded(props.defaultOpen)
38
        }
39
    }, [props.defaultOpen])
23 40

  
24 41
    /**
25 42
     * Searches through names by given role, return getty_data.display_name if exists otherwise name.value
......
66 83
    useEffect(() => {
67 84
        if (isExpanded) {
68 85
            if (!items || items.length == 0) {
69
                dispatch(loadItemsByInventory(props.inventoryName))
86
                handleLoadMoreItems(props.inventoryName, 0)
70 87
            }
71 88
        }
72 89
    }, [isExpanded])
73 90

  
74
    useEffect(() => {
75
        if (loading) {
76
            setIsExpanded(false)
77
        }
78
    }, [loading])
91
    // Po načtení prvních data zůstal inventory results zavřený...  -> zakomentováno
92
    // useEffect(() => {
93
    //     if (loading) {
94
    //         setIsExpanded(false)
95
    //     }
96
    // }, [loading])
79 97

  
80 98
    useEffect(() => {
81 99
        if (props.defaultOpen) {
82 100
            if (!items || items.length == 0) {
83
                dispatch(loadItemsByInventory(props.inventoryName))
101
                handleLoadMoreItems(props.inventoryName, 0)
84 102
            }
85 103
        }
86 104
    }, [])
......
113 131
            ))}
114 132
            {isExpanded && items && pagination && pagination.cursor < pagination.records && (
115 133
                <Button
116
                    onPress={() => dispatch(loadItemsByInventory(props.inventoryName))}
134
                    onPress={() => {
135
                        handleLoadMoreItems(props.inventoryName, pagination.cursor)
136
                    }}
117 137
                    variant={"Link"}
118 138
                    mt={0}
119 139
                    size={"sm"}
src/pages/SearchPage.tsx
17 17
    fetchSubjects,
18 18
    fetchTechniques
19 19
} from "../stores/actions/searchFormThunks"
20
import { useDispatch } from "react-redux"
21
import { AppDispatch } from "../stores/store"
20
import { useDispatch, useSelector } from "react-redux"
21
import { AppDispatch, RootState } from "../stores/store"
22 22
import { log } from "../logging/logger"
23 23
import { DrawerScreenProps } from "@react-navigation/drawer"
24 24
import { RootDrawerParamList } from "./Navigation"
25
import { loadItemsByInventory } from "../stores/actions/listViewThunks"
25 26

  
26 27

  
27 28
const SearchPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList, 'Search'>) => {
28 29
    const dispatch = useDispatch<AppDispatch>()
29 30

  
31
    const inventories = useSelector((state: RootState) => state.listView.inventories)
32
    const numberOfResults = useSelector((state: RootState) => state.listView.numOfResults)
33

  
34
    const data = useSelector((state: RootState) => state.listView.data)
35
    const dataLoading = useSelector((state: RootState) => state.listView.inventoriesDataLoading)
36
    const pagination = useSelector((state: RootState) => state.listView.loadedPages)
37

  
38

  
30 39
    useEffect(() => {
31 40
        log.debug("SearchPage", "useEffect", "fetchEverything")
32 41

  
......
48 57
        log.debug("SearchPage", "props", route.params.inventoryId)
49 58
    }, [])
50 59

  
60
    console.log(data)
61

  
51 62
    return (
52 63
        <Center m={ 2 } mr={0} mb={6} flex={1}>
53 64

  
54 65
            <ScrollView flex={1} w={"100%"} >
55 66
                <VStack space={ 1 } mr={ 4 }>
56 67
                    <SearchForm inventoryId={route.params.inventoryId}/>
57
                    <ListView navigation={navigation}/>
68
                    <ListView navigation={navigation} 
69
                    
70
                    inventories={inventories} 
71
                    numOfResults={numberOfResults}
72

  
73
                    data={data} 
74
                    loadedPages={pagination} 
75
                    inventoriesDataLoading={dataLoading} 
76
                    handleLoadMoreItems={(inventoryName: string, cursor: number) => {
77
                        dispatch(loadItemsByInventory(inventoryName))
78
                    } }/>
58 79
                </VStack>
59 80
            </ScrollView>
60 81
        </Center>

Také k dispozici: Unified diff