import { User } from '../types/user.interface' ; import services from '../services' ; import { UserStorage } from '../utils/user-storage' ; import { BASE_NAME } from '../constant' ; type UserState = User | null ; export const user = { state : null as UserState , reducers : { logout ( ) { UserStorage . logout ( ) ; return null ; } } , effects : { async register ( state , payload ) { const user = await services . user . visitor . register ( payload ) ; UserStorage . setToken ( user . token , true ) ; return user ; } , async login ( state , payload ) { const user = await services . user . visitor . login ( payload . phone , payload . password ) ; UserStorage . setToken ( user . token , true ) ; return user ; } , async auth ( state , permissionRequired ? ) { if ( ! UserStorage . getToken ( ) ) { if ( permissionRequired ) { window . location . replace ( '/' ) ; } return state ; } try { const data = await services . user . user . getInfo ( ) ; return data ; } catch ( error ) { if ( permissionRequired ) { if ( error . code === 403 ) { UserStorage . logout ( ) ; } else { window . history . back ( ) ; } } return state ; } } , async updateInfo ( state , payload ) { await services . user . user . update ( payload ) ; return services . user . user . getInfo ( ) ; } , async updateTheme ( state , payload ) { await services . user . user . updateTheme ( payload ) ; return services . user . user . getInfo ( ) ; } , } , } ;