From 0e07c1e19fc1ee18f372dc0e228fdf3ee4c8f168 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Sun, 5 Feb 2023 22:01:23 +0530 Subject: [PATCH] refactor: removed unnecessary api calls --- .../core/board-view/single-board.tsx | 3 +- .../core/board-view/single-issue.tsx | 47 +++------- .../core/bulk-delete-issues-modal.tsx | 24 ++--- .../core/existing-issues-list-modal.tsx | 19 +--- apps/app/components/core/issues-view.tsx | 62 +++++++++++- .../components/core/list-view/all-lists.tsx | 5 +- .../core/list-view/single-issue.tsx | 39 ++++---- .../components/core/list-view/single-list.tsx | 30 +++++- .../projects/[projectId]/cycles/[cycleId].tsx | 35 +------ .../projects/[projectId]/issues/index.tsx | 94 ++++++++----------- .../[projectId]/modules/[moduleId].tsx | 35 +------ 11 files changed, 182 insertions(+), 211 deletions(-) diff --git a/apps/app/components/core/board-view/single-board.tsx b/apps/app/components/core/board-view/single-board.tsx index c38ddd5ea..9e8dc1b16 100644 --- a/apps/app/components/core/board-view/single-board.tsx +++ b/apps/app/components/core/board-view/single-board.tsx @@ -34,7 +34,7 @@ type Props = { }; export const SingleBoard: React.FC = ({ - type = "issue", + type, provided, snapshot, bgColor, @@ -117,6 +117,7 @@ export const SingleBoard: React.FC = ({ {...provided.dragHandleProps} > = ({ type, - typeId, issue, properties, snapshot, @@ -67,7 +58,7 @@ export const SingleBoardIssue: React.FC = ({ userAuth, }) => { const router = useRouter(); - const { workspaceSlug, projectId } = router.query; + const { workspaceSlug, projectId, cycleId, moduleId } = router.query; const { data: states } = useSWR( workspaceSlug && projectId ? STATE_LIST(projectId as string) : null, @@ -76,19 +67,12 @@ export const SingleBoardIssue: React.FC = ({ : null ); - const { data: projectDetails } = useSWR( - workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null, - workspaceSlug && projectId - ? () => projectService.getProject(workspaceSlug as string, projectId as string) - : null - ); - const partialUpdateIssue = (formData: Partial) => { if (!workspaceSlug || !projectId) return; - if (typeId) { + if (cycleId) mutate( - CYCLE_ISSUES(typeId ?? ""), + CYCLE_ISSUES(cycleId as string), (prevData) => { const updatedIssues = (prevData ?? []).map((p) => { if (p.issue_detail.id === issue.id) { @@ -107,8 +91,9 @@ export const SingleBoardIssue: React.FC = ({ false ); + if (moduleId) mutate( - MODULE_ISSUES(typeId ?? ""), + MODULE_ISSUES(moduleId as string), (prevData) => { const updatedIssues = (prevData ?? []).map((p) => { if (p.issue_detail.id === issue.id) { @@ -126,7 +111,6 @@ export const SingleBoardIssue: React.FC = ({ }, false ); - } mutate( PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string), @@ -143,10 +127,14 @@ export const SingleBoardIssue: React.FC = ({ issuesService .patchIssue(workspaceSlug as string, projectId as string, issue.id, formData) .then((res) => { - if (typeId) { - mutate(CYCLE_ISSUES(typeId ?? "")); - mutate(MODULE_ISSUES(typeId ?? "")); - } + mutate( + cycleId ? CYCLE_ISSUES(cycleId as string) : CYCLE_ISSUES(issue?.issue_cycle?.cycle ?? "") + ); + mutate( + moduleId + ? MODULE_ISSUES(moduleId as string) + : MODULE_ISSUES(issue?.issue_module?.module ?? "") + ); mutate(PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string)); }) @@ -179,7 +167,7 @@ export const SingleBoardIssue: React.FC = ({ {properties.key && (
- {projectDetails?.identifier}-{issue.sequence_id} + {issue.project_detail.identifier}-{issue.sequence_id}
)}
= ({ )} )} - {/* {properties.cycle && !typeId && ( -
- {issue.issue_cycle ? issue.issue_cycle.cycle_detail.name : "None"} -
- )} */} {properties.due_date && (
= ({ isOpen, setIsOpen }) => : null ); - const { data: projectDetails } = useSWR( - workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null, - workspaceSlug && projectId - ? () => projectService.getProject(workspaceSlug as string, projectId as string) - : null - ); - const { setToastAlert } = useToast(); const { @@ -213,7 +205,7 @@ export const BulkDeleteIssuesModal: React.FC = ({ isOpen, setIsOpen }) => }} /> - {projectDetails?.identifier}-{issue.sequence_id} + {issue.project_detail.identifier}-{issue.sequence_id} {issue.name}
diff --git a/apps/app/components/core/existing-issues-list-modal.tsx b/apps/app/components/core/existing-issues-list-modal.tsx index e03d3fdbd..f07025808 100644 --- a/apps/app/components/core/existing-issues-list-modal.tsx +++ b/apps/app/components/core/existing-issues-list-modal.tsx @@ -1,24 +1,17 @@ import React, { useState } from "react"; -import { useRouter } from "next/router"; - -import useSWR from "swr"; // react-hook-form import { Controller, SubmitHandler, useForm } from "react-hook-form"; // hooks import { Combobox, Dialog, Transition } from "@headlessui/react"; import { MagnifyingGlassIcon, RectangleStackIcon } from "@heroicons/react/24/outline"; import useToast from "hooks/use-toast"; -// services -import projectService from "services/project.service"; // headless ui // ui import { Button } from "components/ui"; import { LayerDiagonalIcon } from "components/icons"; // types import { IIssue } from "types"; -// fetch-keys -import { PROJECT_DETAILS } from "constants/fetch-keys"; type FormInput = { issues: string[]; @@ -41,16 +34,6 @@ export const ExistingIssuesListModal: React.FC = ({ }) => { const [query, setQuery] = useState(""); - const router = useRouter(); - const { workspaceSlug, projectId } = router.query; - - const { data: projectDetails } = useSWR( - workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null, - workspaceSlug && projectId - ? () => projectService.getProject(workspaceSlug as string, projectId as string) - : null - ); - const { setToastAlert } = useToast(); const handleClose = () => { @@ -175,7 +158,7 @@ export const ExistingIssuesListModal: React.FC = ({ }} /> - {projectDetails?.identifier}-{issue.sequence_id} + {issue.project_detail.identifier}-{issue.sequence_id} {issue.name} diff --git a/apps/app/components/core/issues-view.tsx b/apps/app/components/core/issues-view.tsx index ac236f0fc..263bf0538 100644 --- a/apps/app/components/core/issues-view.tsx +++ b/apps/app/components/core/issues-view.tsx @@ -10,10 +10,11 @@ import { DropResult } from "react-beautiful-dnd"; import issuesService from "services/issues.service"; import stateService from "services/state.service"; import projectService from "services/project.service"; +import modulesService from "services/modules.service"; // hooks import useIssueView from "hooks/use-issue-view"; // components -import { AllLists, AllBoards } from "components/core"; +import { AllLists, AllBoards, ExistingIssuesListModal } from "components/core"; import { CreateUpdateIssueModal, DeleteIssueModal } from "components/issues"; // types import { @@ -124,6 +125,7 @@ export const IssuesView: React.FC = ({ }); } else { const draggedItem = groupedByIssues[source.droppableId][source.index]; + console.log(draggedItem); if (source.droppableId !== destination.droppableId) { const sourceGroup = source.droppableId; // source group id const destinationGroup = destination.droppableId; // destination group id @@ -217,6 +219,7 @@ export const IssuesView: React.FC = ({ ? MODULE_ISSUES(moduleId as string) : MODULE_ISSUES(draggedItem.issue_module?.module ?? "") ); + mutate(PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string)); }); } else if (selectedGroup === "state_detail.name") { @@ -358,6 +361,54 @@ export const IssuesView: React.FC = ({ setIssueToDelete(issue); }; + const removeIssueFromCycle = (bridgeId: string) => { + if (!workspaceSlug || !projectId) return; + + mutate( + CYCLE_ISSUES(cycleId as string), + (prevData) => prevData?.filter((p) => p.id !== bridgeId), + false + ); + + issuesService + .removeIssueFromCycle( + workspaceSlug as string, + projectId as string, + cycleId as string, + bridgeId + ) + .then((res) => { + console.log(res); + }) + .catch((e) => { + console.log(e); + }); + }; + + const removeIssueFromModule = (bridgeId: string) => { + if (!workspaceSlug || !projectId) return; + + mutate( + MODULE_ISSUES(moduleId as string), + (prevData) => prevData?.filter((p) => p.id !== bridgeId), + false + ); + + modulesService + .removeIssueFromModule( + workspaceSlug as string, + projectId as string, + moduleId as string, + bridgeId + ) + .then((res) => { + console.log(res); + }) + .catch((e) => { + console.log(e); + }); + }; + return ( <> = ({ states={states} members={members} addIssueToState={addIssueToState} - openIssuesListModal={type !== "issue" ? openIssuesListModal : null} handleEditIssue={handleEditIssue} handleDeleteIssue={handleDeleteIssue} + openIssuesListModal={type !== "issue" ? openIssuesListModal : null} + removeIssue={ + type === "cycle" + ? removeIssueFromCycle + : type === "module" + ? removeIssueFromModule + : null + } userAuth={userAuth} /> ) : ( diff --git a/apps/app/components/core/list-view/all-lists.tsx b/apps/app/components/core/list-view/all-lists.tsx index 1f5ebcb2d..c2b6c498a 100644 --- a/apps/app/components/core/list-view/all-lists.tsx +++ b/apps/app/components/core/list-view/all-lists.tsx @@ -12,9 +12,10 @@ type Props = { states: IState[] | undefined; members: IProjectMember[] | undefined; addIssueToState: (groupTitle: string, stateId: string | null) => void; - openIssuesListModal?: (() => void) | null; handleEditIssue: (issue: IIssue) => void; handleDeleteIssue: (issue: IIssue) => void; + openIssuesListModal?: (() => void) | null; + removeIssue: ((bridgeId: string) => void) | null; userAuth: UserAuth; }; @@ -27,6 +28,7 @@ export const AllLists: React.FC = ({ openIssuesListModal, handleEditIssue, handleDeleteIssue, + removeIssue, userAuth, }) => { const { groupedByIssues, groupByProperty: selectedGroup } = useIssueView(issues); @@ -51,6 +53,7 @@ export const AllLists: React.FC = ({ handleEditIssue={handleEditIssue} handleDeleteIssue={handleDeleteIssue} openIssuesListModal={type !== "issue" ? openIssuesListModal : null} + removeIssue={removeIssue} userAuth={userAuth} /> ); diff --git a/apps/app/components/core/list-view/single-issue.tsx b/apps/app/components/core/list-view/single-issue.tsx index 1f2995889..dbadd84f7 100644 --- a/apps/app/components/core/list-view/single-issue.tsx +++ b/apps/app/components/core/list-view/single-issue.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React from "react"; import Link from "next/link"; import { useRouter } from "next/router"; @@ -11,8 +11,6 @@ import { Listbox, Transition } from "@headlessui/react"; import issuesService from "services/issues.service"; import workspaceService from "services/workspace.service"; import stateService from "services/state.service"; -// components -import { DeleteIssueModal } from "components/issues"; // ui import { CustomMenu, CustomSelect, AssigneesList, Avatar, CustomDatePicker } from "components/ui"; // helpers @@ -42,18 +40,16 @@ import { PRIORITIES } from "constants/"; type Props = { type?: string; - typeId?: string; issue: IIssue; properties: Properties; editIssue: () => void; - removeIssue?: () => void; + removeIssue?: (() => void) | null; handleDeleteIssue: (issue: IIssue) => void; userAuth: UserAuth; }; export const SingleListIssue: React.FC = ({ type, - typeId, issue, properties, editIssue, @@ -62,7 +58,7 @@ export const SingleListIssue: React.FC = ({ userAuth, }) => { const router = useRouter(); - const { workspaceSlug, projectId } = router.query; + const { workspaceSlug, projectId, cycleId, moduleId } = router.query; const { data: states } = useSWR( workspaceSlug && projectId ? STATE_LIST(projectId as string) : null, @@ -79,9 +75,9 @@ export const SingleListIssue: React.FC = ({ const partialUpdateIssue = (formData: Partial) => { if (!workspaceSlug || !projectId) return; - if (typeId) { + if (cycleId) mutate( - CYCLE_ISSUES(typeId ?? ""), + CYCLE_ISSUES(cycleId as string), (prevData) => { const updatedIssues = (prevData ?? []).map((p) => { if (p.issue_detail.id === issue.id) { @@ -100,8 +96,9 @@ export const SingleListIssue: React.FC = ({ false ); + if (moduleId) mutate( - MODULE_ISSUES(typeId ?? ""), + MODULE_ISSUES(moduleId as string), (prevData) => { const updatedIssues = (prevData ?? []).map((p) => { if (p.issue_detail.id === issue.id) { @@ -119,7 +116,6 @@ export const SingleListIssue: React.FC = ({ }, false ); - } mutate( PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string), @@ -136,10 +132,14 @@ export const SingleListIssue: React.FC = ({ issuesService .patchIssue(workspaceSlug as string, projectId as string, issue.id, formData) .then((res) => { - if (typeId) { - mutate(CYCLE_ISSUES(typeId ?? "")); - mutate(MODULE_ISSUES(typeId ?? "")); - } + mutate( + cycleId ? CYCLE_ISSUES(cycleId as string) : CYCLE_ISSUES(issue?.issue_cycle?.cycle ?? "") + ); + mutate( + moduleId + ? MODULE_ISSUES(moduleId as string) + : MODULE_ISSUES(issue?.issue_module?.module ?? "") + ); mutate(PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string)); }) @@ -154,7 +154,7 @@ export const SingleListIssue: React.FC = ({
= ({ ))} )} - {/* {properties.cycle && !typeId && ( -
- {issue.issue_cycle ? issue.issue_cycle.cycle_detail.name : "None"} -
- )} */} {properties.due_date && (
= ({ {type && !isNotAllowed && ( Edit - {type !== "issue" && ( + {type !== "issue" && removeIssue && ( <>Remove from {type} diff --git a/apps/app/components/core/list-view/single-list.tsx b/apps/app/components/core/list-view/single-list.tsx index 15299e1a5..690da8175 100644 --- a/apps/app/components/core/list-view/single-list.tsx +++ b/apps/app/components/core/list-view/single-list.tsx @@ -12,6 +12,7 @@ import { ChevronDownIcon, PlusIcon } from "@heroicons/react/24/outline"; import { addSpaceIfCamelCase } from "helpers/string.helper"; // types import { IIssue, IProjectMember, NestedKeyOf, UserAuth } from "types"; +import { CustomMenu } from "components/ui"; type Props = { type?: "issue" | "cycle" | "module"; @@ -25,11 +26,12 @@ type Props = { handleEditIssue: (issue: IIssue) => void; handleDeleteIssue: (issue: IIssue) => void; openIssuesListModal?: (() => void) | null; + removeIssue: ((bridgeId: string) => void) | null; userAuth: UserAuth; }; export const SingleList: React.FC = ({ - type = "issue", + type, groupTitle, groupedByIssues, selectedGroup, @@ -38,6 +40,7 @@ export const SingleList: React.FC = ({ handleEditIssue, handleDeleteIssue, openIssuesListModal, + removeIssue, userAuth, }) => { const router = useRouter(); @@ -105,11 +108,14 @@ export const SingleList: React.FC = ({ return ( handleEditIssue(issue)} handleDeleteIssue={handleDeleteIssue} + removeIssue={() => { + removeIssue && removeIssue(issue.bridge); + }} userAuth={userAuth} /> ); @@ -133,7 +139,25 @@ export const SingleList: React.FC = ({ Add issue - ) : null} + ) : ( + + + Add issue + + } + optionsPosition="left" + noBorder + > + Create new + {openIssuesListModal && ( + + Add an existing issue + + )} + + )}
)} diff --git a/apps/app/pages/[workspaceSlug]/projects/[projectId]/cycles/[cycleId].tsx b/apps/app/pages/[workspaceSlug]/projects/[projectId]/cycles/[cycleId].tsx index 935a35913..a3f8acb8f 100644 --- a/apps/app/pages/[workspaceSlug]/projects/[projectId]/cycles/[cycleId].tsx +++ b/apps/app/pages/[workspaceSlug]/projects/[projectId]/cycles/[cycleId].tsx @@ -132,30 +132,6 @@ const SingleCycle: React.FC = (props) => { }); }; - const removeIssueFromCycle = (bridgeId: string) => { - if (!workspaceSlug || !projectId) return; - - mutate( - CYCLE_ISSUES(cycleId as string), - (prevData) => prevData?.filter((p) => p.id !== bridgeId), - false - ); - - issuesServices - .removeIssueFromCycle( - workspaceSlug as string, - projectId as string, - cycleId as string, - bridgeId - ) - .then((res) => { - console.log(res); - }) - .catch((e) => { - console.log(e); - }); - }; - return ( = (props) => { {cycleIssuesArray ? ( cycleIssuesArray.length > 0 ? (
- {/* */} - + openIssuesListModal={openIssuesListModal} + />
) : (
= (props) => { - const [isOpen, setIsOpen] = useState(false); - const [selectedIssue, setSelectedIssue] = useState< - (IIssue & { actionType: "edit" | "delete" }) | undefined - >(undefined); - - const { - query: { workspaceSlug, projectId }, - } = useRouter(); + const router = useRouter(); + const { workspaceSlug, projectId } = router.query; const { data: projectIssues } = useSWR( workspaceSlug && projectId @@ -50,23 +44,8 @@ const ProjectIssues: NextPage = (props) => { : null ); - useEffect(() => { - if (!isOpen) { - const timer = setTimeout(() => { - setSelectedIssue(undefined); - clearTimeout(timer); - }, 500); - } - }, [isOpen]); - return ( - setIsOpen(false)} - data={selectedIssue} - /> @@ -92,35 +71,42 @@ const ProjectIssues: NextPage = (props) => {
} > - {!projectIssues ? ( + {projectIssues ? ( + projectIssues.count > 0 ? ( + p.parent === null) ?? []} + userAuth={props} + /> + ) : ( +
+ + + Use
C
shortcut to + create a new issue + + } + Icon={PlusIcon} + action={() => { + const e = new KeyboardEvent("keydown", { + key: "c", + }); + document.dispatchEvent(e); + }} + /> +
+
+ ) + ) : (
- ) : projectIssues.count > 0 ? ( - p.parent === null) ?? []} - userAuth={props} - /> - ) : ( -
- - - Use
C
shortcut to - create a new issue - - } - Icon={PlusIcon} - action={() => setIsOpen(true)} - /> -
-
)}
diff --git a/apps/app/pages/[workspaceSlug]/projects/[projectId]/modules/[moduleId].tsx b/apps/app/pages/[workspaceSlug]/projects/[projectId]/modules/[moduleId].tsx index 4861fc74b..a0f4d6246 100644 --- a/apps/app/pages/[workspaceSlug]/projects/[projectId]/modules/[moduleId].tsx +++ b/apps/app/pages/[workspaceSlug]/projects/[projectId]/modules/[moduleId].tsx @@ -135,30 +135,6 @@ const SingleModule: React.FC = (props) => { setModuleIssuesListModal(true); }; - const removeIssueFromModule = (issueId: string) => { - if (!workspaceSlug || !projectId) return; - - mutate( - MODULE_ISSUES(moduleId as string), - (prevData) => prevData?.filter((p) => p.id !== issueId), - false - ); - - modulesService - .removeIssueFromModule( - workspaceSlug as string, - projectId as string, - moduleId as string, - issueId - ) - .then((res) => { - console.log(res); - }) - .catch((e) => { - console.log(e); - }); - }; - const handleDeleteModule = () => { if (!moduleDetails) return; @@ -247,15 +223,12 @@ const SingleModule: React.FC = (props) => { {moduleIssuesArray ? ( moduleIssuesArray.length > 0 ? (
- {/* */} - + openIssuesListModal={openIssuesListModal} + />
) : (