Projekt

Obecné

Profil

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