import React, { useEffect } from "react"; // next imports import { useRouter } from "next/router"; // react-hook-form import { useForm } from "react-hook-form"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // ui components import { ToggleSwitch, PrimaryButton, SecondaryButton } from "components/ui"; import { CustomPopover } from "./popover"; // mobx react lite import { observer } from "mobx-react-lite"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; import { RootStore } from "store/root"; import { IProjectPublishSettingsViews } from "store/project-publish"; // hooks import useToast from "hooks/use-toast"; import useProjectDetails from "hooks/use-project-details"; type Props = { // user: ICurrentUserResponse | undefined; }; const defaultValues: Partial = { id: null, comments: false, reactions: false, votes: false, inbox: null, views: ["list", "kanban"], }; const viewOptions = [ { key: "list", value: "List" }, { key: "kanban", value: "Kanban" }, // { key: "calendar", value: "Calendar" }, // { key: "gantt", value: "Gantt" }, // { key: "spreadsheet", value: "Spreadsheet" }, ]; export const PublishProjectModal: React.FC = observer(() => { const store: RootStore = useMobxStore(); const { projectPublish } = store; const { projectDetails, mutateProjectDetails } = useProjectDetails(); const { setToastAlert } = useToast(); const handleToastAlert = (title: string, type: string, message: string) => { setToastAlert({ title: title || "Title", type: "error" || "warning", message: message || "Message", }); }; const { NEXT_PUBLIC_DEPLOY_URL } = process.env; const plane_deploy_url = NEXT_PUBLIC_DEPLOY_URL ? NEXT_PUBLIC_DEPLOY_URL : "http://localhost:3001"; const router = useRouter(); const { workspaceSlug } = router.query; const { formState: { errors, isSubmitting }, handleSubmit, reset, watch, setValue, } = useForm({ defaultValues, reValidateMode: "onChange", }); const handleClose = () => { projectPublish.handleProjectModal(null); reset({ ...defaultValues }); }; useEffect(() => { if ( projectPublish.projectPublishSettings && projectPublish.projectPublishSettings != "not-initialized" ) { let userBoards: string[] = []; if (projectPublish.projectPublishSettings?.views) { const _views: IProjectPublishSettingsViews | null = projectPublish.projectPublishSettings?.views || null; if (_views != null) { if (_views.list) userBoards.push("list"); if (_views.kanban) userBoards.push("kanban"); if (_views.calendar) userBoards.push("calendar"); if (_views.gantt) userBoards.push("gantt"); if (_views.spreadsheet) userBoards.push("spreadsheet"); userBoards = userBoards && userBoards.length > 0 ? userBoards : ["list"]; } } const updatedData = { id: projectPublish.projectPublishSettings?.id || null, comments: projectPublish.projectPublishSettings?.comments || false, reactions: projectPublish.projectPublishSettings?.reactions || false, votes: projectPublish.projectPublishSettings?.votes || false, inbox: projectPublish.projectPublishSettings?.inbox || null, views: userBoards, }; reset({ ...updatedData }); } }, [reset, projectPublish.projectPublishSettings]); useEffect(() => { if ( projectPublish.projectPublishModal && workspaceSlug && projectPublish.project_id != null && projectPublish?.projectPublishSettings === "not-initialized" ) { projectPublish.getProjectSettingsAsync( workspaceSlug as string, projectPublish.project_id as string, null ); } }, [workspaceSlug, projectPublish, projectPublish.projectPublishModal]); const onSettingsPublish = async (formData: any) => { if (formData.views && formData.views.length > 0) { const payload = { comments: formData.comments || false, reactions: formData.reactions || false, votes: formData.votes || false, inbox: formData.inbox || null, views: { list: formData.views.includes("list") || false, kanban: formData.views.includes("kanban") || false, calendar: formData.views.includes("calendar") || false, gantt: formData.views.includes("gantt") || false, spreadsheet: formData.views.includes("spreadsheet") || false, }, }; const _workspaceSlug = workspaceSlug; const _projectId = projectPublish.project_id; return projectPublish .createProjectSettingsAsync(_workspaceSlug as string, _projectId as string, payload, null) .then((response) => { mutateProjectDetails(); handleClose(); console.log("_projectId", _projectId); if (_projectId) window.open(`${plane_deploy_url}/${_workspaceSlug}/${_projectId}`, "_blank"); return response; }) .catch((error) => { console.error("error", error); return error; }); } else { handleToastAlert("Missing fields", "warning", "Please select at least one view to publish"); } }; const onSettingsUpdate = async (key: string, value: any) => { const payload = { comments: key === "comments" ? value : watch("comments"), reactions: key === "reactions" ? value : watch("reactions"), votes: key === "votes" ? value : watch("votes"), inbox: key === "inbox" ? value : watch("inbox"), views: key === "views" ? { list: value.includes("list") ? true : false, kanban: value.includes("kanban") ? true : false, calendar: value.includes("calendar") ? true : false, gantt: value.includes("gantt") ? true : false, spreadsheet: value.includes("spreadsheet") ? true : false, } : { list: watch("views").includes("list") ? true : false, kanban: watch("views").includes("kanban") ? true : false, calendar: watch("views").includes("calendar") ? true : false, gantt: watch("views").includes("gantt") ? true : false, spreadsheet: watch("views").includes("spreadsheet") ? true : false, }, }; return projectPublish .updateProjectSettingsAsync( workspaceSlug as string, projectPublish.project_id as string, watch("id"), payload, null ) .then((response) => { mutateProjectDetails(); return response; }) .catch((error) => { console.log("error", error); return error; }); }; const onSettingsUnPublish = async (formData: any) => projectPublish .deleteProjectSettingsAsync( workspaceSlug as string, projectPublish.project_id as string, formData?.id, null ) .then((response) => { mutateProjectDetails(); reset({ ...defaultValues }); handleClose(); return response; }) .catch((error) => { console.error("error", error); return error; }); const CopyLinkToClipboard = ({ copy_link }: { copy_link: string }) => { const [status, setStatus] = React.useState(false); const copyText = () => { navigator.clipboard.writeText(copy_link); setStatus(true); setTimeout(() => { setStatus(false); }, 1000); }; return (
copyText()} > {status ? "Copied" : "Copy Link"}
); }; return (
{/* heading */}
Publish
{projectPublish.loader && (
Changes saved
)}
close
{/* content */}
{watch("id") && (
radio_button_checked
This project is live on web
)}
{`${plane_deploy_url}/${workspaceSlug}/${projectPublish.project_id}`}
Views
0 ? viewOptions .filter( (_view) => watch("views").includes(_view.key) && _view.value ) .map((_view) => _view.value) .join(", ") : `` } placeholder="Select views" > <> {viewOptions && viewOptions.length > 0 && viewOptions.map((_view) => (
{ const _views = watch("views") && watch("views").length > 0 ? watch("views").includes(_view?.key) ? watch("views").filter((_o: string) => _o !== _view?.key) : [...watch("views"), _view?.key] : [_view?.key]; setValue("views", _views); if (watch("id") != null) onSettingsUpdate("views", _views); }} >
{_view.value}
{watch("views") && watch("views").length > 0 && watch("views").includes(_view.key) && ( done )}
))}
{/*
Allow comments
{ const _comments = !watch("comments"); setValue("comments", _comments); if (watch("id") != null) onSettingsUpdate("comments", _comments); }} size="sm" />
*/} {/*
Allow reactions
{ const _reactions = !watch("reactions"); setValue("reactions", _reactions); if (watch("id") != null) onSettingsUpdate("reactions", _reactions); }} size="sm" />
*/} {/*
Allow Voting
{ const _votes = !watch("votes"); setValue("votes", _votes); if (watch("id") != null) onSettingsUpdate("votes", _votes); }} size="sm" />
*/} {/*
Allow issue proposals
{ setValue("inbox", !watch("inbox")); }} size="sm" />
*/}
{/* modal handlers */}
public
Anyone with the link can access
Cancel {watch("id") != null ? ( {isSubmitting ? "Unpublishing..." : "Unpublish"} ) : ( {isSubmitting ? "Publishing..." : "Publish"} )}
); });