chore: add delete button in edit dialog

This commit is contained in:
Steven 2023-06-29 22:05:43 +08:00
parent 774966f2eb
commit fbf958cf3e

View File

@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import { shortcutService } from "../services"; import { shortcutService } from "../services";
import useLoading from "../hooks/useLoading"; import useLoading from "../hooks/useLoading";
import { showCommonDialog } from "./Alert";
import Icon from "./Icon"; import Icon from "./Icon";
interface Props { interface Props {
@ -32,6 +33,7 @@ const CreateShortcutDialog: React.FC<Props> = (props: Props) => {
}); });
const [tag, setTag] = useState<string>(""); const [tag, setTag] = useState<string>("");
const requestState = useLoading(false); const requestState = useLoading(false);
const isEditing = !!shortcutId;
useEffect(() => { useEffect(() => {
if (shortcutId) { if (shortcutId) {
@ -51,27 +53,35 @@ const CreateShortcutDialog: React.FC<Props> = (props: Props) => {
} }
}, [shortcutId]); }, [shortcutId]);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>, key: string) => { const setPartialState = (partialState: Partial<State>) => {
const text = e.target.value as string;
const tempObject = {} as any;
tempObject[key] = text;
setState({ setState({
...state, ...state,
shortcutCreate: Object.assign(state.shortcutCreate, tempObject), ...partialState,
}); });
}; };
const handleNameInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleNameInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
handleInputChange(e, "name"); setPartialState({
shortcutCreate: Object.assign(state.shortcutCreate, {
name: e.target.value.replace(/\s+/g, "-").toLowerCase(),
}),
});
}; };
const handleLinkInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleLinkInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
handleInputChange(e, "link"); setPartialState({
shortcutCreate: Object.assign(state.shortcutCreate, {
link: e.target.value,
}),
});
}; };
const handleDescriptionInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleDescriptionInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
handleInputChange(e, "description"); setPartialState({
shortcutCreate: Object.assign(state.shortcutCreate, {
description: e.target.value,
}),
});
}; };
const handleTagsInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleTagsInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@ -80,7 +90,27 @@ const CreateShortcutDialog: React.FC<Props> = (props: Props) => {
}; };
const handleVisibilityInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleVisibilityInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
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 () => { const handleSaveBtnClick = async () => {
@ -121,7 +151,7 @@ const CreateShortcutDialog: React.FC<Props> = (props: Props) => {
<Modal open={true}> <Modal open={true}>
<ModalDialog> <ModalDialog>
<div className="flex flex-row justify-between items-center w-80 sm:w-96 mb-4"> <div className="flex flex-row justify-between items-center w-80 sm:w-96 mb-4">
<span className="text-lg font-medium">{shortcutId ? "Edit Shortcut" : "Create Shortcut"}</span> <span className="text-lg font-medium">{isEditing ? "Edit Shortcut" : "Create Shortcut"}</span>
<Button variant="plain" onClick={onClose}> <Button variant="plain" onClick={onClose}>
<Icon.X className="w-5 h-auto text-gray-600" /> <Icon.X className="w-5 h-auto text-gray-600" />
</Button> </Button>
@ -182,13 +212,22 @@ const CreateShortcutDialog: React.FC<Props> = (props: Props) => {
</RadioGroup> </RadioGroup>
</div> </div>
</div> </div>
<div className="w-full flex flex-row justify-end items-center mt-4 space-x-2"> <div className="w-full flex flex-row justify-between items-center mt-8 space-x-2">
<Button color="neutral" variant="plain" disabled={requestState.isLoading} loading={requestState.isLoading} onClick={onClose}> <div>
Cancel {isEditing && (
</Button> <Button color="danger" variant="plain" onClick={handleDeleteShortcutButtonClick}>
<Button color="primary" disabled={requestState.isLoading} loading={requestState.isLoading} onClick={handleSaveBtnClick}> Delete
Save </Button>
</Button> )}
</div>
<div className="space-x-2">
<Button color="neutral" variant="plain" disabled={requestState.isLoading} loading={requestState.isLoading} onClick={onClose}>
Cancel
</Button>
<Button color="primary" disabled={requestState.isLoading} loading={requestState.isLoading} onClick={handleSaveBtnClick}>
Save
</Button>
</div>
</div> </div>
</div> </div>
</ModalDialog> </ModalDialog>