import { AUTH_USER_ACTIONS } from '~/redux/auth/constants'; import * as ActionCreators from '~/redux/auth/actions'; import { IAuthState } from '~/redux/auth/types'; interface ActionHandler { (state: IAuthState, payload: T extends (...args: any[]) => infer R ? R : any): IAuthState; } const setLoginError: ActionHandler = ( state, { error } ) => ({ ...state, login: { ...state.login, error, }, }); const setUser: ActionHandler = (state, { profile }) => ({ ...state, user: { ...state.user, ...profile, }, }); const setState: ActionHandler = (state, { payload }) => ({ ...state, ...payload, }); const setToken: ActionHandler = (state, { token }) => ({ ...state, token, }); const setProfile: ActionHandler = (state, { profile }) => ({ ...state, profile: { ...state.profile, ...profile, }, }); const setUpdates: ActionHandler = (state, { updates }) => ({ ...state, updates: { ...state.updates, ...updates, }, }); const setLastSeenMessages: ActionHandler = ( state, { last_seen_messages } ) => ({ ...state, user: { ...state.user, last_seen_messages, }, }); const setRestore: ActionHandler = (state, { restore }) => ({ ...state, restore: { ...state.restore, ...restore, }, }); const setSocials: ActionHandler = (state, { socials }) => ({ ...state, profile: { ...state.profile, socials: { ...state.profile.socials, ...socials, }, }, }); const setRegisterSocial: ActionHandler = ( state, { register_social } ) => ({ ...state, register_social: { ...state.register_social, ...register_social, }, }); const setRegisterSocialErrors: ActionHandler = ( state, { errors } ) => ({ ...state, register_social: { ...state.register_social, errors: { ...state.register_social.errors, ...errors, }, }, }); export const AUTH_USER_HANDLERS = { [AUTH_USER_ACTIONS.SET_LOGIN_ERROR]: setLoginError, [AUTH_USER_ACTIONS.SET_USER]: setUser, [AUTH_USER_ACTIONS.SET_STATE]: setState, [AUTH_USER_ACTIONS.SET_TOKEN]: setToken, [AUTH_USER_ACTIONS.SET_PROFILE]: setProfile, [AUTH_USER_ACTIONS.SET_UPDATES]: setUpdates, [AUTH_USER_ACTIONS.SET_LAST_SEEN_MESSAGES]: setLastSeenMessages, [AUTH_USER_ACTIONS.SET_RESTORE]: setRestore, [AUTH_USER_ACTIONS.SET_SOCIALS]: setSocials, [AUTH_USER_ACTIONS.SET_REGISTER_SOCIAL]: setRegisterSocial, [AUTH_USER_ACTIONS.SET_REGISTER_SOCIAL_ERRORS]: setRegisterSocialErrors, };