import { useEffect, useState } from "react"; import { shortcutService } from "../services"; import useLoading from "../hooks/useLoading"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; interface Props extends DialogProps { workspaceId: WorkspaceId; shortcutId?: ShortcutId; } interface State { shortcutCreate: ShortcutCreate; } const CreateShortcutDialog: React.FC = (props: Props) => { const { destroy, workspaceId, shortcutId } = props; const [state, setState] = useState({ shortcutCreate: { workspaceId: workspaceId, name: "", link: "", description: "", visibility: "PRIVATE", }, }); const requestState = useLoading(false); useEffect(() => { if (shortcutId) { const shortcutTemp = shortcutService.getShortcutById(shortcutId); if (shortcutTemp) { setState({ ...state, shortcutCreate: Object.assign(state.shortcutCreate, { workspaceId: shortcutTemp.workspaceId, name: shortcutTemp.name, link: shortcutTemp.link, description: shortcutTemp.description, visibility: shortcutTemp.visibility, }), }); } } }, [shortcutId]); const handleInputChange = (e: React.ChangeEvent, key: string) => { const text = e.target.value as string; const tempObject = {} as any; tempObject[key] = text; setState({ ...state, shortcutCreate: Object.assign(state.shortcutCreate, tempObject), }); }; const handleNameInputChange = (e: React.ChangeEvent) => { handleInputChange(e, "name"); }; const handleLinkInputChange = (e: React.ChangeEvent) => { handleInputChange(e, "link"); }; const handleDescriptionInputChange = (e: React.ChangeEvent) => { handleInputChange(e, "description"); }; const handleVisibilityInputChange = (e: React.ChangeEvent) => { handleInputChange(e, "visibility"); }; const handleSaveBtnClick = async () => { if (!state.shortcutCreate.name) { toastHelper.error("Name is required"); return; } try { if (shortcutId) { await shortcutService.patchShortcut({ id: shortcutId, name: state.shortcutCreate.name, link: state.shortcutCreate.link, description: state.shortcutCreate.description, visibility: state.shortcutCreate.visibility, }); } else { await shortcutService.createShortcut(state.shortcutCreate); } destroy(); } catch (error: any) { console.error(error); toastHelper.error(error.response.data.error || error.response.data.message); } }; return ( <>

{shortcutId ? "Edit Shortcut" : "Create Shortcut"}

Name
Link
Description
Visibility
); }; export default function showCreateShortcutDialog(workspaceId: WorkspaceId, shortcutId?: ShortcutId): void { generateDialog( { className: "px-2 sm:px-0", }, CreateShortcutDialog, { workspaceId, shortcutId, } ); }