Projekt

Obecné

Profil

Stáhnout (4.5 KB) Statistiky
| Větev: | Tag: | Revize:
1 006d5d56 Michal Schwob
import {Box, Center, Image, Pressable, ScrollView, Text, useToast} from "native-base"
2 4da7b143 Schwobik
import { useDispatch, useSelector } from "react-redux"
3
import { AppDispatch, RootState } from "../stores/store"
4 c8be2597 Fantič
import { useContext, useEffect, useState } from "react"
5 4da7b143 Schwobik
import { fetchData } from "../stores/actions/homePageThunks"
6
import { ApplicationHeading } from "../components/reusables/ApplicationHeading"
7
import LoadingBox from "../components/loading/LoadingBox"
8
import { DrawerScreenProps } from "@react-navigation/drawer"
9 a62d7c92 Michal Schwob
import { RootStackParamList } from "./Navigation"
10 4da7b143 Schwobik
import { log } from "../logging/logger"
11 8c3203e0 Schwobik
import { HOME_PAGE_IMG_URL } from "../api/constants"
12 f386a4fe Fantič
import { ErrorToast } from "../components/toast/ErrorToast"
13 ccb721de Michal Schwob
import {checkAuth} from "../stores/actions/userThunks"
14 4da7b143 Schwobik
// import { Image } from "react-native"
15
16 a62d7c92 Michal Schwob
const HomePage = ({navigation}: DrawerScreenProps<RootStackParamList, 'Home'>) => {
17 4da7b143 Schwobik
    const data = useSelector((state: RootState) => state.homePage.data)
18
    const loading = useSelector((state: RootState) => state.homePage.loading)
19 f386a4fe Fantič
    const lastError = useSelector((state: RootState) => state.itemViewState.lastError)
20
    const toast = useToast();
21
  
22 c8be2597 Fantič
    useEffect(() => {
23
        if (lastError) {
24 f386a4fe Fantič
            toast.closeAll()
25
            toast.show({
26
                render: ({
27
                    id
28
                }) => {
29
                    return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
30
                },
31
                duration: 3000
32
            });
33 c8be2597 Fantič
        }
34
    }, [lastError])
35
36 4da7b143 Schwobik
    const [aspectRatio, setAspectRatio] = useState(1.0)
37
38
    const dispatch = useDispatch<AppDispatch>()
39
40
    useEffect(() => {
41 ccb721de Michal Schwob
        dispatch(checkAuth())
42
43 4da7b143 Schwobik
        if (data === null || data.length === 0 && !loading) {
44
            dispatch(fetchData())
45
        }
46
47
    }, [])
48 b7014ba2 Schwobik
49 c8be2597 Fantič
50
51 b7014ba2 Schwobik
    return (
52 006d5d56 Michal Schwob
        <Center m={2} mx={4} mb={20}>
53 4da7b143 Schwobik
            { loading ? (
54
                <LoadingBox text={ "Loading..." }/>
55
            ) : (
56
                <>
57
                    <ApplicationHeading/>
58 cbf81c55 Schwobik
                    <ScrollView mb={5} >
59 4da7b143 Schwobik
                    <Image
60 8c3203e0 Schwobik
                        source={ {uri: HOME_PAGE_IMG_URL} }
61 4da7b143 Schwobik
                        w={"100%"} h={ 200 }
62
                        alignSelf={ "center" }
63
                        alt={ "Rudolf-Aachen" }
64 006d5d56 Michal Schwob
                        rounded={ "lg" }
65
                        my={ 2 }
66 4da7b143 Schwobik
                    />
67
                    { data && data.length > 0 && (
68 006d5d56 Michal Schwob
                        <Box m={2} p={ 3 } bg={"primary.100"} rounded={"lg"}>
69
                            <Text
70
                                fontSize={ "sm" }
71
                                lineHeight={"sm"}
72
                                textAlign={"justify"}
73
                            >
74
                                {data[0]["general"].text}
75 4da7b143 Schwobik
                        </Text>
76 006d5d56 Michal Schwob
                        </Box>
77 4da7b143 Schwobik
                    )}
78
                        <Text
79
                            mt={ 2 }
80 006d5d56 Michal Schwob
                            ml={ 2 }
81
                            mb={ -2 }
82 4da7b143 Schwobik
                            bold
83 006d5d56 Michal Schwob
                            fontSize={"2xl"}
84 4da7b143 Schwobik
                            color={"primary.500"}
85
                        >
86
                            Inventories
87
                        </Text>
88
                    { data && data.length > 1 && data.slice(1).map((item) => (
89
                        <Pressable
90
                            onPress={() => navigation.navigate("Search", {inventoryId: Object.keys(item)[0]}) }
91
                            key={ Object.values(item)[0].label }
92 006d5d56 Michal Schwob
                            rounded={ "lg" }
93
                            backgroundColor={ "primary.100" }
94 4da7b143 Schwobik
                            m = { 2 }
95
                            p={ 3 }
96
                        >
97
                            <Text
98
                                bold
99
                                fontSize={ "md" }
100 006d5d56 Michal Schwob
                                color={"primary.500"}
101 4da7b143 Schwobik
                                mb={1}
102
                            >
103
                                { Object.values(item)[0].label }
104
                            </Text>
105
                            <Text
106
                                fontSize={ "sm" }
107
                                lineHeight={"sm"}
108
                                textAlign={"justify"}
109
                            >
110
                                { Object.values(item)[0].text }
111
                            </Text>
112
                        </Pressable>
113
                    )) }
114
115
                    </ScrollView>
116
                </>
117
            ) }
118 b7014ba2 Schwobik
        </Center>
119
    )
120
}
121
122
export default HomePage