40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { Epic } from "redux-observable";
|
|
import { RootState } from "../types";
|
|
import { changePassword, logout } from "../auth/authSlice";
|
|
import toast from "react-hot-toast";
|
|
import { filter, switchMap } from "rxjs/operators";
|
|
import { of } from "rxjs";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
export const changePasswordEpic: Epic<any, any, RootState> = action$ =>
|
|
action$.pipe(
|
|
// Listen for any action related to changePassword (pending, fulfilled, rejected)
|
|
filter(action => action.type.startsWith(changePassword.typePrefix)),
|
|
switchMap(action => {
|
|
if (changePassword.fulfilled.match(action)) {
|
|
toast.success(
|
|
action.payload?.message || "Password changed successfully"
|
|
);
|
|
|
|
// Logout the user after successful password change
|
|
return of(logout());
|
|
}
|
|
|
|
if (changePassword.rejected.match(action)) {
|
|
const errorMessage =
|
|
(action.payload as string) ||
|
|
action.error?.message ||
|
|
"Password change failed";
|
|
toast.error(errorMessage);
|
|
return of({ type: "CHANGE_PASSWORD_ERROR_HANDLED" });
|
|
}
|
|
|
|
// If it's pending or something else, just ignore it.
|
|
return of({ type: "CHANGE_PASSWORD_NOOP" });
|
|
})
|
|
);
|
|
|
|
const userEpics = [changePasswordEpic];
|
|
|
|
export default userEpics;
|