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