import React, { useState } from "react"; import { useHistory } from "react-router"; import { useTranslation } from "react-i18next"; import { Form, Row, Col, Button, Modal } from "react-bootstrap"; import { useUser, userService } from "@/services/user"; import OperationDialog from "../common/OperationDialog"; interface ChangePasswordDialogProps { open: boolean; close: () => void; } const ChangePasswordDialog: React.FC = (props) => { const history = useHistory(); const [redirect, setRedirect] = useState(false); return ( { const result: Record = {}; if (oldPassword === "") { result[0] = "settings.dialogChangePassword.errorEmptyOldPassword"; } if (newPassword === "") { result[1] = "settings.dialogChangePassword.errorEmptyNewPassword"; } if (retypedNewPassword !== newPassword) { result[2] = "settings.dialogChangePassword.errorRetypeNotMatch"; } return result; }} onProcess={async ([oldPassword, newPassword]) => { await userService.changePassword(oldPassword, newPassword).toPromise(); await userService.logout(); setRedirect(true); }} close={() => { props.close(); if (redirect) { history.push("/login"); } }} /> ); }; const ConfirmLogoutDialog: React.FC<{ toggle: () => void; onConfirm: () => void; }> = ({ toggle, onConfirm }) => { const { t } = useTranslation(); return ( {t("settings.dialogConfirmLogout.title")} {t("settings.dialogConfirmLogout.prompt")} ); }; const SettingsPage: React.FC = (_) => { const { i18n, t } = useTranslation(); const user = useUser(); const history = useHistory(); const [dialog, setDialog] = useState( null ); const language = i18n.language.slice(0, 2); return ( <> {user ? (

{t("settings.subheaders.account")}

{ history.push(`/users/${user.username}`); }} > {t("settings.gotoSelf")}
setDialog("changepassword")} > {t("settings.changePassword")}
{ setDialog("logout"); }} > {t("settings.logout")}
) : null}

{t("settings.subheaders.customization")}

{t("settings.languagePrimary")}
{t("settings.languageSecondary")} { void i18n.changeLanguage(e.target.value); }} >
{(() => { switch (dialog) { case "changepassword": return ( { setDialog(null); }} /> ); case "logout": return ( setDialog(null)} onConfirm={() => { void userService.logout().then(() => { history.push("/"); }); }} /> ); default: return null; } })()} ); }; export default SettingsPage;