hook reducer into auth/signup request service

This commit is contained in:
Sorrel Bri 2020-01-16 16:59:11 -08:00 committed by sorrelbri
parent 78ba0a035e
commit 9b02705a91
5 changed files with 81 additions and 4 deletions

View file

@ -9,6 +9,7 @@ div.main-wrapper {
display: flex;
flex-direction: column;
flex-grow: 1;
max-width: 20vw;
a {
display: block;
}

View 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;
}

View 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 () => {
})

View file

@ -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: {}}
}

View file

@ -14,7 +14,6 @@ const signupService = async (formData) => {
.then(res => {
return res;
}).catch(err => {
console.log(err)
return err;
});
return response;