1
|
import { 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 { Image } from "react-native"
|
14
|
|
15
|
const HomePage = ({navigation}: DrawerScreenProps<RootStackParamList, 'Home'>) => {
|
16
|
const data = useSelector((state: RootState) => state.homePage.data)
|
17
|
const loading = useSelector((state: RootState) => state.homePage.loading)
|
18
|
const lastError = useSelector((state: RootState) => state.itemViewState.lastError)
|
19
|
const toast = useToast();
|
20
|
|
21
|
useEffect(() => {
|
22
|
if (lastError) {
|
23
|
toast.closeAll()
|
24
|
toast.show({
|
25
|
render: ({
|
26
|
id
|
27
|
}) => {
|
28
|
return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
|
29
|
},
|
30
|
duration: 3000
|
31
|
});
|
32
|
}
|
33
|
}, [lastError])
|
34
|
|
35
|
const [aspectRatio, setAspectRatio] = useState(1.0)
|
36
|
|
37
|
const dispatch = useDispatch<AppDispatch>()
|
38
|
|
39
|
useEffect(() => {
|
40
|
if (data === null || data.length === 0 && !loading) {
|
41
|
dispatch(fetchData())
|
42
|
}
|
43
|
|
44
|
}, [])
|
45
|
|
46
|
|
47
|
|
48
|
return (
|
49
|
<Center m={2} mb={10}>
|
50
|
{ loading ? (
|
51
|
<LoadingBox text={ "Loading..." }/>
|
52
|
) : (
|
53
|
<>
|
54
|
<ApplicationHeading/>
|
55
|
<ScrollView mb={5} >
|
56
|
<Image
|
57
|
source={ {uri: HOME_PAGE_IMG_URL} }
|
58
|
w={"100%"} h={ 200 }
|
59
|
resizeMode={"contain"}
|
60
|
alignSelf={ "center" }
|
61
|
alt={ "Rudolf-Aachen" }
|
62
|
/>
|
63
|
{ data && data.length > 0 && (
|
64
|
<Text
|
65
|
mx={ 2 }
|
66
|
fontSize={ "sm" }
|
67
|
lineHeight={"sm"}
|
68
|
color={"gray.500"}
|
69
|
textAlign={"justify"}
|
70
|
>
|
71
|
{data[0]["general"].text}
|
72
|
</Text>
|
73
|
)}
|
74
|
<Text
|
75
|
mt={ 2 }
|
76
|
bold
|
77
|
fontSize={"xl"}
|
78
|
color={"primary.500"}
|
79
|
>
|
80
|
Inventories
|
81
|
</Text>
|
82
|
{ data && data.length > 1 && data.slice(1).map((item) => (
|
83
|
<Pressable
|
84
|
onPress={() => navigation.navigate("Search", {inventoryId: Object.keys(item)[0]}) }
|
85
|
key={ Object.values(item)[0].label }
|
86
|
rounded={ "md" }
|
87
|
borderWidth={ 1 }
|
88
|
borderColor={ "gray.300" }
|
89
|
m = { 2 }
|
90
|
p={ 3 }
|
91
|
>
|
92
|
<Text
|
93
|
bold
|
94
|
fontSize={ "md" }
|
95
|
mb={1}
|
96
|
>
|
97
|
{ Object.values(item)[0].label }
|
98
|
</Text>
|
99
|
<Text
|
100
|
fontSize={ "sm" }
|
101
|
lineHeight={"sm"}
|
102
|
color={"gray.500"}
|
103
|
textAlign={"justify"}
|
104
|
>
|
105
|
{ Object.values(item)[0].text }
|
106
|
</Text>
|
107
|
</Pressable>
|
108
|
)) }
|
109
|
|
110
|
</ScrollView>
|
111
|
</>
|
112
|
) }
|
113
|
</Center>
|
114
|
)
|
115
|
}
|
116
|
|
117
|
export default HomePage
|