import _ from 'lodash' ; import { Reducer } from 'redux' ; import { REHYDRATE } from 'redux-persist/lib/constants' ; import { IdToken } from 'libs/Twitch' ; import { createAction , RehydrateAction } from 'utils/redux' ; /**
 * Actions types.
 */ export enum Actions { SET_TOKENS = 'user/SET_TOKENS' , RESET = 'user/RESET' , SET_MODERATOR = 'user/SET_MODERATOR' } /**
 * Initial state.
 */ export const initialState = { isMod : false , tokens : null , username : null , } ; param state param action return const userReducer = ( state = initialState , action ) => { switch ( action . type ) { case REHYDRATE : { if ( ! _ . get ( action . payload , 'user' ) ) { return state ; } return { ... action . payload . user , isMod : false } ; } case Actions . SET_TOKENS : { return { ... state , tokens : action . payload , username : action . payload . id . preferred_username . toLowerCase ( ) , } ; } case Actions . RESET : { return initialState ; } case Actions . SET_MODERATOR : { return { ... state , isMod : action . payload . isMod , } ; } default : { return state ; } } } ; export default userReducer ; param access param id return export const setTokens = ( access , id ) => createAction ( Actions . SET_TOKENS , { access , id , } ) ; return export const resetUser = ( ) => createAction ( Actions . RESET ) ; param isMod return export const setModerator = ( isMod ) => createAction ( Actions . SET_MODERATOR , { isMod , } ) ; /**
 * User actions.
 */ export type UserActions = RehydrateAction | ReturnType < typeof setTokens > | ReturnType < typeof resetUser > | ReturnType < typeof setModerator > ; /**
 * User state.
 */ export type UserState = { /**
     * Tokens.
     */ tokens : { access : string ; id : IdToken ; } | null ; /**
     * Username.
     */ username : string | null ; /**
     * `true` when the user is a moderator.
     */ isMod : boolean ; } ;