import { useRouter } from "next/router"; import dynamic from "next/dynamic"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // services import pagesService from "services/pages.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Input, Loader, PrimaryButton, SecondaryButton } from "components/ui"; // types import { IPageBlock } from "types"; // fetch-keys import { PAGE_BLOCKS_LIST } from "constants/fetch-keys"; import { useCallback, useEffect } from "react"; import issuesService from "services/issues.service"; type Props = { handleClose: () => void; data?: IPageBlock; setIsSyncing?: React.Dispatch>; focus?: keyof IPageBlock; }; const defaultValues = { name: "", description: "

", }; const RemirrorRichTextEditor = dynamic(() => import("components/rich-text-editor"), { ssr: false, loading: () => ( ), }); export const CreateUpdateBlockInline: React.FC = ({ handleClose, data, setIsSyncing, focus, }) => { 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 onClose = useCallback(() => { if (data) handleClose(); reset(); }, [handleClose, reset, data]); const createPageBlock = async (formData: Partial) => { if (!workspaceSlug || !projectId || !pageId) return; await pagesService .createPageBlock(workspaceSlug as string, projectId as string, pageId as string, { name: formData.name, description: formData.description ?? "", description_html: formData.description_html ?? "

", }) .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.", }); }) .finally(() => onClose()); }; const updatePageBlock = async (formData: Partial) => { if (!workspaceSlug || !projectId || !pageId || !data) return; if (data.issue && data.sync && setIsSyncing) setIsSyncing(true); mutate( PAGE_BLOCKS_LIST(pageId as string), (prevData) => prevData?.map((p) => { if (p.id === data.id) return { ...p, ...formData }; return p; }), false ); await pagesService .patchPageBlock(workspaceSlug as string, projectId as string, pageId as string, data.id, { name: formData.name, description: formData.description, description_html: formData.description_html, }) .then((res) => { mutate(PAGE_BLOCKS_LIST(pageId as string)); if (data.issue && data.sync) issuesService .patchIssue(workspaceSlug as string, projectId as string, data.issue, { name: res.name, description: res.description, description_html: res.description_html, }) .finally(() => { if (setIsSyncing) setIsSyncing(false); }); }) .finally(() => onClose()); }; useEffect(() => { if (focus) setFocus(focus); if (!data) return; reset({ ...defaultValues, name: data.name, description: data.description, description_html: data.description_html, }); }, [reset, data, focus, setFocus]); useEffect(() => { window.addEventListener("keydown", (e: KeyboardEvent) => { if (e.key === "Escape") handleClose(); }); return () => { window.removeEventListener("keydown", (e: KeyboardEvent) => { if (e.key === "Escape") handleClose(); }); }; }, [handleClose]); return (
( setValue("description", jsonValue)} onHTMLChange={(htmlValue) => setValue("description_html", htmlValue)} placeholder="Description" customClassName="text-sm" noBorder borderOnFocus={false} /> )} />
Cancel {data ? isSubmitting ? "Updating..." : "Update block" : isSubmitting ? "Adding..." : "Add block"}
); };