From df110d187ab9d6243625740e941fd080f4581605 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Thu, 16 Jan 2020 16:59:11 -0800 Subject: [PATCH] hook reducer into auth/signup request service --- .../components/MainWrapper/MainWrapper.scss | 1 + .../src/reducers/auth/stateReducer.auth.js | 41 +++++++++++++++++++ .../reducers/auth/stateReducer.auth.test.js | 24 +++++++++++ play-node-go/src/reducers/stateReducer.js | 18 ++++++-- play-node-go/src/services/authServices.js | 1 - 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 play-node-go/src/reducers/auth/stateReducer.auth.js create mode 100644 play-node-go/src/reducers/auth/stateReducer.auth.test.js diff --git a/play-node-go/src/components/MainWrapper/MainWrapper.scss b/play-node-go/src/components/MainWrapper/MainWrapper.scss index 2f4d07c..e7678c7 100644 --- a/play-node-go/src/components/MainWrapper/MainWrapper.scss +++ b/play-node-go/src/components/MainWrapper/MainWrapper.scss @@ -9,6 +9,7 @@ div.main-wrapper { display: flex; flex-direction: column; flex-grow: 1; + max-width: 20vw; a { display: block; } diff --git a/play-node-go/src/reducers/auth/stateReducer.auth.js b/play-node-go/src/reducers/auth/stateReducer.auth.js new file mode 100644 index 0000000..96ef9e4 --- /dev/null +++ b/play-node-go/src/reducers/auth/stateReducer.auth.js @@ -0,0 +1,41 @@ +// @flow +import type { state, action } from '../stateReducer'; + +import authServices from '../../services/authServices'; + +export const authReducer = (state: state, action: action):state => { + switch (action.message) { + case 'LOGIN': + return loginReducer(state, action); + + case 'SIGNUP': + return signupReducer(state, action); + + case 'LOGOUT': + return state; + + default: + return state; + } +} + +function loginReducer(state: state, action: action): state { + const userCredentials = action.body; + + + 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; + 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'}}; + + // returnstate; +} \ No newline at end of file diff --git a/play-node-go/src/reducers/auth/stateReducer.auth.test.js b/play-node-go/src/reducers/auth/stateReducer.auth.test.js new file mode 100644 index 0000000..e443c1e --- /dev/null +++ b/play-node-go/src/reducers/auth/stateReducer.auth.test.js @@ -0,0 +1,24 @@ +import {authReducer} from './stateReducer.auth'; + +const newCredentials = { + username: 'newUsername7', + email: 'example7@test.com', + password: 'newPass1', + confirmPassword: 'newPass1' +} + +const signupAction = { + body: newCredentials, + message: 'SIGNUP', + type: 'AUTH' +} + +const state = { + user: null, + errors: {} +} + +it('return ', async () => { + + +}) \ No newline at end of file diff --git a/play-node-go/src/reducers/stateReducer.js b/play-node-go/src/reducers/stateReducer.js index b405a93..6b3f631 100644 --- a/play-node-go/src/reducers/stateReducer.js +++ b/play-node-go/src/reducers/stateReducer.js @@ -1,18 +1,30 @@ // @flow import { initState } from './init/stateReducer.init'; +import { authReducer } from './auth/stateReducer.auth'; export type state = { - user: {} + user: {}, + errors: {} } -type action = { - type: string +export type action = { + type: string, + message: ?string, + body: {} } export const stateReducer = (state: state, action: action): state => { + const errorStrippedState = stripErrors({...state}); + switch (action.type) { case 'INIT': return initState(); + + case 'AUTH': return authReducer(errorStrippedState, action); default: return state; } +} + +function stripErrors(state: state): state { + return {...state, errors: {}} } \ No newline at end of file diff --git a/play-node-go/src/services/authServices.js b/play-node-go/src/services/authServices.js index 19abf05..2a27deb 100644 --- a/play-node-go/src/services/authServices.js +++ b/play-node-go/src/services/authServices.js @@ -14,7 +14,6 @@ const signupService = async (formData) => { .then(res => { return res; }).catch(err => { - console.log(err) return err; }); return response;