refactor: streamline async logic and notification handling across various settings and integration components

This commit is contained in:
Victor Santos
2025-11-03 15:11:10 -03:00
parent 3aeee8d552
commit c005f8a0fe
33 changed files with 709 additions and 1061 deletions
@@ -83,23 +83,14 @@ export const ChangeEmailSection = () => {
return;
}
try {
await requestEmailChangeOTP({ newEmail });
setPendingEmail(newEmail);
setIsOTPModalOpen(true);
await requestEmailChangeOTP({ newEmail });
setPendingEmail(newEmail);
setIsOTPModalOpen(true);
createNotification({
text: "Verification code sent to your new email address. Check your inbox!",
type: "success"
});
} catch (err: any) {
console.error(err);
const errorMessage = err?.response?.data?.message || "Failed to send verification code";
createNotification({
text: errorMessage,
type: "error"
});
}
createNotification({
text: "Verification code sent to your new email address. Check your inbox!",
type: "success"
});
};
const [typedOTP, setTypedOTP] = useState("");
@@ -135,8 +126,6 @@ export const ChangeEmailSection = () => {
navigate({ to: "/login" });
}, 2000);
} catch (err: any) {
console.error(err);
const errorMessage = err?.response?.data?.message || "Invalid verification code";
if (errorMessage.includes("Invalid verification code")) {
// Reset to email step so user must request new OTP
@@ -149,11 +138,6 @@ export const ChangeEmailSection = () => {
text: "Invalid verification code. Please request a new one.",
type: "error"
});
} else {
createNotification({
text: errorMessage,
type: "error"
});
}
}
};
@@ -97,21 +97,13 @@ export const ChangePasswordSection = () => {
};
const onSetupPassword = async () => {
try {
await sendSetupPasswordEmail.mutateAsync();
await sendSetupPasswordEmail.mutateAsync();
createNotification({
title: "Password setup verification email sent",
text: "Check your email to confirm password setup",
type: "info"
});
} catch (err) {
console.error(err);
createNotification({
text: "Failed to send password setup email",
type: "error"
});
}
createNotification({
title: "Password setup verification email sent",
text: "Check your email to confirm password setup",
type: "info"
});
};
return (
@@ -15,23 +15,15 @@ export const DeleteAccountSection = () => {
const { mutateAsync: deleteUserMutateAsync, isPending } = useDeleteMe();
const handleDeleteAccountSubmit = async () => {
try {
await deleteUserMutateAsync();
await deleteUserMutateAsync();
createNotification({
text: "Successfully deleted account",
type: "success"
});
createNotification({
text: "Successfully deleted account",
type: "success"
});
navigate({ to: "/login" });
handlePopUpClose("deleteAccount");
} catch (err) {
console.error(err);
createNotification({
text: "Failed to delete account",
type: "error"
});
}
navigate({ to: "/login" });
handlePopUpClose("deleteAccount");
};
return (
@@ -84,49 +84,27 @@ export const MFASection = () => {
}, [totpRegistration, showMobileAuthSetup]);
const handleTotpDeletion = async () => {
try {
await deleteTotpConfiguration();
await deleteTotpConfiguration();
await mutateAsync({
selectedMfaMethod: MfaMethod.EMAIL
});
await mutateAsync({
selectedMfaMethod: MfaMethod.EMAIL
});
createNotification({
text: "Successfully deleted mobile authenticator and switched to email authentication",
type: "success"
});
createNotification({
text: "Successfully deleted mobile authenticator and switched to email authentication",
type: "success"
});
handlePopUpClose("deleteTotpConfig");
} catch (err) {
console.error(err);
const error = err as any;
const text = error?.response?.data?.message ?? "Failed to delete mobile authenticator";
createNotification({
text,
type: "error"
});
}
handlePopUpClose("deleteTotpConfig");
};
const handleGenerateMoreRecoveryCodes = async () => {
try {
await createTotpRecoveryCodes();
await createTotpRecoveryCodes();
createNotification({
text: "Successfully generated new recovery codes",
type: "success"
});
} catch (err) {
console.error(err);
const error = err as any;
const text = error?.response?.data?.message ?? "Failed to generate new recovery codes";
createNotification({
text,
type: "error"
});
}
createNotification({
text: "Successfully generated new recovery codes",
type: "success"
});
};
const handleFormDataChange = async (field: string, value: any) => {
@@ -200,10 +178,6 @@ export const MFASection = () => {
await queryClient.invalidateQueries({ queryKey: userKeys.totpConfiguration });
} catch {
createNotification({
text: "Failed to verify TOTP code. Please try again.",
type: "error"
});
setIsLoading(false);
return;
}
@@ -249,12 +223,6 @@ export const MFASection = () => {
setShowMobileAuthSetup(false);
setTotpCode("");
setShouldShowRecoveryCodes.off();
} catch (err) {
createNotification({
text: "Something went wrong while updating two-factor authentication settings.",
type: "error"
});
console.error(err);
} finally {
setIsLoading(false);
}
@@ -40,19 +40,11 @@ export const SessionsTable = () => {
] as const);
const handleSignOut = async (sessionId: string) => {
try {
await revokeMySessionById(sessionId);
createNotification({
text: "Session revoked successfully",
type: "success"
});
} catch (error) {
console.error(error);
createNotification({
text: "Failed to revoke session",
type: "error"
});
}
await revokeMySessionById(sessionId);
createNotification({
text: "Session revoked successfully",
type: "success"
});
handlePopUpClose("deleteSession");
};
@@ -27,22 +27,14 @@ export const UserNameSection = (): JSX.Element => {
}, [user]);
const onFormSubmit = async ({ name }: FormData) => {
try {
if (!user?.id) return;
if (name === "") return;
if (!user?.id) return;
if (name === "") return;
await mutateAsync({ newName: name });
createNotification({
text: "Successfully renamed user",
type: "success"
});
} catch (error) {
console.error(error);
createNotification({
text: "Failed to rename user",
type: "error"
});
}
await mutateAsync({ newName: name });
createNotification({
text: "Successfully renamed user",
type: "success"
});
};
return (