Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 5ed9692c

Přidáno uživatelem Schwobik před asi 2 roky(ů)

#re 10171
Demo of application login with redux, axios and nativeBase. Tested

Zobrazit rozdíly:

src/pages/LoginPage.tsx
1 1
import { Center, Box, Heading, VStack, FormControl, Link, Input, Button, HStack, Text } from "native-base"
2
import { axiosInstance } from "../api/api"
2
import { useState } from "react"
3
import { useDispatch, useSelector } from "react-redux"
4
import { login } from "../stores/actions/userThunks"
5
import { AppDispatch, RootState } from "../stores/store"
3 6

  
4
let text: number = 199
5
const fakeLogin = async () => {
6
    text = 101
7
    axiosInstance.post(
8
        '/api/login/',
9
        {"username": "viktorie", "password": "Golem123."}
10
    ).then((response) => {
11
        text = response.status
12
        console.log(response)
13
    })
14
}
15 7

  
16
const fakeRequest = async () => {
17
    text = 101
18
    console.log("request")
19
    axiosInstance.get(
20
        '/api/users'
21
    ).then((response) => {
22
        text = response.status
23
        console.log(response)
24
    })
25
}
26 8
const LoginPage = () => {
9
    const [username, setUsername] = useState("")
10
    const [password, setPassword] = useState("")
11
    const status = useSelector((state: RootState) => state.user.loggedIn)
12

  
13
    const dispatch = useDispatch<AppDispatch>()
14

  
15
    const loginUser = () => {
16
        dispatch(login({ username, password }))
17
        //TODO - add error handling
18
        //TODO add redirect to home page
19
    }
20

  
27 21
    return (
28 22
        <Center w="100%">
29 23
            <Box safeArea p="2" py="8" w="90%" maxW="290">
......
40 34

  
41 35
                <VStack space={3} mt="5">
42 36
                    <FormControl>
43
                        <FormControl.Label>Email ID</FormControl.Label>
44
                        <Input />
37
                        <FormControl.Label>Username</FormControl.Label>
38
                        <Input
39
                            // value={username}
40
                            onChangeText={(username) => setUsername(username)}
41
                        />
45 42
                    </FormControl>
46 43
                    <FormControl>
47 44
                        <FormControl.Label>Password</FormControl.Label>
48
                        <Input type="password" />
45
                        <Input type="password"
46
                            // value={password}
47
                            onChangeText={(password) => setPassword(password)}
48
                        />
49 49
                        <Link _text={{
50 50
                            fontSize: "xs",
51 51
                            fontWeight: "500",
......
54 54
                            Forget Password?
55 55
                        </Link>
56 56
                    </FormControl>
57
                    <Button mt="2" colorScheme="indigo">
57
                    <Button
58
                        mt="2"
59
                        colorScheme="indigo"
60
                        onPress={() => loginUser()}
61
                    >
58 62
                        Sign in
59 63
                    </Button>
60 64
                    <HStack mt="6" justifyContent="center">

Také k dispozici: Unified diff