62 lines
1.7 KiB
TypeScript

import { createReducer } from '@reduxjs/toolkit';
import { updateProfile, getStaffDetails, resetEditProfileState } from './thunk';
import { UserData } from '@interfaces';
export interface EditProfileState {
loading: boolean;
loadingDetails: boolean;
error: string | null;
successMessage: string | null;
staffDetails: UserData | null;
}
const initialState: EditProfileState = {
loading: false,
loadingDetails: false,
error: null,
successMessage: null,
staffDetails: null,
};
export const editProfileReducer = createReducer(initialState, builder => {
builder
.addCase(resetEditProfileState, () => initialState)
// updateProfile cases
.addCase(updateProfile.pending, acc => {
acc.loading = true;
acc.error = null;
acc.successMessage = null;
})
.addCase(updateProfile.fulfilled, acc => {
acc.loading = false;
acc.successMessage = 'Profile updated successfully.';
acc.error = null;
})
.addCase(updateProfile.rejected, (acc, action) => {
acc.loading = false;
acc.error =
(action.payload as string) ??
action.error.message ??
'Failed to update profile';
})
// getStaffDetails cases
.addCase(getStaffDetails.pending, acc => {
acc.loadingDetails = true;
acc.error = null;
})
.addCase(getStaffDetails.fulfilled, (acc, action) => {
acc.loadingDetails = false;
acc.staffDetails = action.payload;
acc.error = null;
})
.addCase(getStaffDetails.rejected, (acc, action) => {
acc.loadingDetails = false;
acc.error =
(action.payload as string) ??
action.error.message ??
'Failed to load staff details';
});
});
export default editProfileReducer;