import { Action , AnyAction , Reducer } from "redux" ; import * as SI from "seamless-immutable" ; import { createAction , PayloadAction } from "typesafe-actions" ; import { mapReducers , ReducerMap } from "../../Lib/ReduxHelpers" ; interface RequestParams { username : string ; } interface SuccessParams { avatar : string ; } const actions = { userRequest : createAction ( "githubUserRequest" , ( params ) => ( { type : "githubUserRequest" , payload : params } ) ) , userSuccess : createAction ( "githubUserSuccess" , ( params ) => ( { type : "githubUserSuccess" , payload : params } ) ) , userFailure : createAction ( "githubUserFailure" ) , } ; export const GithubActions = actions ; interface GithubState { avatar ? : string | null ; fetching ? : boolean | null ; error ? : boolean | null ; username ? : string | null ; } export type GithubAction = PayloadAction < string , GithubState > ; export type ImmutableGithubState = SI . ImmutableObject < GithubState > ; export const INITIAL_STATE = SI . from ( { avatar : null , fetching : null , error : null , username : null , } ) ; export const userRequest = ( state , { payload } ) => payload ? state . merge ( { fetching : true , username : payload . username , avatar : null } ) : state ; export const userSuccess = ( state , { payload } ) => payload ? state . merge ( { fetching : false , error : null , avatar : payload . avatar } ) : state ; export const userFailure = ( state ) => state . merge ( { fetching : false , error : true , avatar : null } ) ; const reducerMap = { userRequest , userSuccess , userFailure , } ; export const GithubReducer = mapReducers ( INITIAL_STATE , reducerMap , actions ) ; export default GithubReducer ;