Projekt

Obecné

Profil

Stáhnout (4.09 KB) Statistiky
| Větev: | Tag: | Revize:
1 ddc84a2f Schwobik
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 06808454 Schwobik
import { useCallback, useEffect, useState } from "react"
15 5ed9692c Schwobik
import { useDispatch, useSelector } from "react-redux"
16 06808454 Schwobik
import { checkAuth, login } from "../stores/actions/userThunks"
17 5ed9692c Schwobik
import { AppDispatch, RootState } from "../stores/store"
18 ddc84a2f Schwobik
import { Platform } from "react-native"
19 06808454 Schwobik
import { log } from "../logging/logger"
20
import * as SplashScreen from "expo-splash-screen"
21
import { ApplicationHeading } from "../components/reusables/ApplicationHeading"
22 7e4b4e6a mschwob
23 5c86e446 mschwob
24 7e4b4e6a mschwob
const LoginPage = () => {
25 5ed9692c Schwobik
    const [username, setUsername] = useState("")
26
    const [password, setPassword] = useState("")
27 06808454 Schwobik
    const lastError = useSelector((state: RootState) => state.user.lastError)
28 5ed9692c Schwobik
29
    const dispatch = useDispatch<AppDispatch>()
30
31
    const loginUser = () => {
32 06808454 Schwobik
        log.debug("LoginPage", "loginUser", "dispatching login")
33 ddc84a2f Schwobik
        dispatch(login({username, password}))
34 5ed9692c Schwobik
        //TODO - add error handling
35
    }
36
37 06808454 Schwobik
38
    useEffect(() => {
39
        dispatch(checkAuth())
40
    }, [])
41
42 7e4b4e6a mschwob
    return (
43 ddc84a2f Schwobik
        <KeyboardAvoidingView
44 06808454 Schwobik
            behavior={ Platform.OS === "ios" ? "padding" : "height" }
45
            flex={ 1 }
46
            justifyContent={ "center" }
47 ddc84a2f Schwobik
            h={ {
48
                base: "400px",
49
                lg: "auto"
50
            } }
51
        >
52
            <Center w="100%">
53 06808454 Schwobik
                <VStack
54 ddc84a2f Schwobik
                    p="2"
55 06808454 Schwobik
                    w="80%"
56
                    justifyContent={ "center" }
57 ddc84a2f Schwobik
                >
58 06808454 Schwobik
                    <ApplicationHeading />
59 ddc84a2f Schwobik
                    <Heading
60
                        mt="10"
61
                        textAlign="center"
62
                        _dark={ {
63 1980ed09 Schwobik
                            color: "primary.500"
64 ddc84a2f Schwobik
                        } }
65 1980ed09 Schwobik
66 ddc84a2f Schwobik
                        fontWeight="bold"
67
                        size="xl"
68 5ed9692c Schwobik
                    >
69 ddc84a2f Schwobik
                        Log in
70
                    </Heading>
71
                    <Text
72
                        mt="1"
73
                        textAlign="center"
74
                    >
75
                        Please log in to continue
76
                    </Text>
77 06808454 Schwobik
                    { lastError && (
78
                        <Text
79
                            mt="1"
80
                            textAlign="center"
81
                            color="error.500"
82
                        >
83
                            { lastError }
84
                        </Text>
85
                    ) }
86 ddc84a2f Schwobik
87 06808454 Schwobik
                    <VStack space={ 3 } >
88 ddc84a2f Schwobik
                        <FormControl>
89
                            <FormControl.Label>Username</FormControl.Label>
90
                            <Input
91
                                // value={username}
92 ca53e9f1 Schwobik
                                textContentType={"username"}
93 1980ed09 Schwobik
                                rounded={ "xl" }
94 ca53e9f1 Schwobik
                                autoComplete={"username"}
95
                                onSubmitEditing={ () => { loginUser() } }
96 ddc84a2f Schwobik
                                onChangeText={ (username) => setUsername(username) }
97
                            />
98
                            <FormControl.Label>Password</FormControl.Label>
99 ca53e9f1 Schwobik
                            <Input
100
                                type="password"
101
                                autoComplete={ "password" }
102
                                id={ "password" }
103 1980ed09 Schwobik
                                returnKeyType={ "go" }
104
                                rounded={ "xl" }
105 ca53e9f1 Schwobik
                                onSubmitEditing={ () => loginUser() }
106
                                onChangeText={ (password) => setPassword(password) }
107 ddc84a2f Schwobik
                            />
108 1980ed09 Schwobik
                            <Button
109
                                mt="2"
110
                                bg={ "primary.500" }
111
                                onPress={ loginUser }
112
                                rounded={ "xl"}
113
                            >
114
                                Sign in
115
                            </Button>
116 ddc84a2f Schwobik
                        </FormControl>
117
                    </VStack>
118 06808454 Schwobik
                </VStack>
119 ddc84a2f Schwobik
            </Center>
120
        </KeyboardAvoidingView>
121 5c86e446 mschwob
    )
122 7e4b4e6a mschwob
}
123
124
export default LoginPage