Projekt

Obecné

Profil

Stáhnout (4.62 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Center,
3
    Box,
4
    Heading,
5
    VStack,
6
    FormControl,
7
    Link,
8
    Input,
9
    Button,
10
    HStack,
11
    Text,
12
    KeyboardAvoidingView, useToast, ScrollView
13
} from "native-base"
14
import { useCallback, useEffect, useState } from "react"
15
import { useDispatch, useSelector } from "react-redux"
16
import { checkAuth, login } from "../stores/actions/userThunks"
17
import { AppDispatch, RootState } from "../stores/store"
18
import { Platform } from "react-native"
19
import { log } from "../logging/logger"
20
import * as SplashScreen from "expo-splash-screen"
21
import { ApplicationHeading } from "../components/reusables/ApplicationHeading"
22
import {consumeError} from "../stores/reducers/userSlice"
23

    
24

    
25
const LoginPage = () => {
26
    const [username, setUsername] = useState("")
27
    const [password, setPassword] = useState("")
28
    const lastError = useSelector((state: RootState) => state.user.lastError)
29

    
30
    const dispatch = useDispatch<AppDispatch>()
31

    
32
    const toast = useToast()
33
    const loginUser = () => {
34
        log.debug("LoginPage", "loginUser", "dispatching login")
35
        dispatch(login({username, password}))
36
        //TODO - add error handling
37
    }
38

    
39
    useEffect(() => {
40
        dispatch(checkAuth())
41
    }, [])
42

    
43
    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
    return (
56
        <KeyboardAvoidingView
57
            behavior={ Platform.OS === "ios" ? "padding" : "height" }
58
            flex={ 1 }
59
            justifyContent={ "center" }
60
            h={ {lg: "auto"} }
61
            // behavior={ "padding" }
62
            keyboardVerticalOffset={ 100 }
63
        >
64

    
65
            <Center w="100%">
66
                <VStack
67
                    p="2"
68
                    w="80%"
69
                    justifyContent={ "space-between" }
70
                >
71
                    <ApplicationHeading />
72
                    <Heading
73
                        mt="10"
74
                        textAlign="center"
75
                        _dark={ {
76
                            color: "primary.500"
77
                        } }
78

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

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

    
139
export default LoginPage
(3-3/7)