Created
February 26, 2018 10:48
-
-
Save thedevdavid/874aa092520b1cebd7c346f575f6f53f to your computer and use it in GitHub Desktop.
Auth reducer
This file contains 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 types from '../actions/types'; | |
const initialState = { | |
loggedIn: false, | |
isFetching: false, | |
hasError: false, | |
errorMessage: '', | |
user: null | |
}; | |
export default (state = initialState, action) => { | |
switch (action.type) { | |
case types.LOGIN_START: { | |
return { | |
...state, | |
isFetching: true | |
}; | |
} | |
case types.LOGIN_FINISHED: { | |
const { user } = action; | |
return { | |
...state, | |
isFetching: false, | |
loggedIn: true, | |
user | |
}; | |
} | |
case types.LOGIN_ERROR: { | |
const { error } = action; | |
return { | |
...state, | |
isFetching: false, | |
loggedIn: false, | |
hasError: true, | |
user: null, | |
errorMessage: error | |
}; | |
} | |
case types.LOGOUT_START: { | |
return { | |
...state, | |
isFetching: true | |
}; | |
} | |
case types.LOGOUT_FINISHED: { | |
return { | |
...initialState | |
}; | |
} | |
case types.LOGOUT_ERROR: { | |
const { error } = action; | |
return { | |
...state, | |
isFetching: false, | |
loggedIn: true, | |
hasError: true, | |
errorMessage: error | |
}; | |
} | |
default: { | |
return state; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment