Login:Src:Components: replace then with async/await

This commit is contained in:
Darya Umrikhina 2024-08-15 01:28:08 +04:00
parent bc77043419
commit 5169987153
6 changed files with 106 additions and 81 deletions

View File

@ -48,13 +48,15 @@ const AuthHandler = () => {
const { email = "", key = "" } = linkData;
useEffect(() => {
loginWithConfirmKey({
ConfirmData: {
Email: email,
Key: key,
},
})
?.then((res) => {
async function loginWithKey() {
try {
const res = await loginWithConfirmKey({
ConfirmData: {
Email: email,
Key: key,
},
});
//console.log("Login with confirm key success", res);
frameCallEvent({ event: "onAuthSuccess" });
@ -89,8 +91,7 @@ const AuthHandler = () => {
if (typeof res === "string") window.location.replace(res);
else window.location.replace("/");
})
.catch((error) => {
} catch (error) {
const knownError = error as TError;
let errorMessage: string;
@ -105,8 +106,11 @@ const AuthHandler = () => {
}
frameCallEvent({ event: "onAppError", data: error });
toastr.error(error);
});
toastr.error(errorMessage);
}
}
loginWithKey();
});
return <AppLoader />;

View File

@ -151,16 +151,28 @@ const CreateUserForm = (props: CreateUserFormProps) => {
culture: currentCultureName,
};
return signupOAuth(signupAccount)
?.then(() => {
const url = roomData.roomId
? `/rooms/shared/${roomData.roomId}/filter?folder=${roomData.roomId}/`
: defaultPage;
window.location.replace(url);
})
.catch((e) => {
toastr.error(e);
});
try {
await signupOAuth(signupAccount);
const url = roomData.roomId
? `/rooms/shared/${roomData.roomId}/filter?folder=${roomData.roomId}/`
: defaultPage;
window.location.replace(url);
} catch (error) {
const knownError = error as TError;
let errorMessage: string;
if (typeof knownError === "object") {
errorMessage =
knownError?.response?.data?.error?.message ||
knownError?.statusText ||
knownError?.message ||
"";
} else {
errorMessage = knownError;
}
toastr.error(errorMessage);
}
},
[
currentCultureName,
@ -241,7 +253,7 @@ const CreateUserForm = (props: CreateUserFormProps) => {
setIsLoading(false);
};
const onSubmit = () => {
const onSubmit = async () => {
const type = parseInt(linkData?.emplType ?? "");
setIsLoading(true);
@ -299,7 +311,9 @@ const CreateUserForm = (props: CreateUserFormProps) => {
const headerKey = linkData?.confirmHeader ?? "";
createConfirmUser(confirmUser, headerKey).catch((error) => {
try {
await createConfirmUser(confirmUser, headerKey);
} catch (error) {
const knownError = error as TError;
let errorMessage: string;
@ -318,7 +332,7 @@ const CreateUserForm = (props: CreateUserFormProps) => {
setEmailErrorText(errorMessage);
setEmailValid(false);
setIsLoading(false);
});
}
};
const createConfirmUser = async (
@ -385,7 +399,7 @@ const CreateUserForm = (props: CreateUserFormProps) => {
};
const onSocialButtonClick = useCallback(
(e: MouseEvent<Element>) => {
async (e: MouseEvent<Element>) => {
const target = e.target as HTMLElement;
let targetElement = target;
@ -408,18 +422,18 @@ const CreateUserForm = (props: CreateUserFormProps) => {
"width=800,height=500,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=no",
);
getOAuthToken(tokenGetterWin).then((code) => {
const token = window.btoa(
JSON.stringify({
auth: providerName,
mode: "popup",
callback: "authCallback",
}),
);
const code = await getOAuthToken(tokenGetterWin);
if (tokenGetterWin && typeof tokenGetterWin === "object")
tokenGetterWin.location.href = getLoginLink(token, code);
});
const token = window.btoa(
JSON.stringify({
auth: providerName,
mode: "popup",
callback: "authCallback",
}),
);
if (tokenGetterWin && typeof tokenGetterWin === "object")
tokenGetterWin.location.href = getLoginLink(token, code);
} catch (err) {
console.log(err);
}

View File

@ -46,11 +46,15 @@ const EmailActivationHandler = () => {
const { email, uid = "", key = "" } = linkData;
useEffect(() => {
updateActivationStatus(EmployeeActivationStatus.Activated, uid, key)
.then(() => {
window.location.replace(`/login?confirmedEmail=${email}`);
})
.catch((error) => {
async function changeActivationStatus() {
await updateActivationStatus(
EmployeeActivationStatus.Activated,
uid,
key,
);
window.location.replace(`/login?confirmedEmail=${email}`);
try {
} catch (error) {
const knownError = error as TError;
let errorMessage: string;
@ -63,9 +67,11 @@ const EmailActivationHandler = () => {
} else {
errorMessage = knownError;
}
setError(errorMessage);
});
}
}
changeActivationStatus();
}, [email, key, uid, router]);
if (error) {

View File

@ -42,11 +42,11 @@ const EmailChangeHandler = () => {
const { email = "", uid = "", key = "" } = linkData;
useEffect(() => {
changeEmail(uid, email, key)
.then(() => {
async function emailChange() {
try {
await changeEmail(uid, email, key);
window.location.replace(`/profile?email_change=success`);
})
.catch((error) => {
} catch (error) {
const knownError = error as TError;
let errorMessage: string;
@ -59,9 +59,11 @@ const EmailChangeHandler = () => {
} else {
errorMessage = knownError;
}
setError(errorMessage);
});
}
}
emailChange();
}, [email, uid, key]);
if (error) {

View File

@ -58,32 +58,32 @@ const ProfileRemoveForm = ({
const [isProfileDeleted, setIsProfileDeleted] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const onDeleteProfile = () => {
const onDeleteProfile = async () => {
setIsLoading(true);
deleteSelf(linkData.confirmHeader)
?.then(() => {
setIsLoading(false);
setIsProfileDeleted(true);
})
.catch((error) => {
const knownError = error as TError;
let errorMessage: string;
try {
await deleteSelf(linkData.confirmHeader);
if (typeof knownError === "object") {
errorMessage =
knownError?.response?.data?.error?.message ||
knownError?.statusText ||
knownError?.message ||
"";
} else {
errorMessage = knownError;
}
console.error(errorMessage);
setIsLoading(false);
setIsProfileDeleted(true);
} catch (error) {
const knownError = error as TError;
let errorMessage: string;
setIsLoading(false);
toastr.error(error);
});
if (typeof knownError === "object") {
errorMessage =
knownError?.response?.data?.error?.message ||
knownError?.statusText ||
knownError?.message ||
"";
} else {
errorMessage = knownError;
}
console.error(errorMessage);
setIsLoading(false);
toastr.error(errorMessage);
}
};
if (isProfileDeleted) {

View File

@ -231,7 +231,7 @@ function WizardForm(props: WizardFormProps) {
});
};
const onLicenseFileHandler = (file: File | File[]) => {
const onLicenseFileHandler = async (file: File | File[]) => {
if (licenseUpload) setLicenseUpload(null);
setHasErrorLicense(false);
setInvalidLicense(false);
@ -241,15 +241,14 @@ function WizardForm(props: WizardFormProps) {
if (!wizardToken) return;
setLicense(wizardToken, fd)
.then((res) => {
setLicenseUpload(res);
})
.catch((e) => {
console.error(e);
setHasErrorLicense(true);
setInvalidLicense(true);
});
try {
const res = await setLicense(wizardToken, fd);
setLicenseUpload(res);
} catch (e) {
console.error(e);
setHasErrorLicense(true);
setInvalidLicense(true);
}
};
const onAgreeTermsChange = () => {