Projekt

Obecné

Profil

Stáhnout (4.5 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 7410d6c1 Michal Schwob
    KeyboardAvoidingView, useToast
13 ddc84a2f Schwobik
} 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 7410d6c1 Michal Schwob
import {consumeError} from "../stores/reducers/userSlice"
23 7e4b4e6a mschwob
24 5c86e446 mschwob
25 7e4b4e6a mschwob
const LoginPage = () => {
26 5ed9692c Schwobik
    const [username, setUsername] = useState("")
27
    const [password, setPassword] = useState("")
28 06808454 Schwobik
    const lastError = useSelector((state: RootState) => state.user.lastError)
29 5ed9692c Schwobik
30
    const dispatch = useDispatch<AppDispatch>()
31
32 7410d6c1 Michal Schwob
    const toast = useToast()
33 5ed9692c Schwobik
    const loginUser = () => {
34 06808454 Schwobik
        log.debug("LoginPage", "loginUser", "dispatching login")
35 ddc84a2f Schwobik
        dispatch(login({username, password}))
36 5ed9692c Schwobik
        //TODO - add error handling
37
    }
38
39 06808454 Schwobik
    useEffect(() => {
40
        dispatch(checkAuth())
41
    }, [])
42
43 7410d6c1 Michal Schwob
    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 7e4b4e6a mschwob
    return (
56 ddc84a2f Schwobik
        <KeyboardAvoidingView
57 06808454 Schwobik
            behavior={ Platform.OS === "ios" ? "padding" : "height" }
58
            flex={ 1 }
59
            justifyContent={ "center" }
60 9fa2b717 Michal Schwob
            h={ {lg: "auto"} }
61
            // behavior={ "padding" }
62
            keyboardVerticalOffset={ 100 }
63 ddc84a2f Schwobik
        >
64 7410d6c1 Michal Schwob
65 ddc84a2f Schwobik
            <Center w="100%">
66 06808454 Schwobik
                <VStack
67 ddc84a2f Schwobik
                    p="2"
68 06808454 Schwobik
                    w="80%"
69 9fa2b717 Michal Schwob
                    justifyContent={ "space-between" }
70 ddc84a2f Schwobik
                >
71 06808454 Schwobik
                    <ApplicationHeading />
72 ddc84a2f Schwobik
                    <Heading
73
                        mt="10"
74
                        textAlign="center"
75
                        _dark={ {
76 1980ed09 Schwobik
                            color: "primary.500"
77 ddc84a2f Schwobik
                        } }
78 1980ed09 Schwobik
79 ddc84a2f Schwobik
                        fontWeight="bold"
80
                        size="xl"
81 5ed9692c Schwobik
                    >
82 ddc84a2f Schwobik
                        Log in
83
                    </Heading>
84
                    <Text
85
                        mt="1"
86
                        textAlign="center"
87
                    >
88
                        Please log in to continue
89
                    </Text>
90 06808454 Schwobik
                    { lastError && (
91
                        <Text
92
                            mt="1"
93
                            textAlign="center"
94
                            color="error.500"
95
                        >
96
                            { lastError }
97
                        </Text>
98
                    ) }
99 ddc84a2f Schwobik
100 06808454 Schwobik
                    <VStack space={ 3 } >
101 ddc84a2f Schwobik
                        <FormControl>
102
                            <FormControl.Label>Username</FormControl.Label>
103
                            <Input
104
                                // value={username}
105 ca53e9f1 Schwobik
                                textContentType={"username"}
106 1980ed09 Schwobik
                                rounded={ "xl" }
107 ca53e9f1 Schwobik
                                autoComplete={"username"}
108
                                onSubmitEditing={ () => { loginUser() } }
109 ddc84a2f Schwobik
                                onChangeText={ (username) => setUsername(username) }
110
                            />
111
                            <FormControl.Label>Password</FormControl.Label>
112 ca53e9f1 Schwobik
                            <Input
113
                                type="password"
114
                                autoComplete={ "password" }
115
                                id={ "password" }
116 1980ed09 Schwobik
                                returnKeyType={ "go" }
117
                                rounded={ "xl" }
118 ca53e9f1 Schwobik
                                onSubmitEditing={ () => loginUser() }
119
                                onChangeText={ (password) => setPassword(password) }
120 ddc84a2f Schwobik
                            />
121 1980ed09 Schwobik
                            <Button
122
                                mt="2"
123
                                bg={ "primary.500" }
124
                                onPress={ loginUser }
125
                                rounded={ "xl"}
126
                            >
127
                                Sign in
128
                            </Button>
129 ddc84a2f Schwobik
                        </FormControl>
130
                    </VStack>
131 06808454 Schwobik
                </VStack>
132 ddc84a2f Schwobik
            </Center>
133
        </KeyboardAvoidingView>
134 5c86e446 mschwob
    )
135 7e4b4e6a mschwob
}
136
137
export default LoginPage