Projekt

Obecné

Profil

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