From eab1d9329bb0aa0e44c677c8513ce12da82f9661 Mon Sep 17 00:00:00 2001 From: Lakhan Baheti <94619783+1akhanBaheti@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:59:17 +0530 Subject: [PATCH] feat: editor for issue description (#2038) --- apps/app/pages/[workspaceSlug]/editor.tsx | 192 ++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 apps/app/pages/[workspaceSlug]/editor.tsx diff --git a/apps/app/pages/[workspaceSlug]/editor.tsx b/apps/app/pages/[workspaceSlug]/editor.tsx new file mode 100644 index 000000000..73f0932ea --- /dev/null +++ b/apps/app/pages/[workspaceSlug]/editor.tsx @@ -0,0 +1,192 @@ +import { TipTapEditor } from "components/tiptap"; +import type { NextPage } from "next"; +import { useCallback, useEffect, useState } from "react"; +import { Controller, useForm } from "react-hook-form"; +import issuesService from "services/issues.service"; +import { ICurrentUserResponse, IIssue } from "types"; +import useReloadConfirmations from "hooks/use-reload-confirmation"; +import { Spinner } from "components/ui"; +import Image404 from "public/404.svg"; +import DefaultLayout from "layouts/default-layout"; +import Image from "next/image"; +import userService from "services/user.service"; +import { useRouter } from "next/router"; + +const Editor: NextPage = () => { + const [user, setUser] = useState(); + const [isSubmitting, setIsSubmitting] = useState<"submitting" | "submitted" | "saved">("saved"); + const [isLoading, setIsLoading] = useState("false"); + const { setShowAlert } = useReloadConfirmations(); + const [cookies, setCookies] = useState({}); + const [issueDetail, setIssueDetail] = useState(null); + const router = useRouter(); + const { editable } = router.query; + const { + handleSubmit, + watch, + setValue, + control, + formState: { errors }, + } = useForm({ + defaultValues: { + name: "", + description: "", + description_html: "", + }, + }); + + const getCookies = () => { + const cookies = document.cookie.split(";"); + const cookieObj: any = {}; + cookies.forEach((cookie) => { + const cookieArr = cookie.split("="); + cookieObj[cookieArr[0].trim()] = cookieArr[1]; + }); + + setCookies(cookieObj); + return cookieObj; + }; + + const getIssueDetail = async (cookiesData: any) => { + try { + setIsLoading("true"); + const userData = await userService.currentUser(); + setUser(userData); + const issueDetail = await issuesService.retrieve( + cookiesData.MOBILE_slug, + cookiesData.MOBILE_project_id, + cookiesData.MOBILE_issue_id + ); + setIssueDetail(issueDetail); + setIsLoading("false"); + setValue("description_html", issueDetail.description_html); + setValue("description", issueDetail.description); + } catch (e) { + setIsLoading("error"); + console.log(e); + } + }; + useEffect(() => { + const cookiesData = getCookies(); + + getIssueDetail(cookiesData); + }, []); + + useEffect(() => { + if (isSubmitting === "submitted") { + setShowAlert(false); + setTimeout(async () => { + setIsSubmitting("saved"); + }, 2000); + } else if (isSubmitting === "submitting") { + setShowAlert(true); + } + }, [isSubmitting, setShowAlert]); + + const submitChanges = async ( + formData: Partial, + workspaceSlug: string, + projectId: string, + issueId: string + ) => { + if (!workspaceSlug || !projectId || !issueId) return; + + const payload: Partial = { + ...formData, + }; + + delete payload.blocker_issues; + delete payload.blocked_issues; + await issuesService + .patchIssue(workspaceSlug as string, projectId as string, issueId as string, payload, user) + .catch((e) => { + console.log(e); + }); + }; + + const handleDescriptionFormSubmit = useCallback( + async (formData: Partial) => { + if (!formData) return; + + await submitChanges( + { + name: issueDetail?.name ?? "", + description: formData.description ?? "", + description_html: formData.description_html ?? "

", + }, + cookies.MOBILE_slug, + cookies.MOBILE_project_id, + cookies.MOBILE_issue_id + ); + }, + [submitChanges] + ); + + return isLoading === "error" ? ( + + ) : isLoading === "true" ? ( +
+ +
+ ) : ( +
+ ( + { + setShowAlert(true); + setIsSubmitting("submitting"); + onChange(description_html); + setValue("description", description); + handleSubmit(handleDescriptionFormSubmit)().finally(() => { + setIsSubmitting("submitted"); + }); + }} + /> + )} + /> +
+ {isSubmitting === "submitting" ? "Saving..." : "Saved"} +
+
+ ); +}; + +const ErrorEncountered: NextPage = () => ( + +
+
+
+ 404- Page not found +
+
+

Oops! Something went wrong.

+
+
+
+
+); + +export default Editor;