Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6129910f

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

Show dialog in login page

re #9628

Zobrazit rozdíly:

frontend/src/features/Auth/LoginDialog.tsx
1
import { Fragment, FunctionComponent, useState } from 'react'
1
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
2 2
import Dialog, { DialogProps } from '@mui/material/Dialog'
3 3
import {
4 4
    Button,
5 5
    DialogContent,
6 6
    Link,
7
    Stack,
8 7
    TextField,
9 8
    Typography,
10 9
} from '@mui/material'
11 10
import { useFormik } from 'formik'
12 11
import * as yup from 'yup'
13
import { useDispatch } from 'react-redux'
14
import { showNotification } from '../Notification/notificationSlice'
15
import axiosInstance from '../../api/api'
16
import { Link as RouterLink } from 'react-router-dom'
12
import { useDispatch, useSelector } from 'react-redux'
13
import { Link as RouterLink, useNavigate } from 'react-router-dom'
14
import { logIn } from './userThunks'
15
import { RootState } from '../redux/store'
16
import { resetLoggingIn } from './userSlice'
17 17

  
18 18
export interface CreateIndexDialogProps {
19 19
    maxWidth?: DialogProps['maxWidth']
......
22 22
const RegisterDialog: FunctionComponent<CreateIndexDialogProps> = ({
23 23
    maxWidth,
24 24
}) => {
25
    const [open, setOpen] = useState(false)
26
    const [submitButtonEnabled, setSubmitButtonEnabled] = useState(true)
25
    const [open, setOpen] = useState(true)
27 26

  
28 27
    const dispatch = useDispatch()
29

  
30
    const hideDialog = () => {
31
        setOpen(false)
32
    }
33

  
34
    const showDialog = () => {
35
        setOpen(true)
36
    }
37

  
28
    const navigate = useNavigate()
29
    dispatch(resetLoggingIn())
38 30
    const validationSchema = yup.object().shape({
39
        email: yup.string().email().required('Email is required'),
31
        username: yup.string().required('Username is required'),
40 32
        password: yup.string().required('Password is required'),
41 33
    })
42 34

  
35
    const isLoggingIn = useSelector(
36
        (state: RootState) => state.user.isLoggingIn
37
    )
38

  
43 39
    const formik = useFormik({
44 40
        initialValues: {
45
            name: '',
41
            username: '',
46 42
            password: '',
47 43
        },
48 44
        validationSchema,
49
        onSubmit: async (values) => {
50
            setSubmitButtonEnabled(false)
51
            let userRegistered = false
52
            try {
53
                const { status } = await axiosInstance.post(
54
                    `/users/${values.name}`,
55
                    values
56
                )
57

  
58
                switch (status) {
59
                    case 200:
60
                        dispatch({
61
                            message: 'User was created successfully',
62
                            severity: 'success',
63
                        })
64
                        userRegistered = true
65
                        break
66
                    case 204:
67
                        dispatch(
68
                            showNotification({
69
                                message: 'User already exists',
70
                                severity: 'error',
71
                            })
72
                        )
73
                        break
74
                    default:
75
                        dispatch({
76
                            message:
77
                                'Unknown error ocurred, the user was not registered. Please try again later',
78
                            severity: 'error',
79
                        })
80
                }
81
            } catch (err: any) {
82
                dispatch(
83
                    showNotification({
84
                        message: 'The user could not be registered 😥',
85
                        severity: 'error',
86
                    })
87
                )
88
            }
89

  
90
            if (userRegistered) {
91
                onClose()
92
            }
93

  
94
            // Always fetch new indices
95
            // TODO actually fetch the users
96
            // dispatch(fetchUsers())
97
            setSubmitButtonEnabled(true)
45
        onSubmit: () => {
46
            dispatch(
47
                logIn({
48
                    username: formik.values.username,
49
                    password: formik.values.password,
50
                })
51
            )
98 52
        },
99 53
    })
100 54

  
101
    // Method called on closing the dialog
102
    const onClose = () => {
103
        hideDialog()
55
    const onCancel = () => {
104 56
        formik.resetForm()
57
        navigate('/')
105 58
    }
106 59

  
107 60
    return (
108 61
        <Fragment>
109
            <Stack
110
                direction="row"
111
                justifyContent="flex-end"
112
                alignItems="center"
113
            >
114
                <Button variant="outlined" color="primary" onClick={showDialog}>
115
                    Create new Index
116
                </Button>
117
            </Stack>
118

  
119 62
            <Dialog
120 63
                open={open}
121 64
                fullWidth={true}
122
                onClose={onClose}
123
                maxWidth={maxWidth || 'lg'}
65
                onClose={onCancel}
66
                maxWidth="md"
124 67
            >
125 68
                <Typography sx={{ ml: 2, mt: 2 }} variant="h5" fontWeight="600">
126 69
                    Login
......
130 73
                        <TextField
131 74
                            fullWidth
132 75
                            label="Name"
133
                            name="name"
76
                            name="username"
134 77
                            sx={{ mb: 2 }}
135
                            value={formik.values.name}
78
                            value={formik.values.username}
136 79
                            onChange={formik.handleChange}
137 80
                            error={
138
                                Boolean(formik.errors.name) &&
139
                                formik.touched.name
81
                                Boolean(formik.errors.username) &&
82
                                formik.touched.username
140 83
                            }
141 84
                            helperText={
142
                                formik.errors.name &&
143
                                formik.touched.name &&
144
                                formik.errors.name
85
                                formik.errors.username &&
86
                                formik.touched.username &&
87
                                formik.errors.username
145 88
                            }
146 89
                        />
147 90
                        <TextField
......
166 109
                            <Button
167 110
                                type="submit"
168 111
                                variant="contained"
169
                                disabled={!submitButtonEnabled}
170 112
                                fullWidth
113
                                disabled={isLoggingIn}
171 114
                            >
172 115
                                Log in
173 116
                            </Button>
174 117
                        </Fragment>
175
                    </form> 
118
                    </form>
176 119

  
177
                    <Link component={RouterLink} to="/resetPassword">Forgot password?</Link>
120
                    <Link component={RouterLink} to="/resetPassword">
121
                        Forgot password?
122
                    </Link>
178 123
                </DialogContent>
179 124
            </Dialog>
180 125
        </Fragment>

Také k dispozici: Unified diff