hook reducer into auth/signup request service
This commit is contained in:
parent
285d9b7a46
commit
918b2ac5cb
5 changed files with 81 additions and 4 deletions
|
@ -9,6 +9,7 @@ div.main-wrapper {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
|
max-width: 20vw;
|
||||||
a {
|
a {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
// @flow
|
||||||
import { initState } from './init/stateReducer.init';
|
import { initState } from './init/stateReducer.init';
|
||||||
|
import { authReducer } from './auth/stateReducer.auth';
|
||||||
|
|
||||||
export type state = {
|
export type state = {
|
||||||
user: {}
|
user: {},
|
||||||
|
errors: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
type action = {
|
export type action = {
|
||||||
type: string
|
type: string,
|
||||||
|
message: ?string,
|
||||||
|
body: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const stateReducer = (state: state, action: action): state => {
|
export const stateReducer = (state: state, action: action): state => {
|
||||||
|
const errorStrippedState = stripErrors({...state});
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'INIT': return initState();
|
case 'INIT': return initState();
|
||||||
|
|
||||||
|
case 'AUTH': return authReducer(errorStrippedState, action);
|
||||||
|
|
||||||
default: return state;
|
default: return state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stripErrors(state: state): state {
|
||||||
|
return {...state, errors: {}}
|
||||||
|
}
|
|
@ -14,7 +14,6 @@ const signupService = async (formData) => {
|
||||||
.then(res => {
|
.then(res => {
|
||||||
return res;
|
return res;
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log(err)
|
|
||||||
return err;
|
return err;
|
||||||
});
|
});
|
||||||
return response;
|
return response;
|
||||||
|
|
Loading…
Reference in a new issue