Projekt

Obecné

Profil

Stáhnout (3.6 KB) Statistiky
| Větev: | Tag: | Revize:
1 9c55d3bb Schwobik
import { useEffect, useState } from "react"
2 f386a4fe Fantič
import { ScrollView, Text, VStack, useToast } from "native-base"
3 9c55d3bb Schwobik
import { useSelector } from "react-redux"
4
import { RootState } from "../../stores/store"
5 ca53e9f1 Schwobik
import ItemPreview from "./ItemPreview"
6 2b71499c Fantič
import { ItemPreviewType, LoadedPagesOfInventories } from "../../types/listViewTypes"
7 e49b1f44 Schwobik
import { log } from "../../logging/logger"
8 ca44ce3d Schwobik
import ListViewInventoryGroup from "./ListViewInventoryGroup"
9 f386a4fe Fantič
import { ErrorToast } from "../toast/ErrorToast"
10 ab632fe5 Michal Schwob
import LoadingBox from "../loading/LoadingBox"
11 2b71499c Fantič
import { Inventory } from "../../types/searchFormTypes"
12 e49b1f44 Schwobik
13 04928342 Schwobik
type ListViewProps = {
14 a6999074 Fantič
    navigation: any
15 2b71499c Fantič
16 a6999074 Fantič
    inventories: Inventory[]
17
    numOfResults?: number
18 2b71499c Fantič
19
    lastError?: string
20
21
    data: {[key: string]: ItemPreviewType[]}
22
    loadedPages: LoadedPagesOfInventories
23
    inventoriesDataLoading: {[key: string]: boolean}
24
25 a6999074 Fantič
    handleLoadMoreItems: (inventoryName: string, cursor: number) => void;
26 8e5880b0 Michal Schwob
27 a6999074 Fantič
    hideHeaderText?: boolean
28 04928342 Schwobik
}
29 9c55d3bb Schwobik
30 04928342 Schwobik
const ListView = (props: ListViewProps) => {
31 9c55d3bb Schwobik
32 2b71499c Fantič
    const inventories = props.inventories
33
    const numberOfResults = props.numOfResults
34 e49b1f44 Schwobik
35 2b71499c Fantič
    const data = props.data
36
    const loadedPages = props.loadedPages
37
    const inventoriesDataLoading = props.inventoriesDataLoading
38 ab632fe5 Michal Schwob
    const loading = useSelector((state: RootState) => state.listView.loading)
39 e49b1f44 Schwobik
40 f386a4fe Fantič
    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 9c55d3bb Schwobik
    return (
58 e49b1f44 Schwobik
        <>
59 a6999074 Fantič
            {!props.hideHeaderText && <Text fontSize={16} fontWeight={"bold"} color={"primary.500"}>Search results{numberOfResults ? ` (${numberOfResults})` : ""}:</Text>}
60 ab632fe5 Michal Schwob
            {loading ? (
61
                <LoadingBox text={"Loading..."} />
62
            )  :
63
            inventories && inventories.length > 0 ?
64 1980ed09 Schwobik
                (inventories.length > 1 ?
65
                    inventories.map((inventory) => (
66 f386a4fe Fantič
                        <ListViewInventoryGroup
67
                            inventoryName={inventory.name}
68
                            inventoryLabel={inventory.label}
69 2b71499c Fantič
70 f386a4fe Fantič
                            navigation={props.navigation}
71 2b71499c Fantič
72
                            // items
73
                            items={data[inventory.name]}
74
                            loading={inventoriesDataLoading[inventory.name]}
75 8e5880b0 Michal Schwob
                            loadedPages={loadedPages[inventory.name]}
76
77
                            handleLoadMoreItems={props.handleLoadMoreItems }
78 2b71499c Fantič
                            />
79 f386a4fe Fantič
                    )
80 1980ed09 Schwobik
                    ) : (
81
                        <ListViewInventoryGroup
82 f386a4fe Fantič
                            inventoryName={inventories[0].name}
83
                            inventoryLabel={inventories[0].label}
84
                            navigation={props.navigation}
85
                            defaultOpen={true}
86 2b71499c Fantič
87
                            // items
88
                            items={data[inventories[0].name]}
89
                            loading={inventoriesDataLoading[inventories[0].name]}
90 8e5880b0 Michal Schwob
                            loadedPages={loadedPages[inventories[0].name]}
91
92 2b71499c Fantič
                            handleLoadMoreItems={props.handleLoadMoreItems }                         />
93 1980ed09 Schwobik
                    )) : (
94 f386a4fe Fantič
                    <Text alignSelf={"center"}>No results found</Text>
95 ca44ce3d Schwobik
                )
96 ab632fe5 Michal Schwob
97 1980ed09 Schwobik
            }
98 e49b1f44 Schwobik
        </>
99 9c55d3bb Schwobik
    )
100
}
101
102
export default ListView