123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import React from "react";
- import { useTranslation } from "react-i18next";
- import Modal from "../ui/Modal";
- import { sendAuthToken } from "../utils/api";
- import useAuth from "../hooks/useAuth";
- import { toast } from "react-toastify";
- import Waiter from "../ui/Waiter";
- const Account = ({ disabled, ...props }) => {
- const { t } = useTranslation();
- const { isAuthenticated, logout } = useAuth();
- const [email, setEmail] = React.useState("");
- const [emailSent, setEmailSent] = React.useState(false);
- const [showLogin, setShowLogin] = React.useState(false);
- const [loginInProgress, setLoginInProgress] = React.useState(false);
- const handleChange = (e) => {
- setEmail(e.target.value);
- };
- const handleSubmit = async () => {
- try {
- setLoginInProgress(true);
- await sendAuthToken(email);
- setEmailSent(true);
- setLoginInProgress(false);
- } catch (e) {
- setLoginInProgress(false);
- console.log(e);
- toast.error(t("Error while logging, verify your email address"));
- }
- };
- const showAlert = () => {
- alert(t("Cookie are disabled or not yet accepted, can't connect"));
- };
- React.useEffect(() => {
- if (!showLogin) {
- setEmail("");
- setEmailSent(false);
- }
- }, [showLogin]);
- if (disabled) {
- return (
- <div {...props}>
- <button
- onClick={showAlert}
- title={t("Cookie are disabled or not yet accepted, can't connect")}
- style={{ opacity: 0.4, cursor: "not-allowed" }}
- >
- {t("Login")}
- </button>
- </div>
- );
- }
- const handleKeyDown = (e) => {
- if (e.key === "Enter") {
- handleSubmit();
- }
- };
- return (
- <>
- <div {...props}>
- {isAuthenticated ? (
- <button onClick={logout}>{t("Logout")}</button>
- ) : (
- <button onClick={() => setShowLogin(true)}>{t("Login")}</button>
- )}
- </div>
- {loginInProgress && <Waiter message={t("In progress...")} />}
- <Modal show={showLogin} setShow={setShowLogin} title={t("Login")}>
- {!emailSent && (
- <>
- <input
- value={email}
- onChange={handleChange}
- placeholder={t("Enter your email here")}
- onKeyDown={handleKeyDown}
- />
- <div
- style={{
- display: "flex",
- justifyContent: "center",
- marginTop: "2em",
- }}
- >
- <button onClick={handleSubmit} className="button primary">
- {t("Ask authentication link")}
- </button>
- </div>
- </>
- )}
- {emailSent && (
- <>
- <p>
- {t("Mail sent, check your inbox and click the link to login.")}
- </p>
- <div
- style={{
- display: "flex",
- justifyContent: "center",
- marginTop: "2em",
- }}
- >
- <button
- className="button primary"
- onClick={() => setShowLogin(false)}
- >
- Ok
- </button>
- </div>
- </>
- )}
- </Modal>
- </>
- );
- };
- export default Account;
|