From 8c6e38ec668896b50b284f5b3bb063888b9871af Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Fri, 17 Jan 2020 12:21:43 -0800 Subject: [PATCH] hook reducer into signup post response --- packages/server/play-node-go/src/App.js | 2 + .../src/components/Signup/Signup.js | 37 +++++++++++++++++-- .../src/pages/Home/HomeSidebar.js | 4 +- .../src/reducers/auth/stateReducer.auth.js | 32 ++++++++++------ .../play-node-go/src/reducers/stateReducer.js | 5 ++- 5 files changed, 61 insertions(+), 19 deletions(-) diff --git a/packages/server/play-node-go/src/App.js b/packages/server/play-node-go/src/App.js index 8edaeda..df061eb 100644 --- a/packages/server/play-node-go/src/App.js +++ b/packages/server/play-node-go/src/App.js @@ -31,6 +31,7 @@ function App() { .catch(err => setError([...error, err])) .then(data => setFetchData(data)) }) + useEffect(() => { socket.emit('connect'); @@ -38,6 +39,7 @@ function App() { socket.on('connect_error', err => setError([...error, err])); socket.on('error', err => setError([...error, err])) }) + return ( diff --git a/packages/server/play-node-go/src/components/Signup/Signup.js b/packages/server/play-node-go/src/components/Signup/Signup.js index f6a6361..e440693 100644 --- a/packages/server/play-node-go/src/components/Signup/Signup.js +++ b/packages/server/play-node-go/src/components/Signup/Signup.js @@ -1,15 +1,35 @@ import React, { useState } from 'react'; import './Signup.scss'; +import authServices from '../../services/authServices'; const Signup = (props) => { const [ username, setUsername ] = useState(''); + const [ email, setEmail ] = useState(''); const [ password, setPassword ] = useState(''); const [ confirmPassword, setConfirmPassword ] = useState(''); + const handleSubmit = async e => { + e.preventDefault(); + const signupResponse = await authServices.signupService({ + username, + email, + password, + confirmPassword + }) + return props.dispatch({ + type: 'AUTH', + message: 'SIGNUP', + body: signupResponse + }) + } + return (
-
- + handleSubmit(e)} + > + { default="username" /> + + setEmail(e.target.value)} + default="email" + /> + setPassword(e.target.value)} default="" /> @@ -32,7 +61,7 @@ const Signup = (props) => { setConfirmPassword(e.target.value)} default="" /> diff --git a/packages/server/play-node-go/src/pages/Home/HomeSidebar.js b/packages/server/play-node-go/src/pages/Home/HomeSidebar.js index 3679643..ca4b8b4 100644 --- a/packages/server/play-node-go/src/pages/Home/HomeSidebar.js +++ b/packages/server/play-node-go/src/pages/Home/HomeSidebar.js @@ -10,9 +10,9 @@ const HomeSidebar = (props) => { ); diff --git a/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js b/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js index 96ef9e4..5715a14 100644 --- a/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js +++ b/packages/server/play-node-go/src/reducers/auth/stateReducer.auth.js @@ -7,7 +7,7 @@ export const authReducer = (state: state, action: action):state => { switch (action.message) { case 'LOGIN': return loginReducer(state, action); - + case 'SIGNUP': return signupReducer(state, action); @@ -26,16 +26,26 @@ function loginReducer(state: state, action: action): state { return state; } -async function signupReducer(state: state, action: action): state { - const userCredentials = action.body; - - const signupResponse = await authServices.signupService(userCredentials); - const errors = signupResponse.response ? signupResponse.response.data.errors : null; +function signupReducer(state: state, action: action): state { + const signupResponse = action.body; + let error; + + if (signupResponse.response) { + error = signupResponse.response.data.errors; + } let responseUser; - if (signupResponse.data) responseUser = {...signupResponse.data} - if (errors) return {...state, errors: {authError: errors} }; - if (responseUser) return {...state, user: responseUser }; - return {...state, errors: {requestError: 'something went wrong'}}; + if (signupResponse.data) { + responseUser = {...signupResponse.data} + } - // returnstate; + if (error) { + const errors = error.reduce((errorObject, error) => errorObject[Object.keys(error)[0] = error]) + return {...state, errors }; + } + + if (responseUser) { + return {...state, user: responseUser }; + } + + return {...state, errors: {requestError: 'something went wrong'}}; } \ No newline at end of file diff --git a/packages/server/play-node-go/src/reducers/stateReducer.js b/packages/server/play-node-go/src/reducers/stateReducer.js index 6b3f631..4bbd676 100644 --- a/packages/server/play-node-go/src/reducers/stateReducer.js +++ b/packages/server/play-node-go/src/reducers/stateReducer.js @@ -15,11 +15,12 @@ export type action = { export const stateReducer = (state: state, action: action): state => { const errorStrippedState = stripErrors({...state}); - + console.log(action) switch (action.type) { case 'INIT': return initState(); - case 'AUTH': return authReducer(errorStrippedState, action); + case 'AUTH': + return authReducer(errorStrippedState, action); default: return state; }