import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit'; import { authService } from '../../services/Auth-service'; import { userProfileService } from '../../services/UserProfile-service'; import { Presence } from '../contacts/types'; import { IUser } from '../userProfile/types'; import { IAuthResult, IAuthState, SelfRegisterParams } from './types'; import { RootState } from '../index'; const initialState: IAuthState = { isAuthInProgress: true, responseMsg: '', isAuthError: false, isRegistrationInProgress: false, registrationError: null, registrationStep: 'email', // 'email' | 'verification' | 'profile' | 'complete' }; interface ICredentials { email: string; password: string } export const login = createAsyncThunk( 'auth/login', async (credentials: ICredentials ) => { authService.login(credentials.email, credentials.password); } ); export const resetPassword = createAsyncThunk( 'auth/resetPassword', async (passData: { temporaryCode: string; newPassword: string }) => { authService.resetUserPassword(passData.temporaryCode, passData.newPassword); } ); // Async thunk for self-registration export const registerUser = createAsyncThunk( 'auth/register', async (params: SelfRegisterParams, { rejectWithValue }) => { try { const result = await authService.selfRegisterByEmail(params); if (!result.success) { return rejectWithValue(result.error || 'Registration failed'); } return result; } catch (error) { return rejectWithValue(error instanceof Error ? error.message : 'Registration failed'); } } ); // Define a slice using createSlice const authSlice = createSlice({ name: 'auth', initialState, reducers: { setRegistrationStep: (state, action: PayloadAction) => { state.registrationStep = action.payload; }, resetRegistrationState: (state) => { state.isRegistrationInProgress = false; state.registrationError = null; state.registrationStep = 'email'; }, setAuthState: (state, action: PayloadAction<{ inProgress: boolean; error?: boolean }>) => { state.isAuthInProgress = action.payload.inProgress; state.isAuthError = action.payload.error || false; }, setPresence(state, action: PayloadAction) { userProfileService.setPresence(action.payload); }, sendResetPasswordEmail(state, action) { authService.sendResetPasswordEmail(action.payload); }, silentLogin(state) { // state.isAuthInProgress = true; authService.silentLogin(); }, signOut(state) { authService.signOut(); }, SignOutResult(state) { // state.isAuthInProgress = false; state.user = undefined; }, getLoginResult(state, action: PayloadAction) { state.isAuthInProgress = false; state.responseMsg = action.payload.errorCode; state.user = action.payload.user; }, updateConnectedUserPresence(state, action: PayloadAction<{ presence: Presence }>) { const newPresence = action.payload.presence; if (state.user) { // Update the user object with the new presence state.user = { ...state.user, contact: { ...state.user.contact, presence: newPresence, }, }; } }, getGetConnectedUser(state) { userProfileService.getConnectedUser(); }, getGetConnectedUserResult(state, action: PayloadAction<{ user: IUser }>) { state.user = action.payload.user; } // Add other synchronous actions here }, extraReducers: (builder) => { builder .addCase(login.fulfilled, (state) => { state.isAuthInProgress = false; }) .addCase(login.rejected, (state) => { state.isAuthInProgress = false; state.isAuthError = true; }) .addCase(registerUser.pending, (state) => { state.isRegistrationInProgress = true; state.registrationError = null; }) .addCase(registerUser.fulfilled, (state) => { state.isRegistrationInProgress = false; state.registrationStep = 'complete'; }) .addCase(registerUser.rejected, (state, action) => { state.isRegistrationInProgress = false; state.registrationError = action.payload as string || 'Registration failed'; }); }, }); export const connectedUser = (state: RootState) => state.authReducer.user; export const authResponseMsg = (state: RootState) => state.authReducer.responseMsg; export const isAuthInProgress = (state: RootState) => state.authReducer.isAuthInProgress; export const isRegistrationInProgress = (state: RootState) => state.authReducer.isRegistrationInProgress; export const registrationError = (state: RootState) => state.authReducer.registrationError; export const registrationStep = (state: RootState) => state.authReducer.registrationStep; // Export action creators and reducer export const { setAuthState, setPresence, sendResetPasswordEmail, silentLogin, signOut, getLoginResult, SignOutResult, updateConnectedUserPresence, getGetConnectedUser, getGetConnectedUserResult, setRegistrationStep, resetRegistrationState } = authSlice.actions; export default authSlice.reducer; export const authActions = authSlice.actions;