import { FC } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; import { SendHorizonal } from "lucide-react"; import { Controller, useForm } from "react-hook-form"; // services import { PageService } from "services/page.service"; // hooks import useToast from "hooks/use-toast"; // ui import { TextArea } from "@plane/ui"; // types import { IUser, IPageBlock } from "types"; // fetch-keys import { PAGE_BLOCKS_LIST } from "constants/fetch-keys"; const defaultValues = { name: "", }; type Props = { user: IUser | undefined; }; const pageService = new PageService(); export const CreateBlock: FC = ({ user }) => { // const [blockTitle, setBlockTitle] = useState(""); // router const router = useRouter(); const { workspaceSlug, projectId, pageId } = router.query; // toast const { setToastAlert } = useToast(); // form info const { handleSubmit, control, watch, reset, formState: { errors }, } = useForm({ defaultValues, }); const createPageBlock = async () => { if (!workspaceSlug || !projectId || !pageId) return; await pageService .createPageBlock( workspaceSlug as string, projectId as string, pageId as string, { name: watch("name"), }, user ) .then((res) => { mutate( PAGE_BLOCKS_LIST(pageId as string), (prevData) => [...(prevData as IPageBlock[]), res], false ); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Page could not be created. Please try again.", }); }); reset(); }; const handleKeyDown = (e: any) => { const keyCombination = ((e.ctrlKey || e.metaKey) && e.key === "Enter") || (e.shiftKey && e.key === "Enter"); if (e.key === "Enter" && !keyCombination) { if (watch("name") && watch("name") !== "") { e.preventDefault(); createPageBlock(); reset(); } } }; return (
(