hook reducer into auth/signup request service
This commit is contained in:
parent
3bbc272743
commit
df110d187a
5 changed files with 81 additions and 4 deletions
|
@ -9,6 +9,7 @@ div.main-wrapper {
|
|||
display: flex;
|
||||
flex-direction: column;
|
||||
flex-grow: 1;
|
||||
max-width: 20vw;
|
||||
a {
|
||||
display: block;
|
||||
}
|
||||
|
|
41
play-node-go/src/reducers/auth/stateReducer.auth.js
Normal file
41
play-node-go/src/reducers/auth/stateReducer.auth.js
Normal file
|
@ -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;
|
||||
}
|
24
play-node-go/src/reducers/auth/stateReducer.auth.test.js
Normal file
24
play-node-go/src/reducers/auth/stateReducer.auth.test.js
Normal file
|
@ -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 () => {
|
||||
|
||||
|
||||
})
|
|
@ -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: {}}
|
||||
}
|
|
@ -14,7 +14,6 @@ const signupService = async (formData) => {
|
|||
.then(res => {
|
||||
return res;
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
return err;
|
||||
});
|
||||
return response;
|
||||
|
|
Loading…
Reference in a new issue