Projekt

Obecné

Profil

Stáhnout (4.48 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 ddc84a2f Schwobik
            h={ {
61
                base: "400px",
62
                lg: "auto"
63
            } }
64
        >
65 7410d6c1 Michal Schwob
66 ddc84a2f Schwobik
            <Center w="100%">
67 06808454 Schwobik
                <VStack
68 ddc84a2f Schwobik
                    p="2"
69 06808454 Schwobik
                    w="80%"
70
                    justifyContent={ "center" }
71 ddc84a2f Schwobik
                >
72 06808454 Schwobik
                    <ApplicationHeading />
73 ddc84a2f Schwobik
                    <Heading
74
                        mt="10"
75
                        textAlign="center"
76
                        _dark={ {
77 1980ed09 Schwobik
                            color: "primary.500"
78 ddc84a2f Schwobik
                        } }
79 1980ed09 Schwobik
80 ddc84a2f Schwobik
                        fontWeight="bold"
81
                        size="xl"
82 5ed9692c Schwobik
                    >
83 ddc84a2f Schwobik
                        Log in
84
                    </Heading>
85
                    <Text
86
                        mt="1"
87
                        textAlign="center"
88
                    >
89
                        Please log in to continue
90
                    </Text>
91 06808454 Schwobik
                    { lastError && (
92
                        <Text
93
                            mt="1"
94
                            textAlign="center"
95
                            color="error.500"
96
                        >
97
                            { lastError }
98
                        </Text>
99
                    ) }
100 ddc84a2f Schwobik
101 06808454 Schwobik
                    <VStack space={ 3 } >
102 ddc84a2f Schwobik
                        <FormControl>
103
                            <FormControl.Label>Username</FormControl.Label>
104
                            <Input
105
                                // value={username}
106 ca53e9f1 Schwobik
                                textContentType={"username"}
107 1980ed09 Schwobik
                                rounded={ "xl" }
108 ca53e9f1 Schwobik
                                autoComplete={"username"}
109
                                onSubmitEditing={ () => { loginUser() } }
110 ddc84a2f Schwobik
                                onChangeText={ (username) => setUsername(username) }
111
                            />
112
                            <FormControl.Label>Password</FormControl.Label>
113 ca53e9f1 Schwobik
                            <Input
114
                                type="password"
115
                                autoComplete={ "password" }
116
                                id={ "password" }
117 1980ed09 Schwobik
                                returnKeyType={ "go" }
118
                                rounded={ "xl" }
119 ca53e9f1 Schwobik
                                onSubmitEditing={ () => loginUser() }
120
                                onChangeText={ (password) => setPassword(password) }
121 ddc84a2f Schwobik
                            />
122 1980ed09 Schwobik
                            <Button
123
                                mt="2"
124
                                bg={ "primary.500" }
125
                                onPress={ loginUser }
126
                                rounded={ "xl"}
127
                            >
128
                                Sign in
129
                            </Button>
130 ddc84a2f Schwobik
                        </FormControl>
131
                    </VStack>
132 06808454 Schwobik
                </VStack>
133 ddc84a2f Schwobik
            </Center>
134
        </KeyboardAvoidingView>
135 5c86e446 mschwob
    )
136 7e4b4e6a mschwob
}
137
138
export default LoginPage