diff --git a/web/src/components/CreateShortcutDialog.tsx b/web/src/components/CreateShortcutDialog.tsx index afbe0ad..ecdcda6 100644 --- a/web/src/components/CreateShortcutDialog.tsx +++ b/web/src/components/CreateShortcutDialog.tsx @@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next"; import { toast } from "react-hot-toast"; import { shortcutService } from "../services"; import useLoading from "../hooks/useLoading"; +import { showCommonDialog } from "./Alert"; import Icon from "./Icon"; interface Props { @@ -32,6 +33,7 @@ const CreateShortcutDialog: React.FC = (props: Props) => { }); const [tag, setTag] = useState(""); const requestState = useLoading(false); + const isEditing = !!shortcutId; useEffect(() => { if (shortcutId) { @@ -51,27 +53,35 @@ const CreateShortcutDialog: React.FC = (props: Props) => { } }, [shortcutId]); - const handleInputChange = (e: React.ChangeEvent, key: string) => { - const text = e.target.value as string; - const tempObject = {} as any; - tempObject[key] = text; - + const setPartialState = (partialState: Partial) => { setState({ ...state, - shortcutCreate: Object.assign(state.shortcutCreate, tempObject), + ...partialState, }); }; const handleNameInputChange = (e: React.ChangeEvent) => { - handleInputChange(e, "name"); + setPartialState({ + shortcutCreate: Object.assign(state.shortcutCreate, { + name: e.target.value.replace(/\s+/g, "-").toLowerCase(), + }), + }); }; const handleLinkInputChange = (e: React.ChangeEvent) => { - handleInputChange(e, "link"); + setPartialState({ + shortcutCreate: Object.assign(state.shortcutCreate, { + link: e.target.value, + }), + }); }; const handleDescriptionInputChange = (e: React.ChangeEvent) => { - handleInputChange(e, "description"); + setPartialState({ + shortcutCreate: Object.assign(state.shortcutCreate, { + description: e.target.value, + }), + }); }; const handleTagsInputChange = (e: React.ChangeEvent) => { @@ -80,7 +90,27 @@ const CreateShortcutDialog: React.FC = (props: Props) => { }; const handleVisibilityInputChange = (e: React.ChangeEvent) => { - handleInputChange(e, "visibility"); + setPartialState({ + shortcutCreate: Object.assign(state.shortcutCreate, { + visibility: e.target.value, + }), + }); + }; + + const handleDeleteShortcutButtonClick = () => { + if (!shortcutId) { + return; + } + + showCommonDialog({ + title: "Delete Shortcut", + content: `Are you sure to delete shortcut \`${state.shortcutCreate.name}\`? You can not undo this action.`, + style: "danger", + onConfirm: async () => { + await shortcutService.deleteShortcutById(shortcutId); + onClose(); + }, + }); }; const handleSaveBtnClick = async () => { @@ -121,7 +151,7 @@ const CreateShortcutDialog: React.FC = (props: Props) => {
- {shortcutId ? "Edit Shortcut" : "Create Shortcut"} + {isEditing ? "Edit Shortcut" : "Create Shortcut"} @@ -182,13 +212,22 @@ const CreateShortcutDialog: React.FC = (props: Props) => {
-
- - +
+
+ {isEditing && ( + + )} +
+
+ + +