Last active
May 15, 2019 20:09
-
-
Save talyssonoc/a37d01dbf16a9d761706cf5109b7c052 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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