import { createReducer, createActions } from "reduxsauce"; import Immutable from "seamless-immutable"; import {Reducer} from "redux"; import {GithubReducerTypes} from "./Types"; /* ------------- 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 : Reducer = createReducer(INITIAL_STATE, { [Types.USER_REQUEST]: request, [Types.USER_SUCCESS]: success, [Types.USER_FAILURE]: failure });