Skip to content

Instantly share code, notes, and snippets.

@talyssonoc
Last active May 15, 2019 20:09
Show Gist options
  • Save talyssonoc/a37d01dbf16a9d761706cf5109b7c052 to your computer and use it in GitHub Desktop.
Save talyssonoc/a37d01dbf16a9d761706cf5109b7c052 to your computer and use it in GitHub Desktop.
import Auth from '../domain/auth';
import { AUTH } from './actionTypes';
const States = {
PRISTINE: 'PRISTINE',
VALID: 'VALID',
INVALID: 'INVALID',
SUBMITTING: 'SUBMITTING',
SUCCESS: 'SUCCESS'
};
const initialState = {
currentState: States.PRISTINE,
data: {}
};
export const reducer = (state = initialState, action) => {
switch(action.type) {
case AUTH.UPDATE_AUTH_FIELD:
const newData = { ...state.data, ...action.data };
return {
...state,
// ...
data: newData,
currentState: Auth.isValid(newData) ? States.VALID : States.INVALID
};
case AUTH.SUBMIT_SIGN_IN:
if(state.currentState === States.INVALID) {
return state; // makes it impossible to submit if it's invalid
}
return {
...state,
// ...
currentState: States.SUBMITTING
};
case AUTH.SIGN_IN_SUCCESS:
return {
...state,
// ...
currentState: States.SUCCESS
};
}
return state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment