Skip to content

Instantly share code, notes, and snippets.

@karlingen
Created June 12, 2018 11:16
Show Gist options
  • Save karlingen/5530abf0f4b827ab4ddd94fb46be5d6f to your computer and use it in GitHub Desktop.
Save karlingen/5530abf0f4b827ab4ddd94fb46be5d6f to your computer and use it in GitHub Desktop.
GithubRedux.js
import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
userRequest: ['username'],
userSuccess: ['avatar'],
userFailure: null
})
export const GithubTypes = Types
export default Creators
/* ------------- Initial State ------------- */
export const INITIAL_STATE = Immutable({
avatar: null,
fetching: null,
error: null,
username: null
})
/* ------------- Selectors ------------- */
export const GithubSelectors = {
selectAvatar: state => state.github.avatar
}
/* ------------- Reducers ------------- */
// request the avatar for a user
export const request = (state, { username }) =>
state.merge({ fetching: true, username, avatar: null })
// successful avatar lookup
export const success = (state, action) => {
const { avatar } = action
return state.merge({ fetching: false, error: null, avatar })
}
// failed to get the avatar
export const failure = (state) =>
state.merge({ fetching: false, error: true, avatar: null })
/* ------------- Hookup Reducers To Types ------------- */
export const reducer = createReducer(INITIAL_STATE, {
[Types.USER_REQUEST]: request,
[Types.USER_SUCCESS]: success,
[Types.USER_FAILURE]: failure
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment