Account.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import Modal from "../ui/Modal";
  4. import { sendAuthToken } from "../utils/api";
  5. import useAuth from "../hooks/useAuth";
  6. import { toast } from "react-toastify";
  7. import Waiter from "../ui/Waiter";
  8. const Account = ({ disabled, ...props }) => {
  9. const { t } = useTranslation();
  10. const { isAuthenticated, logout } = useAuth();
  11. const [email, setEmail] = React.useState("");
  12. const [emailSent, setEmailSent] = React.useState(false);
  13. const [showLogin, setShowLogin] = React.useState(false);
  14. const [loginInProgress, setLoginInProgress] = React.useState(false);
  15. const handleChange = (e) => {
  16. setEmail(e.target.value);
  17. };
  18. const handleSubmit = async () => {
  19. try {
  20. setLoginInProgress(true);
  21. await sendAuthToken(email);
  22. setEmailSent(true);
  23. setLoginInProgress(false);
  24. } catch (e) {
  25. setLoginInProgress(false);
  26. console.log(e);
  27. toast.error(t("Error while logging, verify your email address"));
  28. }
  29. };
  30. const showAlert = () => {
  31. alert(t("Cookie are disabled or not yet accepted, can't connect"));
  32. };
  33. React.useEffect(() => {
  34. if (!showLogin) {
  35. setEmail("");
  36. setEmailSent(false);
  37. }
  38. }, [showLogin]);
  39. if (disabled) {
  40. return (
  41. <div {...props}>
  42. <button
  43. onClick={showAlert}
  44. title={t("Cookie are disabled or not yet accepted, can't connect")}
  45. style={{ opacity: 0.4, cursor: "not-allowed" }}
  46. >
  47. {t("Login")}
  48. </button>
  49. </div>
  50. );
  51. }
  52. const handleKeyDown = (e) => {
  53. if (e.key === "Enter") {
  54. handleSubmit();
  55. }
  56. };
  57. return (
  58. <>
  59. <div {...props}>
  60. {isAuthenticated ? (
  61. <button onClick={logout}>{t("Logout")}</button>
  62. ) : (
  63. <button onClick={() => setShowLogin(true)}>{t("Login")}</button>
  64. )}
  65. </div>
  66. {loginInProgress && <Waiter message={t("In progress...")} />}
  67. <Modal show={showLogin} setShow={setShowLogin} title={t("Login")}>
  68. {!emailSent && (
  69. <>
  70. <input
  71. value={email}
  72. onChange={handleChange}
  73. placeholder={t("Enter your email here")}
  74. onKeyDown={handleKeyDown}
  75. />
  76. <div
  77. style={{
  78. display: "flex",
  79. justifyContent: "center",
  80. marginTop: "2em",
  81. }}
  82. >
  83. <button onClick={handleSubmit} className="button primary">
  84. {t("Ask authentication link")}
  85. </button>
  86. </div>
  87. </>
  88. )}
  89. {emailSent && (
  90. <>
  91. <p>
  92. {t("Mail sent, check your inbox and click the link to login.")}
  93. </p>
  94. <div
  95. style={{
  96. display: "flex",
  97. justifyContent: "center",
  98. marginTop: "2em",
  99. }}
  100. >
  101. <button
  102. className="button primary"
  103. onClick={() => setShowLogin(false)}
  104. >
  105. Ok
  106. </button>
  107. </div>
  108. </>
  109. )}
  110. </Modal>
  111. </>
  112. );
  113. };
  114. export default Account;