Projekt

Obecné

Profil

Stáhnout (3.6 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { useEffect, useState } from "react"
2
import { ScrollView, Text, VStack, useToast } from "native-base"
3
import { useSelector } from "react-redux"
4
import { RootState } from "../../stores/store"
5
import ItemPreview from "./ItemPreview"
6
import { ItemPreviewType, LoadedPagesOfInventories } from "../../types/listViewTypes"
7
import { log } from "../../logging/logger"
8
import ListViewInventoryGroup from "./ListViewInventoryGroup"
9
import { ErrorToast } from "../toast/ErrorToast"
10
import LoadingBox from "../loading/LoadingBox"
11
import { Inventory } from "../../types/searchFormTypes"
12

    
13
type ListViewProps = {
14
    navigation: any
15

    
16
    inventories: Inventory[]
17
    numOfResults?: number
18

    
19
    lastError?: string
20

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

    
25
    handleLoadMoreItems: (inventoryName: string, cursor: number) => void;
26

    
27
    hideHeaderText?: boolean
28
}
29

    
30
const ListView = (props: ListViewProps) => {
31

    
32
    const inventories = props.inventories
33
    const numberOfResults = props.numOfResults
34

    
35
    const data = props.data
36
    const loadedPages = props.loadedPages
37
    const inventoriesDataLoading = props.inventoriesDataLoading
38
    const loading = useSelector((state: RootState) => state.listView.loading)
39

    
40
    const lastError = useSelector((state: RootState) => state.listView.lastError)
41
    const toast = useToast();
42

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

    
57
    return (
58
        <>
59
            {!props.hideHeaderText && <Text fontSize={16} fontWeight={"bold"} color={"primary.500"}>Search results{numberOfResults ? ` (${numberOfResults})` : ""}:</Text>}
60
            {loading ? (
61
                <LoadingBox text={"Loading..."} />
62
            )  :
63
            inventories && inventories.length > 0 ?
64
                (inventories.length > 1 ?
65
                    inventories.map((inventory) => (
66
                        <ListViewInventoryGroup
67
                            inventoryName={inventory.name}
68
                            inventoryLabel={inventory.label}
69

    
70
                            navigation={props.navigation}
71

    
72
                            // items
73
                            items={data[inventory.name]}
74
                            loading={inventoriesDataLoading[inventory.name]}
75
                            loadedPages={loadedPages[inventory.name]}
76

    
77
                            handleLoadMoreItems={props.handleLoadMoreItems }
78
                            />
79
                    )
80
                    ) : (
81
                        <ListViewInventoryGroup
82
                            inventoryName={inventories[0].name}
83
                            inventoryLabel={inventories[0].label}
84
                            navigation={props.navigation}
85
                            defaultOpen={true}
86

    
87
                            // items
88
                            items={data[inventories[0].name]}
89
                            loading={inventoriesDataLoading[inventories[0].name]}
90
                            loadedPages={loadedPages[inventories[0].name]}
91

    
92
                            handleLoadMoreItems={props.handleLoadMoreItems }                         />
93
                    )) : (
94
                    <Text alignSelf={"center"}>No results found</Text>
95
                )
96

    
97
            }
98
        </>
99
    )
100
}
101

    
102
export default ListView
(3-3/4)