chore: update draft issue logic

This commit is contained in:
Aaryan Khandelwal 2023-11-24 15:00:47 +05:30
parent a16515b577
commit 9c15de0a38
3 changed files with 52 additions and 47 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from "react"; import React from "react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { observer } from "mobx-react-lite"; import { observer } from "mobx-react-lite";
import useSWR from "swr"; import useSWR from "swr";
@ -14,11 +14,8 @@ import {
ProjectSpreadsheetLayout, ProjectSpreadsheetLayout,
} from "components/issues"; } from "components/issues";
import { Spinner } from "@plane/ui"; import { Spinner } from "@plane/ui";
import { ProjectIssueModal } from "components/issues/issue-modal/modal";
export const ProjectLayoutRoot: React.FC = observer(() => { export const ProjectLayoutRoot: React.FC = observer(() => {
const [isModalOpen, setIsModalOpen] = useState(true);
const router = useRouter(); const router = useRouter();
const { workspaceSlug, projectId } = router.query as { workspaceSlug: string; projectId: string }; const { workspaceSlug, projectId } = router.query as { workspaceSlug: string; projectId: string };
@ -37,8 +34,6 @@ export const ProjectLayoutRoot: React.FC = observer(() => {
const activeLayout = issueFilters?.displayFilters?.layout; const activeLayout = issueFilters?.displayFilters?.layout;
return ( return (
<>
{projectId && <ProjectIssueModal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)} />}
<div className="relative w-full h-full flex flex-col overflow-hidden"> <div className="relative w-full h-full flex flex-col overflow-hidden">
<ProjectAppliedFiltersRoot /> <ProjectAppliedFiltersRoot />
@ -65,6 +60,5 @@ export const ProjectLayoutRoot: React.FC = observer(() => {
</> </>
)} )}
</div> </div>
</>
); );
}); });

View File

@ -17,7 +17,7 @@ export interface DraftIssueProps {
changesMade: Partial<IIssue> | null; changesMade: Partial<IIssue> | null;
data?: IIssue; data?: IIssue;
onChange: (formData: Partial<IIssue> | null) => void; onChange: (formData: Partial<IIssue> | null) => void;
onClose: () => void; onClose: (saveDraftIssueInLocalStorage?: boolean) => void;
onSubmit: (formData: Partial<IIssue>) => Promise<void>; onSubmit: (formData: Partial<IIssue>) => Promise<void>;
projectId: string; projectId: string;
} }
@ -39,7 +39,7 @@ export const DraftIssueLayout: React.FC<DraftIssueProps> = observer((props) => {
const handleClose = () => { const handleClose = () => {
if (changesMade) setIssueDiscardModal(true); if (changesMade) setIssueDiscardModal(true);
else onClose(); else onClose(false);
}; };
const handleCreateDraftIssue = async () => { const handleCreateDraftIssue = async () => {
@ -58,7 +58,7 @@ export const DraftIssueLayout: React.FC<DraftIssueProps> = observer((props) => {
onChange(null); onChange(null);
setIssueDiscardModal(false); setIssueDiscardModal(false);
handleClose(); onClose(false);
}) })
.catch(() => .catch(() =>
setToastAlert({ setToastAlert({
@ -84,7 +84,7 @@ export const DraftIssueLayout: React.FC<DraftIssueProps> = observer((props) => {
onDiscard={() => { onDiscard={() => {
onChange(null); onChange(null);
setIssueDiscardModal(false); setIssueDiscardModal(false);
onClose(); onClose(false);
}} }}
/> />
<IssueFormRoot <IssueFormRoot

View File

@ -9,6 +9,7 @@ import useToast from "hooks/use-toast";
import useLocalStorage from "hooks/use-local-storage"; import useLocalStorage from "hooks/use-local-storage";
// components // components
import { DraftIssueLayout } from "./draft-issue-layout"; import { DraftIssueLayout } from "./draft-issue-layout";
import { IssueFormRoot } from "./form";
// types // types
import type { IIssue } from "types"; import type { IIssue } from "types";
@ -16,10 +17,11 @@ export interface IssuesModalProps {
data?: IIssue; data?: IIssue;
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
withDraftIssueWrapper?: boolean;
} }
export const ProjectIssueModal: React.FC<IssuesModalProps> = observer((props) => { export const ProjectIssueModal: React.FC<IssuesModalProps> = observer((props) => {
const { data, isOpen, onClose } = props; const { data, isOpen, onClose, withDraftIssueWrapper } = props;
const [changesMade, setChangesMade] = useState<Partial<IIssue> | null>(null); const [changesMade, setChangesMade] = useState<Partial<IIssue> | null>(null);
@ -38,8 +40,8 @@ export const ProjectIssueModal: React.FC<IssuesModalProps> = observer((props) =>
const { setValue: setLocalStorageDraftIssue } = useLocalStorage<any>("draftedIssue", {}); const { setValue: setLocalStorageDraftIssue } = useLocalStorage<any>("draftedIssue", {});
const handleClose = () => { const handleClose = (saveDraftIssueInLocalStorage?: boolean) => {
if (changesMade) { if (changesMade && saveDraftIssueInLocalStorage) {
const draftIssue = JSON.stringify(changesMade); const draftIssue = JSON.stringify(changesMade);
setLocalStorageDraftIssue(draftIssue); setLocalStorageDraftIssue(draftIssue);
@ -133,7 +135,7 @@ export const ProjectIssueModal: React.FC<IssuesModalProps> = observer((props) =>
return ( return (
<Transition.Root show={isOpen} as={React.Fragment}> <Transition.Root show={isOpen} as={React.Fragment}>
<Dialog as="div" className="relative z-20" onClose={handleClose}> <Dialog as="div" className="relative z-20" onClose={() => handleClose(true)}>
<Transition.Child <Transition.Child
as={React.Fragment} as={React.Fragment}
enter="ease-out duration-300" enter="ease-out duration-300"
@ -158,6 +160,7 @@ export const ProjectIssueModal: React.FC<IssuesModalProps> = observer((props) =>
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
> >
<Dialog.Panel className="relative transform rounded-lg border border-custom-border-200 bg-custom-background-100 p-5 text-left shadow-xl transition-all sm:w-full mx-4 sm:max-w-4xl"> <Dialog.Panel className="relative transform rounded-lg border border-custom-border-200 bg-custom-background-100 p-5 text-left shadow-xl transition-all sm:w-full mx-4 sm:max-w-4xl">
{withDraftIssueWrapper ? (
<DraftIssueLayout <DraftIssueLayout
changesMade={changesMade} changesMade={changesMade}
data={data} data={data}
@ -166,6 +169,14 @@ export const ProjectIssueModal: React.FC<IssuesModalProps> = observer((props) =>
onSubmit={handleFormSubmit} onSubmit={handleFormSubmit}
projectId={selectedProjectId} projectId={selectedProjectId}
/> />
) : (
<IssueFormRoot
data={data}
onClose={() => handleClose(false)}
onSubmit={handleFormSubmit}
projectId={selectedProjectId}
/>
)}
</Dialog.Panel> </Dialog.Panel>
</Transition.Child> </Transition.Child>
</div> </div>