Projekt

Obecné

Profil

Stáhnout (4.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Box, Center, Image, Pressable, ScrollView, Text, useToast} from "native-base"
2
import { useDispatch, useSelector } from "react-redux"
3
import { AppDispatch, RootState } from "../stores/store"
4
import { useContext, useEffect, useState } from "react"
5
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
import { RootStackParamList } from "./Navigation"
10
import { log } from "../logging/logger"
11
import { HOME_PAGE_IMG_URL } from "../api/constants"
12
import { ErrorToast } from "../components/toast/ErrorToast"
13
import {checkAuth} from "../stores/actions/userThunks"
14
// import { Image } from "react-native"
15

    
16
const HomePage = ({navigation}: DrawerScreenProps<RootStackParamList, 'Home'>) => {
17
    const data = useSelector((state: RootState) => state.homePage.data)
18
    const loading = useSelector((state: RootState) => state.homePage.loading)
19
    const lastError = useSelector((state: RootState) => state.itemViewState.lastError)
20
    const toast = useToast();
21
  
22
    useEffect(() => {
23
        if (lastError) {
24
            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
        }
34
    }, [lastError])
35

    
36
    const [aspectRatio, setAspectRatio] = useState(1.0)
37

    
38
    const dispatch = useDispatch<AppDispatch>()
39

    
40
    useEffect(() => {
41
        dispatch(checkAuth())
42

    
43
        if (data === null || data.length === 0 && !loading) {
44
            dispatch(fetchData())
45
        }
46

    
47
    }, [])
48

    
49

    
50

    
51
    return (
52
        <Center m={2} mx={4} mb={20}>
53
            { loading ? (
54
                <LoadingBox text={ "Loading..." }/>
55
            ) : (
56
                <>
57
                    <ApplicationHeading/>
58
                    <ScrollView mb={5} >
59
                    <Image
60
                        source={ {uri: HOME_PAGE_IMG_URL} }
61
                        w={"100%"} h={ 200 }
62
                        alignSelf={ "center" }
63
                        alt={ "Rudolf-Aachen" }
64
                        rounded={ "lg" }
65
                        my={ 2 }
66
                    />
67
                    { data && data.length > 0 && (
68
                        <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
                        </Text>
76
                        </Box>
77
                    )}
78
                        <Text
79
                            mt={ 2 }
80
                            ml={ 2 }
81
                            mb={ -2 }
82
                            bold
83
                            fontSize={"2xl"}
84
                            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
                            rounded={ "lg" }
93
                            backgroundColor={ "primary.100" }
94
                            m = { 2 }
95
                            p={ 3 }
96
                        >
97
                            <Text
98
                                bold
99
                                fontSize={ "md" }
100
                                color={"primary.500"}
101
                                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
        </Center>
119
    )
120
}
121

    
122
export default HomePage
(1-1/8)