Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2d3a4e2b

Přidáno uživatelem Michal Schwob před více než 2 roky(ů)

Fixed registering a closing the dialog after success
re #9627

Zobrazit rozdíly:

frontend/src/features/Auth/Register.tsx
1 1
import { Fragment, useEffect } from 'react'
2
import { useSelector } from 'react-redux'
2
import {useDispatch, useSelector} from 'react-redux'
3 3
import { useNavigate } from 'react-router-dom'
4 4
import { RootState } from '../redux/store'
5 5
import LoginDialog from './LoginDialog'
6 6
import NotAuthorized from "../NotAuthorized/NotAuthorized"
7 7
import RegisterDialog from "./RegisterDialog"
8
import {resetIsRegistered} from "./userSlice"
8 9

  
9 10

  
10 11
const Register = () => {
11 12
    const isAdmin = useSelector(
12 13
        (state: RootState) => state.user.roles.includes("ROLE_ADMIN")
13 14
    )
15
    const isRegistered = useSelector(
16
        (state: RootState) => state.user.isRegistered
17
    )
18

  
19
    const dispatch = useDispatch()
20

  
21
    // Redirect to home if the user is logged in
22
    const navigate = useNavigate()
23
    useEffect(() => {
24
        if (isRegistered) {
25
            dispatch(resetIsRegistered())
26
            navigate('/administration')
27
        }
28
    }, [isRegistered, navigate])
14 29

  
15 30
    return (
16 31
        <Fragment>
frontend/src/features/Auth/RegisterDialog.tsx
15 15
import { RootState } from '../redux/store'
16 16
import { resetLoggingIn } from './userSlice'
17 17
import { Box } from '@mui/system'
18
import {unwrapResult} from "@reduxjs/toolkit"
18 19

  
19 20
export interface RegisterDialogProps {
20 21
    maxWidth?: DialogProps['maxWidth']
......
45 46
        },
46 47
        validationSchema,
47 48
        onSubmit: () => {
48
            dispatch(
49
            const response = dispatch(
49 50
                register({
50
                    username: formik.values.username,
51
                    name: formik.values.username,
51 52
                    passwords: {
52 53
                        password: formik.values.password,
53 54
                        confirmationPassword: formik.values.confirmationPassword,
......
60 61
                    email: formik.values.email,
61 62
                })
62 63
            )
64

  
63 65
        },
64 66
    })
65 67

  
frontend/src/features/Auth/userSlice.ts
1 1
import { createSlice } from '@reduxjs/toolkit'
2 2
import { persistReducer } from 'redux-persist'
3 3
import storage from 'redux-persist/lib/storage'
4
import { logIn } from './userThunks'
4
import {logIn, register} from './userThunks'
5 5

  
6 6
export interface UserState {
7 7
    accessToken?: string
......
10 10
    roles: string[]
11 11
    isLoggingIn: boolean
12 12
    isLoggedIn: boolean
13
    lastErr?: string // consumable for errors during thunks
13
    lastErr?: string// consumable for errors during thunks
14
    isRegistered: boolean
14 15
}
15 16

  
16 17
const persistConfig = {
......
24 25
    isLoggedIn: false,
25 26
    isLoggingIn: false,
26 27
    username: '',
28
    isRegistered: false,
27 29
}
28 30

  
29 31
export const userSlice = createSlice({
......
43 45
        }),
44 46
        setUserState: (state, action) => ({ ...state, ...action.payload }),
45 47
        resetLoggingIn: (state) => ({ ...state, isLoggingIn: false }),
48
        resetIsRegistered: (state) => ({ ...state, isRegistered: false }),
46 49
    },
47 50

  
48 51
    // Thunks
......
58 61
        builder.addCase(logIn.pending, (state) => {
59 62
            return { ...state, isLoggingIn: true }
60 63
        })
64
        builder.addCase(register.fulfilled, (state, action) => {
65
            return {...state, isRegistered: true}
66
        })
61 67
    },
62 68
})
63 69

  
64 70
const userReducer = persistReducer(persistConfig, userSlice.reducer)
65 71

  
66
export const { logout, refreshTokens, setErr, setUserState, resetLoggingIn } =
72
export const { logout, refreshTokens, setErr, setUserState, resetLoggingIn, resetIsRegistered } =
67 73
    userSlice.actions
68 74

  
69 75
export default userReducer
frontend/src/features/Auth/userThunks.ts
16 16
}
17 17

  
18 18
export interface UserRegister {
19
    username: string,
19
    name: string,
20 20
    passwords: {
21 21
        password: string,
22 22
        confirmationPassword: string,
......
58 58
                username: sub,
59 59
                roles: authorities,
60 60
                isLoggingIn: false,
61
                isLoggedIn: true
61
                isLoggedIn: true,
62
                isRegistered: false
62 63
            }
63 64
            
64 65
            return userState
......
77 78
            const { data, status } = await axiosInstance.post('/users', userDto)
78 79
            if (status !== 200) {
79 80
                // TODO read API err
80
                return Promise.reject(loginError)
81
                return Promise.reject(registerError)
81 82
            }
82 83
            return status
83 84
        } catch (err: any) {
84
            return Promise.reject(loginError)
85
            return Promise.reject(registerError)
85 86
        }
86 87
    }
87 88
)

Také k dispozici: Unified diff