Projekt

Obecné

Profil

Stáhnout (4.58 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Center,
3
    Heading,
4
    VStack,
5
    FormControl,
6
    Input,
7
    Button,
8
    Text,
9
    KeyboardAvoidingView,
10
    useToast
11
} from "native-base"
12
import { useCallback, useContext, useEffect, useState } from "react"
13
import { useDispatch, useSelector } from "react-redux"
14
import { checkAuth, login } from "../stores/actions/userThunks"
15
import { AppDispatch, RootState } from "../stores/store"
16
import { Platform } from "react-native"
17
import { log } from "../logging/logger"
18
import { ApplicationHeading } from "../components/reusables/ApplicationHeading"
19
import { InfoToast } from "../components/toast/InfoToast"
20
import { ErrorToast } from "../components/toast/ErrorToast"
21
import { SuccessToast } from "../components/toast/SuccessToast"
22

    
23

    
24
const LoginPage = () => {
25
    const [username, setUsername] = useState("")
26
    const [password, setPassword] = useState("")
27

    
28
    const lastError = useSelector((state: RootState) => state.user.lastError)
29
    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

    
45
    const dispatch = useDispatch<AppDispatch>()
46

    
47
    const loginUser = () => {
48
        log.debug("LoginPage", "loginUser", "dispatching login")
49
        dispatch(login({ username, password }))
50
    }
51

    
52
    useEffect(() => {
53
        dispatch(checkAuth())
54
    }, [])
55

    
56
    return (
57
        <KeyboardAvoidingView
58
            behavior={ Platform.OS === "ios" ? "padding" : "height" }
59
            flex={ 1 }
60
            justifyContent={ "center" }
61
            h={ {lg: "auto"} }
62
            keyboardVerticalOffset={ 100 }
63
        >
64
            <Center w="100%">
65
                <VStack
66
                    p="2"
67
                    w="80%"
68
                    justifyContent={ "space-between" }
69
                >
70
                    <ApplicationHeading />
71
                    <Heading
72
                        mt="10"
73
                        textAlign="center"
74
                        _dark={ {
75
                            color: "primary.500"
76
                        } }
77

    
78
                        fontWeight="bold"
79
                        size="xl"
80
                    >
81
                        Log in
82
                    </Heading>
83
                    <Text
84
                        mt="1"
85
                        textAlign="center"
86
                    >
87
                        Please log in to continue
88
                    </Text>
89
                    { lastError && (
90
                        <Text
91
                            mt="1"
92
                            textAlign="center"
93
                            color="error.500"
94
                        >
95
                            { lastError }
96
                        </Text>
97
                    ) }
98

    
99
                    <VStack space={ 3 } >
100
                        <FormControl>
101
                            <FormControl.Label>Username</FormControl.Label>
102
                            <Input
103
                                // value={username}
104
                                textContentType={"username"}
105
                                rounded={ "xl" }
106
                                autoComplete={"username"}
107
                                onSubmitEditing={ () => { loginUser() } }
108
                                onChangeText={ (username) => setUsername(username) }
109
                            />
110
                            <FormControl.Label>Password</FormControl.Label>
111
                            <Input
112
                                type="password"
113
                                autoComplete={ "password" }
114
                                id={ "password" }
115
                                returnKeyType={ "go" }
116
                                rounded={ "xl" }
117
                                onSubmitEditing={ () => loginUser() }
118
                                onChangeText={ (password) => setPassword(password) }
119
                            />
120
                            <Button
121
                                mt="2"
122
                                bg={ "primary.500" }
123
                                onPress={ loginUser }
124
                                rounded={ "xl"}
125
                            >
126
                                Sign in
127
                            </Button>
128
                        </FormControl>
129
                    </VStack>
130
                </VStack>
131
            </Center>
132
        </KeyboardAvoidingView>
133
    )
134
}
135

    
136
export default LoginPage
(3-3/8)