aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/pages/setting
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-08-29 01:30:30 +0800
committercrupest <crupest@outlook.com>2023-08-29 01:30:30 +0800
commitb05860b6d2ea17db29a338659def49dc31082346 (patch)
treecbbd9d99af5e6b006f142effeae5222278dad02f /FrontEnd/src/pages/setting
parent502dd817c79018c84b0a958dd4b2e24781e68ae1 (diff)
downloadtimeline-b05860b6d2ea17db29a338659def49dc31082346.tar.gz
timeline-b05860b6d2ea17db29a338659def49dc31082346.tar.bz2
timeline-b05860b6d2ea17db29a338659def49dc31082346.zip
Refactor dialog module.
Diffstat (limited to 'FrontEnd/src/pages/setting')
-rw-r--r--FrontEnd/src/pages/setting/ChangeAvatarDialog.tsx32
-rw-r--r--FrontEnd/src/pages/setting/ChangeNicknameDialog.tsx11
-rw-r--r--FrontEnd/src/pages/setting/ChangePasswordDialog.tsx11
-rw-r--r--FrontEnd/src/pages/setting/index.tsx130
4 files changed, 81 insertions, 103 deletions
diff --git a/FrontEnd/src/pages/setting/ChangeAvatarDialog.tsx b/FrontEnd/src/pages/setting/ChangeAvatarDialog.tsx
index 2fcfef2c..96ae971b 100644
--- a/FrontEnd/src/pages/setting/ChangeAvatarDialog.tsx
+++ b/FrontEnd/src/pages/setting/ChangeAvatarDialog.tsx
@@ -9,23 +9,21 @@ import { getHttpUserClient } from "~src/http/user";
import { ImageCropper, useImageCrop } from "~src/components/ImageCropper";
import BlobImage from "~src/components/BlobImage";
import { ButtonRowV2 } from "~src/components/button";
-import { Dialog, DialogContainer } from "~src/components/dialog";
+import {
+ Dialog,
+ DialogContainer,
+ useDialogController,
+} from "~src/components/dialog";
import "./ChangeAvatarDialog.css";
-interface ChangeAvatarDialogProps {
- open: boolean;
- onClose: () => void;
-}
-
-export default function ChangeAvatarDialog({
- open,
- onClose,
-}: ChangeAvatarDialogProps) {
+export default function ChangeAvatarDialog() {
const c = useC();
const user = useUser();
+ const controller = useDialogController();
+
type State =
| "select"
| "crop"
@@ -49,11 +47,7 @@ export default function ChangeAvatarDialog({
"settings.dialogChangeAvatar.prompt.select",
);
- const close = (): void => {
- if (state !== "uploading") {
- onClose();
- }
- };
+ const close = controller.closeDialog;
const onSelectFile = (e: ChangeEvent<HTMLInputElement>): void => {
const files = e.target.files;
@@ -96,6 +90,7 @@ export default function ChangeAvatarDialog({
}
setState("uploading");
+ controller.setCanSwitchDialog(false);
getHttpUserClient()
.putAvatar(user.username, resultBlob)
.then(
@@ -106,7 +101,10 @@ export default function ChangeAvatarDialog({
setState("error");
setMessage("operationDialog.error");
},
- );
+ )
+ .finally(() => {
+ controller.setCanSwitchDialog(true);
+ });
};
const cancelButton = {
@@ -181,7 +179,7 @@ export default function ChangeAvatarDialog({
};
return (
- <Dialog open={open} onClose={close}>
+ <Dialog>
<DialogContainer
title="settings.dialogChangeAvatar.title"
titleColor="primary"
diff --git a/FrontEnd/src/pages/setting/ChangeNicknameDialog.tsx b/FrontEnd/src/pages/setting/ChangeNicknameDialog.tsx
index 843659ef..bd1eaa51 100644
--- a/FrontEnd/src/pages/setting/ChangeNicknameDialog.tsx
+++ b/FrontEnd/src/pages/setting/ChangeNicknameDialog.tsx
@@ -3,19 +3,11 @@ import { useUserLoggedIn } from "~src/services/user";
import OperationDialog from "~src/components/dialog/OperationDialog";
-export interface ChangeNicknameDialogProps {
- open: boolean;
- onClose: () => void;
-}
-
-export default function ChangeNicknameDialog(props: ChangeNicknameDialogProps) {
- const { open, onClose } = props;
-
+export default function ChangeNicknameDialog() {
const user = useUserLoggedIn();
return (
<OperationDialog
- open={open}
title="settings.dialogChangeNickname.title"
inputs={[
{
@@ -29,7 +21,6 @@ export default function ChangeNicknameDialog(props: ChangeNicknameDialogProps) {
nickname: newNickname,
});
}}
- onClose={onClose}
/>
);
}
diff --git a/FrontEnd/src/pages/setting/ChangePasswordDialog.tsx b/FrontEnd/src/pages/setting/ChangePasswordDialog.tsx
index 946b9fbe..c3111ac8 100644
--- a/FrontEnd/src/pages/setting/ChangePasswordDialog.tsx
+++ b/FrontEnd/src/pages/setting/ChangePasswordDialog.tsx
@@ -5,22 +5,13 @@ import { userService } from "~src/services/user";
import { OperationDialog } from "~src/components/dialog";
-interface ChangePasswordDialogProps {
- open: boolean;
- onClose: () => void;
-}
-
-export function ChangePasswordDialog(props: ChangePasswordDialogProps) {
- const { open, onClose } = props;
-
+export function ChangePasswordDialog() {
const navigate = useNavigate();
const [redirect, setRedirect] = useState<boolean>(false);
return (
<OperationDialog
- open={open}
- onClose={onClose}
title="settings.dialogChangePassword.title"
color="danger"
inputPrompt="settings.dialogChangePassword.prompt"
diff --git a/FrontEnd/src/pages/setting/index.tsx b/FrontEnd/src/pages/setting/index.tsx
index 918a77b5..4d2c28c7 100644
--- a/FrontEnd/src/pages/setting/index.tsx
+++ b/FrontEnd/src/pages/setting/index.tsx
@@ -14,7 +14,11 @@ import { pushAlert } from "~src/services/alert";
import { useC, Text } from "~src/common";
-import { useDialog, ConfirmDialog } from "~src/components/dialog";
+import {
+ useDialog,
+ DialogProvider,
+ ConfirmDialog,
+} from "~src/components/dialog";
import Card from "~src/components/Card";
import Spinner from "~src/components/Spinner";
import Page from "~src/components/Page";
@@ -140,7 +144,22 @@ function RegisterCodeSettingItem() {
// undefined: loading
const [registerCode, setRegisterCode] = useState<undefined | null | string>();
- const [dialogOpen, setDialogOpen] = useState(false);
+ const { controller, createDialogSwitch } = useDialog({
+ confirm: (
+ <ConfirmDialog
+ title="settings.renewRegisterCode"
+ body="settings.renewRegisterCodeDesc"
+ onConfirm={() => {
+ if (user == null) throw new Error();
+ void getHttpUserClient()
+ .renewRegisterCode(user.username)
+ .then(() => {
+ setRegisterCode(undefined);
+ });
+ }}
+ />
+ ),
+ });
useEffect(() => {
setRegisterCode(undefined);
@@ -157,49 +176,34 @@ function RegisterCodeSettingItem() {
}, [user, registerCode]);
return (
- <>
- <SettingItemContainer
- title="settings.myRegisterCode"
- description="settings.myRegisterCodeDesc"
- className="register-code-setting-item"
- onClick={() => setDialogOpen(true)}
- >
- {registerCode === undefined ? (
- <Spinner />
- ) : registerCode === null ? (
- <span>Noop</span>
- ) : (
- <code
- className="register-code"
- onClick={(event) => {
- void navigator.clipboard.writeText(registerCode).then(() => {
- pushAlert({
- type: "create",
- message: "settings.myRegisterCodeCopied",
- });
+ <SettingItemContainer
+ title="settings.myRegisterCode"
+ description="settings.myRegisterCodeDesc"
+ className="register-code-setting-item"
+ onClick={createDialogSwitch("confirm")}
+ >
+ {registerCode === undefined ? (
+ <Spinner />
+ ) : registerCode === null ? (
+ <span>Noop</span>
+ ) : (
+ <code
+ className="register-code"
+ onClick={(event) => {
+ void navigator.clipboard.writeText(registerCode).then(() => {
+ pushAlert({
+ type: "create",
+ message: "settings.myRegisterCodeCopied",
});
- event.stopPropagation();
- }}
- >
- {registerCode}
- </code>
- )}
- </SettingItemContainer>
- <ConfirmDialog
- title="settings.renewRegisterCode"
- body="settings.renewRegisterCodeDesc"
- onClose={() => setDialogOpen(false)}
- open={dialogOpen}
- onConfirm={() => {
- if (user == null) throw new Error();
- void getHttpUserClient()
- .renewRegisterCode(user.username)
- .then(() => {
- setRegisterCode(undefined);
});
- }}
- />{" "}
- </>
+ event.stopPropagation();
+ }}
+ >
+ {registerCode}
+ </code>
+ )}
+ <DialogProvider controller={controller} />
+ </SettingItemContainer>
);
}
@@ -240,12 +244,22 @@ export default function SettingPage() {
const user = useUser();
const navigate = useNavigate();
- const { dialogPropsMap, createDialogSwitch } = useDialog([
- "change-password",
- "change-avatar",
- "change-nickname",
- "logout",
- ]);
+ const { controller, createDialogSwitch } = useDialog({
+ "change-password": <ChangeNicknameDialog />,
+ "change-avatar": <ChangeAvatarDialog />,
+ "change-nickname": <ChangePasswordDialog />,
+ logout: (
+ <ConfirmDialog
+ title="settings.dialogConfirmLogout.title"
+ body="settings.dialogConfirmLogout.prompt"
+ onConfirm={() => {
+ void userService.logout().then(() => {
+ navigate("/");
+ });
+ }}
+ />
+ ),
+ });
return (
<Page noTopPadding>
@@ -275,23 +289,7 @@ export default function SettingPage() {
<SettingSection title="settings.subheader.customization">
<LanguageChangeSettingItem />
</SettingSection>
- <ChangePasswordDialog {...dialogPropsMap["change-password"]} />
- {user && (
- <>
- <ConfirmDialog
- title="settings.dialogConfirmLogout.title"
- body="settings.dialogConfirmLogout.prompt"
- onConfirm={() => {
- void userService.logout().then(() => {
- navigate("/");
- });
- }}
- {...dialogPropsMap["logout"]}
- />
- <ChangeAvatarDialog {...dialogPropsMap["change-avatar"]} />
- <ChangeNicknameDialog {...dialogPropsMap["change-nickname"]} />
- </>
- )}
+ <DialogProvider controller={controller} />
</Page>
);
}