1
|
import {
|
2
|
Center,
|
3
|
Box,
|
4
|
Heading,
|
5
|
VStack,
|
6
|
FormControl,
|
7
|
Link,
|
8
|
Input,
|
9
|
Button,
|
10
|
HStack,
|
11
|
Text,
|
12
|
KeyboardAvoidingView,
|
13
|
useToast
|
14
|
} from "native-base"
|
15
|
import { useCallback, useContext, useEffect, useState } from "react"
|
16
|
import { useDispatch, useSelector } from "react-redux"
|
17
|
import { checkAuth, login } from "../stores/actions/userThunks"
|
18
|
import { AppDispatch, RootState } from "../stores/store"
|
19
|
import { Platform } from "react-native"
|
20
|
import { log } from "../logging/logger"
|
21
|
import * as SplashScreen from "expo-splash-screen"
|
22
|
import { ApplicationHeading } from "../components/reusables/ApplicationHeading"
|
23
|
import { InfoToast } from "../components/toast/InfoToast"
|
24
|
import { ErrorToast } from "../components/toast/ErrorToast"
|
25
|
import { SuccessToast } from "../components/toast/SuccessToast"
|
26
|
|
27
|
|
28
|
const LoginPage = () => {
|
29
|
const [username, setUsername] = useState("")
|
30
|
const [password, setPassword] = useState("")
|
31
|
|
32
|
const lastError = useSelector((state: RootState) => state.user.lastError)
|
33
|
|
34
|
const toast = useToast();
|
35
|
|
36
|
|
37
|
useEffect(() => {
|
38
|
if (lastError) {
|
39
|
toast.closeAll()
|
40
|
toast.show({
|
41
|
render: ({
|
42
|
id
|
43
|
}) => {
|
44
|
return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
|
45
|
},
|
46
|
duration: 3000
|
47
|
});
|
48
|
}
|
49
|
|
50
|
}, [lastError])
|
51
|
|
52
|
const dispatch = useDispatch<AppDispatch>()
|
53
|
|
54
|
const loginUser = () => {
|
55
|
log.debug("LoginPage", "loginUser", "dispatching login")
|
56
|
dispatch(login({ username, password }))
|
57
|
}
|
58
|
|
59
|
useEffect(() => {
|
60
|
dispatch(checkAuth())
|
61
|
}, [])
|
62
|
|
63
|
return (
|
64
|
<KeyboardAvoidingView
|
65
|
behavior={Platform.OS === "ios" ? "padding" : "height"}
|
66
|
flex={1}
|
67
|
justifyContent={"center"}
|
68
|
h={{
|
69
|
base: "400px",
|
70
|
lg: "auto"
|
71
|
}}
|
72
|
>
|
73
|
<Center w="100%">
|
74
|
<VStack
|
75
|
p="2"
|
76
|
w="80%"
|
77
|
justifyContent={"center"}
|
78
|
>
|
79
|
<ApplicationHeading />
|
80
|
<Heading
|
81
|
mt="10"
|
82
|
textAlign="center"
|
83
|
_dark={{
|
84
|
color: "primary.500"
|
85
|
}}
|
86
|
|
87
|
fontWeight="bold"
|
88
|
size="xl"
|
89
|
>
|
90
|
Log in
|
91
|
</Heading>
|
92
|
<Text
|
93
|
mt="1"
|
94
|
textAlign="center"
|
95
|
>
|
96
|
Please log in to continue
|
97
|
</Text>
|
98
|
{lastError && (
|
99
|
<Text
|
100
|
mt="1"
|
101
|
textAlign="center"
|
102
|
color="error.500"
|
103
|
>
|
104
|
{lastError}
|
105
|
</Text>
|
106
|
)}
|
107
|
|
108
|
<VStack space={3} >
|
109
|
<FormControl>
|
110
|
<FormControl.Label>Username</FormControl.Label>
|
111
|
<Input
|
112
|
// value={username}
|
113
|
textContentType={"username"}
|
114
|
rounded={"xl"}
|
115
|
autoComplete={"username"}
|
116
|
onSubmitEditing={() => { loginUser() }}
|
117
|
onChangeText={(username) => setUsername(username)}
|
118
|
/>
|
119
|
<FormControl.Label>Password</FormControl.Label>
|
120
|
<Input
|
121
|
type="password"
|
122
|
autoComplete={"password"}
|
123
|
id={"password"}
|
124
|
returnKeyType={"go"}
|
125
|
rounded={"xl"}
|
126
|
onSubmitEditing={() => loginUser()}
|
127
|
onChangeText={(password) => setPassword(password)}
|
128
|
/>
|
129
|
<Button
|
130
|
mt="2"
|
131
|
bg={"primary.500"}
|
132
|
onPress={loginUser}
|
133
|
rounded={"xl"}
|
134
|
>
|
135
|
Sign in
|
136
|
</Button>
|
137
|
</FormControl>
|
138
|
</VStack>
|
139
|
</VStack>
|
140
|
</Center>
|
141
|
</KeyboardAvoidingView>
|
142
|
)
|
143
|
}
|
144
|
|
145
|
export default LoginPage
|