Created
January 14, 2019 00:03
-
-
Save davingee/129c3f79fd8d15ffb047d9dbf5ec45ca to your computer and use it in GitHub Desktop.
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
d |
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 { createActions } from 'reduxsauce' | |
/** | |
* We use reduxsauce's `createActions()` helper to easily create redux actions. | |
* | |
* Keys are action names and values are the list of parameters for the given action. | |
* | |
* Action names are turned to SNAKE_CASE into the `Types` variable. This can be used | |
* to listen to actions: | |
* | |
* - to trigger reducers to update the state, for example in App/Stores/Example/Reducers.js | |
* - to trigger sagas, for example in App/Sagas/index.js | |
* | |
* Actions can be dispatched: | |
* | |
* - in React components using `dispatch(...)`, for example in App/App.js | |
* - in sagas using `yield put(...)`, for example in App/Sagas/ExampleSaga.js | |
* | |
* @see https://github.com/infinitered/reduxsauce#createactions | |
*/ | |
const { Types, Creators } = createActions({ | |
// Fetch the current weather temperature | |
loginUser: null, | |
// The operation has started and is loading | |
loginUserLoading: null, | |
// The temperature was successfully fetched | |
loginUserSuccess: ['users'], | |
// An error occurred | |
loginUserFailure: ['errorMessage'], | |
signupUser: null, | |
// The operation has started and is loading | |
signupUserLoading: null, | |
// The temperature was successfully fetched | |
signupUserSuccess: ['users'], | |
// An error occurred | |
signupUserFailure: ['errorMessage'], | |
logoutUser: null, | |
// The operation has started and is loading | |
logoutUserLoading: null, | |
// The temperature was successfully fetched | |
logoutUserSuccess: ['users'], | |
// An error occurred | |
logoutUserFailure: ['errorMessage'], | |
}) | |
export const RepoTypes = Types | |
export default Creators | |
/** | |
* Reducers specify how the application's state changes in response to actions sent to the store. | |
* | |
* @see https://redux.js.org/basics/reducers | |
*/ | |
import { INITIAL_STATE } from './InitialState' | |
import { createReducer } from 'reduxsauce' | |
import { RepoTypes } from './Actions' | |
export const loginUserLoading = (state) => | |
state.merge({ | |
loading: true, | |
user: {}, | |
}) | |
export const loginUserSuccess = (state, { user }) => | |
state.merge({ | |
loading: false, | |
user: user, | |
ErrorMessage: null, | |
}) | |
export const loginUserFailure = (state, { errorMessage }) => | |
state.merge({ | |
loading: false, | |
user: {}, | |
ErrorMessage: errorMessage, | |
}) | |
export const signupUserLoading = (state) => | |
state.merge({ | |
loading: true, | |
user: {}, | |
}) | |
export const signupUserSuccess = (state, { user }) => | |
state.merge({ | |
loading: false, | |
user: user, | |
ErrorMessage: null, | |
}) | |
export const signupUserFailure = (state, { errorMessage }) => | |
state.merge({ | |
loading: false, | |
user: {}, | |
ErrorMessage: errorMessage, | |
}) | |
export const logoutUserLoading = (state) => | |
state.merge({ | |
loading: true, | |
user: {}, | |
}) | |
export const logoutUserSuccess = (state, { user }) => | |
state.merge({ | |
loading: false, | |
user: user, | |
ErrorMessage: null, | |
}) | |
export const logoutUserFailure = (state, { errorMessage }) => | |
state.merge({ | |
loading: false, | |
user: {}, | |
ErrorMessage: errorMessage, | |
}) | |
/** | |
* @see https://github.com/infinitered/reduxsauce#createreducer | |
*/ | |
export const reducer = createReducer(INITIAL_STATE, { | |
[RepoTypes.LOGIN_USER_LOADING]: loginUserLoading, | |
[RepoTypes.LOGIN_USER_SUCCESS]: loginUserSuccess, | |
[RepoTypes.LOGIN_USER_FAILURE]: loginUserFailure, | |
[RepoTypes.SIGNUP_USER_LOADING]: signupUserLoading, | |
[RepoTypes.SIGNUP_USER_SUCCESS]: signupUserSuccess, | |
[RepoTypes.SIGNUP_USER_FAILURE]: signupUserFailure, | |
[RepoTypes.LOGOUT_USER_LOADING]: logoutUserLoading, | |
[RepoTypes.LOGOUT_USER_SUCCESS]: logoutUserSuccess, | |
[RepoTypes.LOGOUT_USER_FAILURE]: logoutUserFailure, | |
}) | |
// loginUser: null, | |
// loginUserLoading: null, | |
// loginUserSuccess: ['users'], | |
// loginUserFailure: ['errorMessage'], | |
// | |
// signupUser: null, | |
// signupUserLoading: null, | |
// signupUserSuccess: ['users'], | |
// signupUserFailure: ['errorMessage'], | |
// | |
// logoutUser: null, | |
// logoutUserLoading: null, | |
// logoutUserSuccess: ['users'], | |
// logoutUserFailure: ['errorMessage'], | |
// a = %w{ | |
// loginUser | |
// loginUserLoading | |
// loginUserSuccess | |
// loginUserFailure | |
// signupUser | |
// signupUserLoading | |
// signupUserSuccess | |
// signupUserFailure | |
// logoutUser | |
// logoutUserLoading | |
// logoutUserSuccess | |
// logoutUserFailure | |
// } | |
// a.each do |b| | |
// puts "[RepoTypes.#{b.underscore.upcase}]: #{b}," | |
// end | |
// | |
// [ | |
// "LOGIN_USER", | |
// "LOGIN_USER_LOADING", | |
// "LOGIN_USER_SUCCESS", | |
// "LOGIN_USER_FAILURE", | |
// "SIGNUP_USER", | |
// "SIGNUP_USER_LOADING", | |
// "SIGNUP_USER_SUCCESS", | |
// "SIGNUP_USER_FAILURE", | |
// "LOGOUT_USER", | |
// "LOGOUT_USER_LOADING", | |
// "LOGOUT_USER_SUCCESS", | |
// "LOGOUT_USER_FAILURE" | |
// ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment