mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-17 16:44:49 -04:00
use async and await (#62)
Use async and await instead of promise chains
This commit is contained in:
parent
11f01094b4
commit
a5af15b278
7 changed files with 92 additions and 80 deletions
|
@ -13,16 +13,19 @@ export const ApplicationLoader: React.FC<ApplicationLoaderProps> = ({children, i
|
|||
|
||||
useEffect(() => {
|
||||
setDoneTasks(0);
|
||||
initTasks.map(task =>
|
||||
task.then(() =>
|
||||
setDoneTasks(prevDoneTasks => {
|
||||
return prevDoneTasks + 1;
|
||||
}))
|
||||
.catch((reason) => {
|
||||
initTasks.forEach(task => {
|
||||
(async () => {
|
||||
try {
|
||||
await task;
|
||||
setDoneTasks(prevDoneTasks => {
|
||||
return prevDoneTasks + 1;
|
||||
})
|
||||
} catch (reason) {
|
||||
setFailed(true);
|
||||
console.error(reason);
|
||||
})
|
||||
)
|
||||
}
|
||||
})();
|
||||
})
|
||||
}, [initTasks]);
|
||||
|
||||
return (<Fragment>{
|
||||
|
|
|
@ -4,29 +4,34 @@ import React, {Fragment, useState} from "react";
|
|||
import {postEmailLogin} from "../../../../../api/user";
|
||||
import {getAndSetUser} from "../../../../../utils/apiUtils";
|
||||
|
||||
const ViaEMail: React.FC = () => {
|
||||
export const ViaEMail: React.FC = () => {
|
||||
const {t} = useTranslation();
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState(false);
|
||||
const login = (event: any) => {
|
||||
postEmailLogin(email, password)
|
||||
.then(loginJson => {
|
||||
console.log(loginJson)
|
||||
getAndSetUser();
|
||||
}).catch(_reason => {
|
||||
|
||||
const doAsyncLogin = () => {
|
||||
(async () => {
|
||||
try {
|
||||
await postEmailLogin(email, password);
|
||||
await getAndSetUser();
|
||||
} catch {
|
||||
setError(true);
|
||||
}
|
||||
)
|
||||
event.preventDefault();
|
||||
})();
|
||||
}
|
||||
|
||||
const onFormSubmit = (event: any) => {
|
||||
doAsyncLogin();
|
||||
event.preventDefault();
|
||||
};
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
<h5 className="center">
|
||||
<Trans i18nKey="signInVia" values={{service: "E-Mail"}}/>
|
||||
</h5>
|
||||
<Form onSubmit={login}>
|
||||
<Form onSubmit={onFormSubmit}>
|
||||
<Form.Group controlId="email">
|
||||
<Form.Control
|
||||
isInvalid={error}
|
||||
|
@ -60,6 +65,4 @@ const ViaEMail: React.FC = () => {
|
|||
</Form>
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
||||
export {ViaEMail}
|
||||
}
|
|
@ -9,29 +9,35 @@ import {ApplicationState} from "../../../../../redux";
|
|||
const ViaLdap: React.FC = () => {
|
||||
const {t} = useTranslation();
|
||||
const ldapCustomName = useSelector((state: ApplicationState) => state.backendConfig.customAuthNames.ldap);
|
||||
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState(false);
|
||||
const login = (event: any) => {
|
||||
postLdapLogin(username, password)
|
||||
.then(loginJson => {
|
||||
console.log(loginJson)
|
||||
getAndSetUser();
|
||||
}).catch(_reason => {
|
||||
setError(true);
|
||||
}
|
||||
)
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
const name = ldapCustomName ? `${ldapCustomName} (LDAP)` : "LDAP";
|
||||
|
||||
const doAsyncLogin = () => {
|
||||
(async () => {
|
||||
try {
|
||||
await postLdapLogin(username, password);
|
||||
await getAndSetUser();
|
||||
} catch {
|
||||
setError(true);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
const onFormSubmit = (event: any) => {
|
||||
doAsyncLogin();
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
<h5 className="center">
|
||||
<Trans i18nKey="signInVia" values={{service: name}}/>
|
||||
</h5>
|
||||
<Form onSubmit={login}>
|
||||
<Form onSubmit={onFormSubmit}>
|
||||
<Form.Group controlId="username">
|
||||
<Form.Control
|
||||
isInvalid={error}
|
||||
|
|
|
@ -8,15 +8,20 @@ const ViaOpenId: React.FC = () => {
|
|||
useTranslation();
|
||||
const [openId, setOpenId] = useState("");
|
||||
const [error, setError] = useState(false);
|
||||
const login = (event: any) => {
|
||||
postOpenIdLogin(openId)
|
||||
.then(loginJson => {
|
||||
console.log(loginJson)
|
||||
getAndSetUser();
|
||||
}).catch(_reason => {
|
||||
|
||||
const doAsyncLogin = () => {
|
||||
(async () => {
|
||||
try {
|
||||
await postOpenIdLogin(openId);
|
||||
await getAndSetUser();
|
||||
} catch {
|
||||
setError(true);
|
||||
}
|
||||
)
|
||||
})();
|
||||
}
|
||||
|
||||
const onFormSubmit = (event: any) => {
|
||||
doAsyncLogin();
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
|
@ -25,7 +30,7 @@ const ViaOpenId: React.FC = () => {
|
|||
<h5 className="center">
|
||||
<Trans i18nKey="signInVia" values={{service: "OpenID"}}/>
|
||||
</h5>
|
||||
<Form onSubmit={login}>
|
||||
<Form onSubmit={onFormSubmit}>
|
||||
<Form.Group controlId="openid">
|
||||
<Form.Control
|
||||
isInvalid={error}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue