import { RootState } from '../store'; import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'; import { AuthState, LoginState, ProfileRequest } from './models/auth.model'; import { client } from 'utils/api'; import appConfig from 'config'; export const initialState: AuthState = { loading: 'idle', error: null, loggedIn: false, accessToken: null, address: null, balance: 0, noBoxes: 0, username: null, bio: null, email: null, playerId: null, avatar: null, cover: null, }; const pendingStatus = (state) => { if (state.loading === 'idle') { state.loading = 'pending'; } }; const rejectResult = (state, action) => { if (state.loading === 'pending') { state.loading = 'idle'; state.error = action.error; } }; const clientAccount = client(appConfig.server.accountApiUrl); export const updateProfile = createAsyncThunk('account/profile', async (params: ProfileRequest) => { const res = await clientAccount.put(`/profile/account`, params); return res; }); export const authSlice = createSlice({ name: 'auth', initialState, reducers: { login: (state, { payload: { accessToken, address, balance, noBoxes } }: PayloadAction) => { state.loggedIn = true; state.accessToken = accessToken; state.address = address; state.balance = +balance; state.noBoxes = +noBoxes; }, logout: () => initialState, fetchToken: (state, { payload }: PayloadAction) => { state.accessToken = payload; }, fetchBalance: (state, { payload }: PayloadAction) => { state.balance = payload; }, fetchAuth: (state, { payload }: PayloadAction) => ({ ...state, ...payload, loggedIn: true, }), }, extraReducers: (builder) => { builder .addCase(updateProfile.pending, pendingStatus) .addCase(updateProfile.fulfilled, (state, action) => { if (state.loading === 'pending') { return { ...state, ...action.payload, loading: 'idle', error: null }; } }) .addCase(updateProfile.rejected, rejectResult); }, }); export const { login, logout, fetchToken, fetchAuth, fetchBalance } = authSlice.actions; export const selectAuth = (state: RootState) => state.auth; export default authSlice.reducer;