React -> upgrade forms / add register & login & logout operations

feature/2_forms
TBS093A 2021-02-17 23:48:24 +01:00
parent b2ecdfb807
commit 4ee3e84fb4
7 changed files with 58 additions and 12 deletions

View File

@ -14,6 +14,7 @@ const UserLoginForm = () => {
const passwordInput = React.createRef() const passwordInput = React.createRef()
const dispatch = useDispatch() const dispatch = useDispatch()
const { info } = useSelector( userAuthSelector )
let refList = [ let refList = [
usernameInput, usernameInput,
@ -58,6 +59,7 @@ const UserLoginForm = () => {
refList={ refList } refList={ refList }
action={ login } action={ login }
/> />
{ info }
</> </>
) )

View File

@ -15,6 +15,7 @@ const UserRegisterForm = () => {
const emailInput = React.createRef() const emailInput = React.createRef()
const dispatch = useDispatch() const dispatch = useDispatch()
const { info } = useSelector( userCrudSelector )
let refList = [ let refList = [
usernameInput, usernameInput,
@ -27,7 +28,7 @@ const UserRegisterForm = () => {
type: 'info', type: 'info',
action: 'Create', action: 'Create',
endpint: 'user/auth/login', endpint: 'user/auth/login',
button_value: 'Sign In' button_value: 'Sign Up'
}, },
{ {
type: 'text', type: 'text',
@ -66,6 +67,7 @@ const UserRegisterForm = () => {
refList={ refList } refList={ refList }
action={ register } action={ register }
/> />
{ info }
</> </>
) )

View File

@ -5,6 +5,7 @@ import { userAuthSelector } from '../../redux/slices/userAuthSlice'
import { userCrudSelector } from '../../redux/slices/userCrudSlice' import { userCrudSelector } from '../../redux/slices/userCrudSlice'
import userCrudAsyncThunk from '../../redux/asyncThunks/userCrudAsyncThunk' import userCrudAsyncThunk from '../../redux/asyncThunks/userCrudAsyncThunk'
import userAuthAsyncThunk from '../../redux/asyncThunks/userAuthAsyncThunk'
const __setShowGeneral = ( view, key, movements ) => { const __setShowGeneral = ( view, key, movements ) => {
@ -43,7 +44,7 @@ const __setShowGeneral = ( view, key, movements ) => {
new_move new_move
) )
} else if (view === 'model_view') { } else if (view === 'model_view') {
let new_move = movements.user_view.modelCrudView let new_move = movements.model_view.modelCrudView
new_move[key] = true new_move[key] = true
movements.model_view.setModelCrudView( movements.model_view.setModelCrudView(
new_move new_move
@ -86,7 +87,7 @@ const NavigationBar = ({ movements }) => {
const dispatch = useDispatch() const dispatch = useDispatch()
useEffect( () => { useEffect( () => {
if ( user_get !== {} && token !== '' && user.id > 0) if ( user_get === {} && token !== '' && user.id > 0)
dispatch( dispatch(
userCrudAsyncThunk.fetchGetOneUser( userCrudAsyncThunk.fetchGetOneUser(
{ {
@ -102,6 +103,14 @@ const NavigationBar = ({ movements }) => {
const [showRender, setShowRender] = useState(false) const [showRender, setShowRender] = useState(false)
const [showRenderFunc, setShowRenderFunc] = useState(false) const [showRenderFunc, setShowRenderFunc] = useState(false)
const logout = async () => {
dispatch(
userAuthAsyncThunk.fetchLogout(
token
)
)
}
return( return(
<> <>
<div> <div>
@ -175,8 +184,8 @@ const NavigationBar = ({ movements }) => {
</div> </div>
</div> </div>
</div> </div>
<div> <div onClick={ () => logout() }>
Logout Sign Out
</div> </div>
</div> </div>
</> </>

View File

@ -1,13 +1,33 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect } from 'react'
import UserLoginForm from '../../../components/forms/user_auth/userLogin' import UserLoginForm from '../../../components/forms/user_auth/userLogin'
import UserRegisterForm from '../../../components/forms/user_auth/userRegister'
const UserAuthIndex = () => { const UserAuthIndex = () => {
const [swapForm, setSwapForm] = useState(true)
const handleSwap = (event) => {
event.preventDefault()
setSwapForm(
!swapForm
)
}
return ( return (
<div> <div>
<UserLoginForm /> { swapForm
? <UserLoginForm />
: <UserRegisterForm />
}
<button onClick={ (event) => handleSwap(event) }>
{
swapForm
? 'Sign Up'
: 'Sign In'
}
</button>
</div> </div>
) )
} }

View File

@ -51,8 +51,11 @@ const _put = async (endpoint, objectId, body, token) => {
} }
const _delete = async (endpoint, objectId, token) => { const _delete = async (endpoint, objectId, token) => {
let slash = '/'
if ( objectId === '' )
slash = ''
return await responseAbstract( return await responseAbstract(
endpoint + objectId + '/', endpoint + objectId + slash,
'DELETE', 'DELETE',
token, token,
defaultBody defaultBody
@ -98,7 +101,6 @@ const headerBuilder = (url, method, token, body) => {
data: JSON.stringify(body), data: JSON.stringify(body),
}) })
} }
console.log(headers)
return headers return headers
} }

View File

@ -10,7 +10,8 @@ const userAuthSlice = createSlice(
id: 0, id: 0,
username: '', username: '',
email: '' email: ''
} },
info: ''
}, },
reducers: {}, reducers: {},
extraReducers: { extraReducers: {

View File

@ -9,7 +9,8 @@ const userCrudSlice = createSlice(
user_get: {}, user_get: {},
user_register: {}, user_register: {},
user_update: {}, user_update: {},
user_delete: '' user_delete: '',
info: ''
}, },
reducers: {}, reducers: {},
extraReducers: { extraReducers: {
@ -20,7 +21,16 @@ const userCrudSlice = createSlice(
state.user_get = action.payload.data state.user_get = action.payload.data
}, },
[userCrudAsyncThunk.fetchRegister.fulfilled.type]: (state, action) => { [userCrudAsyncThunk.fetchRegister.fulfilled.type]: (state, action) => {
state.user_register = action.payload.data try {
state.user_register = {
id: action.payload.data.id,
username: action.payload.data.username,
email: action.payload.data.email
}
state.info = 'register success'
} catch {
state.info = 'register failed'
}
}, },
[userCrudAsyncThunk.fetchUpdateUser.fulfilled.type]: (state, action) => { [userCrudAsyncThunk.fetchUpdateUser.fulfilled.type]: (state, action) => {
state.user_update = action.payload.data state.user_update = action.payload.data