Projekt

Obecné

Profil

Stáhnout (4.09 KB) Statistiky
| Větev: | Tag: | Revize:
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
} 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

    
23

    
24
const LoginPage = () => {
25
    const [username, setUsername] = useState("")
26
    const [password, setPassword] = useState("")
27
    const lastError = useSelector((state: RootState) => state.user.lastError)
28

    
29
    const dispatch = useDispatch<AppDispatch>()
30

    
31
    const loginUser = () => {
32
        log.debug("LoginPage", "loginUser", "dispatching login")
33
        dispatch(login({username, password}))
34
        //TODO - add error handling
35
    }
36

    
37

    
38
    useEffect(() => {
39
        dispatch(checkAuth())
40
    }, [])
41

    
42
    return (
43
        <KeyboardAvoidingView
44
            behavior={ Platform.OS === "ios" ? "padding" : "height" }
45
            flex={ 1 }
46
            justifyContent={ "center" }
47
            h={ {
48
                base: "400px",
49
                lg: "auto"
50
            } }
51
        >
52
            <Center w="100%">
53
                <VStack
54
                    p="2"
55
                    w="80%"
56
                    justifyContent={ "center" }
57
                >
58
                    <ApplicationHeading />
59
                    <Heading
60
                        mt="10"
61
                        textAlign="center"
62
                        _dark={ {
63
                            color: "primary.500"
64
                        } }
65

    
66
                        fontWeight="bold"
67
                        size="xl"
68
                    >
69
                        Log in
70
                    </Heading>
71
                    <Text
72
                        mt="1"
73
                        textAlign="center"
74
                    >
75
                        Please log in to continue
76
                    </Text>
77
                    { lastError && (
78
                        <Text
79
                            mt="1"
80
                            textAlign="center"
81
                            color="error.500"
82
                        >
83
                            { lastError }
84
                        </Text>
85
                    ) }
86

    
87
                    <VStack space={ 3 } >
88
                        <FormControl>
89
                            <FormControl.Label>Username</FormControl.Label>
90
                            <Input
91
                                // value={username}
92
                                textContentType={"username"}
93
                                rounded={ "xl" }
94
                                autoComplete={"username"}
95
                                onSubmitEditing={ () => { loginUser() } }
96
                                onChangeText={ (username) => setUsername(username) }
97
                            />
98
                            <FormControl.Label>Password</FormControl.Label>
99
                            <Input
100
                                type="password"
101
                                autoComplete={ "password" }
102
                                id={ "password" }
103
                                returnKeyType={ "go" }
104
                                rounded={ "xl" }
105
                                onSubmitEditing={ () => loginUser() }
106
                                onChangeText={ (password) => setPassword(password) }
107
                            />
108
                            <Button
109
                                mt="2"
110
                                bg={ "primary.500" }
111
                                onPress={ loginUser }
112
                                rounded={ "xl"}
113
                            >
114
                                Sign in
115
                            </Button>
116
                        </FormControl>
117
                    </VStack>
118
                </VStack>
119
            </Center>
120
        </KeyboardAvoidingView>
121
    )
122
}
123

    
124
export default LoginPage
(3-3/6)