Projekt

Obecné

Profil

Stáhnout (3.45 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 { Inventory } from "../../types/searchFormTypes"
11

    
12
type ListViewProps = {
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;
25
    
26
    hideHeaderText?: boolean
27
}
28

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

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

    
34
    const data = props.data
35
    const loadedPages = props.loadedPages
36
    const inventoriesDataLoading = props.inventoriesDataLoading
37

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

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

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

    
65
                            navigation={props.navigation}
66

    
67
                            // items
68
                            items={data[inventory.name]}
69
                            loading={inventoriesDataLoading[inventory.name]}
70
                            loadedPages={loadedPages[inventory.name]} 
71
                            
72
                            handleLoadMoreItems={props.handleLoadMoreItems } 
73
                            />
74
                    )
75
                    ) : (
76
                        <ListViewInventoryGroup
77
                            inventoryName={inventories[0].name}
78
                            inventoryLabel={inventories[0].label}
79
                            navigation={props.navigation}
80
                            defaultOpen={true}
81

    
82
                            // items
83
                            items={data[inventories[0].name]}
84
                            loading={inventoriesDataLoading[inventories[0].name]}
85
                            loadedPages={loadedPages[inventories[0].name]} 
86
                            
87
                            handleLoadMoreItems={props.handleLoadMoreItems }                         />
88
                    )) : (
89
                    <Text alignSelf={"center"}>No results found</Text>
90
                )
91
            }
92
        </>
93
    )
94
}
95

    
96
export default ListView
(3-3/4)