diff --git a/apps/app/components/pages/create-block.tsx b/apps/app/components/pages/create-block.tsx new file mode 100644 index 000000000..d8b54a4f8 --- /dev/null +++ b/apps/app/components/pages/create-block.tsx @@ -0,0 +1,113 @@ +import React, { KeyboardEventHandler, useCallback, useEffect, useState } from "react"; + +import { useRouter } from "next/router"; + +import { mutate } from "swr"; + +import { PaperAirplaneIcon } from "@heroicons/react/24/outline"; + +// react-hook-form +import { useForm } from "react-hook-form"; +// services +import pagesService from "services/pages.service"; + +// hooks +import useToast from "hooks/use-toast"; +// ui +import { TextArea } from "components/ui"; +// types +import { IPageBlock } from "types"; +// fetch-keys +import { PAGE_BLOCKS_LIST } from "constants/fetch-keys"; + +const defaultValues = { + name: "", +}; + +export const CreateBlock = () => { + const [blockTitle, setBlockTitle] = useState(""); + + const router = useRouter(); + const { workspaceSlug, projectId, pageId } = router.query; + + const { setToastAlert } = useToast(); + + const { + handleSubmit, + register, + control, + watch, + setValue, + setFocus, + reset, + formState: { isSubmitting }, + } = useForm({ + defaultValues, + }); + + const createPageBlock = async () => { + if (!workspaceSlug || !projectId || !pageId) return; + + await pagesService + .createPageBlock(workspaceSlug as string, projectId as string, pageId as string, { + name: watch("name"), + }) + .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 ( +
+
+
+