From df836d55d5ed6c70cb5841f44eaf5033513da208 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal Date: Fri, 24 Feb 2023 15:50:15 +0530 Subject: [PATCH] chore: new link endpoints --- apps/app/components/core/link-modal.tsx | 2 +- .../components/core/sidebar/links-list.tsx | 1 + apps/app/components/issues/modal.tsx | 2 +- apps/app/components/issues/sidebar.tsx | 29 ++++---- apps/app/components/modules/sidebar.tsx | 72 +++++++++++-------- apps/app/services/issues.service.ts | 35 +++++++++ apps/app/services/modules.service.ts | 35 +++++++++ apps/app/types/issues.d.ts | 1 + apps/app/types/modules.d.ts | 1 + 9 files changed, 134 insertions(+), 44 deletions(-) diff --git a/apps/app/components/core/link-modal.tsx b/apps/app/components/core/link-modal.tsx index 5700946f0..03741963a 100644 --- a/apps/app/components/core/link-modal.tsx +++ b/apps/app/components/core/link-modal.tsx @@ -16,7 +16,7 @@ import type { IIssueLink, ModuleLink } from "types"; type Props = { isOpen: boolean; handleClose: () => void; - onFormSubmit: (formData: IIssueLink | ModuleLink) => void; + onFormSubmit: (formData: IIssueLink | ModuleLink) => Promise; }; const defaultValues: ModuleLink = { diff --git a/apps/app/components/core/sidebar/links-list.tsx b/apps/app/components/core/sidebar/links-list.tsx index 8553ee43c..008b5ea14 100644 --- a/apps/app/components/core/sidebar/links-list.tsx +++ b/apps/app/components/core/sidebar/links-list.tsx @@ -14,6 +14,7 @@ type Props = { created_at: Date; created_by: string; created_by_detail: IUserLite; + metadata: any; title: string; url: string; }[]; diff --git a/apps/app/components/issues/modal.tsx b/apps/app/components/issues/modal.tsx index 28689e6cd..5a09a0578 100644 --- a/apps/app/components/issues/modal.tsx +++ b/apps/app/components/issues/modal.tsx @@ -86,7 +86,7 @@ export const CreateUpdateIssueModal: React.FC = ({ return () => { window.removeEventListener("keydown", handleKeyDown); }; - }, []); + }, [handleClose]); const addIssueToCycle = async (issueId: string, cycleId: string) => { if (!workspaceSlug || !projectId) return; diff --git a/apps/app/components/issues/sidebar.tsx b/apps/app/components/issues/sidebar.tsx index 0829d4226..249927f25 100644 --- a/apps/app/components/issues/sidebar.tsx +++ b/apps/app/components/issues/sidebar.tsx @@ -77,6 +77,17 @@ export const IssueDetailsSidebar: React.FC = ({ const { setToastAlert } = useToast(); + console.log("isseu details: ", issueDetail); + + // const { data: issueLinks } = useSWR( + // workspaceSlug && projectId + // ? PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string) + // : null, + // workspaceSlug && projectId + // ? () => issuesService.getIssues(workspaceSlug as string, projectId as string) + // : null + // ); + const { data: issues } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string) @@ -149,16 +160,12 @@ export const IssueDetailsSidebar: React.FC = ({ const handleCreateLink = async (formData: IIssueLink) => { if (!workspaceSlug || !projectId || !issueDetail) return; - const previousLinks = issueDetail?.issue_link.map((l) => ({ title: l.title, url: l.url })); - - const payload: Partial = { - links_list: [...(previousLinks ?? []), formData], - }; + const payload = { metadata: {}, ...formData }; await issuesService - .patchIssue(workspaceSlug as string, projectId as string, issueDetail.id, payload) + .createIssueLink(workspaceSlug as string, projectId as string, issueDetail.id, payload) .then((res) => { - mutate(ISSUE_DETAILS(issueDetail.id as string)); + mutate(ISSUE_DETAILS(issueDetail.id)); }) .catch((err) => { console.log(err); @@ -171,17 +178,15 @@ export const IssueDetailsSidebar: React.FC = ({ const updatedLinks = issueDetail.issue_link.filter((l) => l.id !== linkId); mutate( - ISSUE_DETAILS(issueDetail.id as string), + ISSUE_DETAILS(issueDetail.id), (prevData) => ({ ...(prevData as IIssue), issue_link: updatedLinks }), false ); await issuesService - .patchIssue(workspaceSlug as string, projectId as string, issueDetail.id, { - links_list: updatedLinks, - }) + .deleteIssueLink(workspaceSlug as string, projectId as string, issueDetail.id, linkId) .then((res) => { - mutate(ISSUE_DETAILS(issueDetail.id as string)); + mutate(ISSUE_DETAILS(issueDetail.id)); }) .catch((err) => { console.log(err); diff --git a/apps/app/components/modules/sidebar.tsx b/apps/app/components/modules/sidebar.tsx index 74d7c9483..151526a50 100644 --- a/apps/app/components/modules/sidebar.tsx +++ b/apps/app/components/modules/sidebar.tsx @@ -71,6 +71,8 @@ export const ModuleDetailsSidebar: React.FC = ({ const [startDateRange, setStartDateRange] = useState(new Date()); const [endDateRange, setEndDateRange] = useState(null); + console.log("module details: ", module); + const router = useRouter(); const { workspaceSlug, projectId, moduleId } = router.query; @@ -115,14 +117,10 @@ export const ModuleDetailsSidebar: React.FC = ({ const handleCreateLink = async (formData: ModuleLink) => { if (!workspaceSlug || !projectId || !moduleId) return; - const previousLinks = module?.link_module.map((l) => ({ title: l.title, url: l.url })); - - const payload: Partial = { - links_list: [...(previousLinks ?? []), formData], - }; + const payload = { metadata: {}, ...formData }; await modulesService - .patchModule(workspaceSlug as string, projectId as string, moduleId as string, payload) + .createModuleLink(workspaceSlug as string, projectId as string, moduleId as string, payload) .then((res) => { mutate(MODULE_DETAILS(moduleId as string)); }) @@ -135,11 +133,25 @@ export const ModuleDetailsSidebar: React.FC = ({ }); }; - const handleDeleteLink = (linkId: string) => { - if (!module) return; + const handleDeleteLink = async (linkId: string) => { + if (!workspaceSlug || !projectId || !module) return; const updatedLinks = module.link_module.filter((l) => l.id !== linkId); - submitChanges({ links_list: updatedLinks }); + + mutate( + MODULE_DETAILS(module.id), + (prevData) => ({ ...(prevData as IModule), link_module: updatedLinks }), + false + ); + + await modulesService + .deleteModuleLink(workspaceSlug as string, projectId as string, module.id, linkId) + .then((res) => { + mutate(MODULE_DETAILS(module.id)); + }) + .catch((err) => { + console.log(err); + }); }; useEffect(() => { @@ -350,27 +362,6 @@ export const ModuleDetailsSidebar: React.FC = ({ -
-
-

Links

- -
-
- {module.link_module && module.link_module.length > 0 ? ( - - ) : null} -
-
{isStartValid && isEndValid ? ( @@ -388,6 +379,27 @@ export const ModuleDetailsSidebar: React.FC = ({ "" )}
+
+
+

Links

+ +
+
+ {module.link_module && module.link_module.length > 0 ? ( + + ) : null} +
+
) : ( diff --git a/apps/app/services/issues.service.ts b/apps/app/services/issues.service.ts index 703c3141c..036cf36db 100644 --- a/apps/app/services/issues.service.ts +++ b/apps/app/services/issues.service.ts @@ -293,6 +293,41 @@ class ProjectIssuesServices extends APIService { throw error?.response?.data; }); } + + async createIssueLink( + workspaceSlug: string, + projectId: string, + issueId: string, + data: { + metadata: any; + title: string; + url: string; + } + ): Promise { + return this.post( + `/api/workspaces/${workspaceSlug}/projects/${projectId}/issues/${issueId}/issue-links/`, + data + ) + .then((response) => response?.data) + .catch((error) => { + throw error?.response?.data; + }); + } + + async deleteIssueLink( + workspaceSlug: string, + projectId: string, + issueId: string, + linkId: string + ): Promise { + return this.delete( + `/api/workspaces/${workspaceSlug}/projects/${projectId}/issues/${issueId}/issue-links/${linkId}/` + ) + .then((response) => response?.data) + .catch((error) => { + throw error?.response?.data; + }); + } } export default new ProjectIssuesServices(); diff --git a/apps/app/services/modules.service.ts b/apps/app/services/modules.service.ts index cbf473253..3c728b8a7 100644 --- a/apps/app/services/modules.service.ts +++ b/apps/app/services/modules.service.ts @@ -116,6 +116,41 @@ class ProjectIssuesServices extends APIService { throw error?.response?.data; }); } + + async createModuleLink( + workspaceSlug: string, + projectId: string, + moduleId: string, + data: { + metadata: any; + title: string; + url: string; + } + ): Promise { + return this.post( + `/api/workspaces/${workspaceSlug}/projects/${projectId}/modules/${moduleId}/module-links/`, + data + ) + .then((response) => response?.data) + .catch((error) => { + throw error?.response?.data; + }); + } + + async deleteModuleLink( + workspaceSlug: string, + projectId: string, + moduleId: string, + linkId: string + ): Promise { + return this.delete( + `/api/workspaces/${workspaceSlug}/projects/${projectId}/modules/${moduleId}/module-links/${linkId}/` + ) + .then((response) => response?.data) + .catch((error) => { + throw error?.response?.data; + }); + } } export default new ProjectIssuesServices(); diff --git a/apps/app/types/issues.d.ts b/apps/app/types/issues.d.ts index 289f29a09..091f47200 100644 --- a/apps/app/types/issues.d.ts +++ b/apps/app/types/issues.d.ts @@ -82,6 +82,7 @@ export interface IIssue { created_by: string; created_by_detail: IUserLite; id: string; + metadata: any; title: string; url: string; }[]; diff --git a/apps/app/types/modules.d.ts b/apps/app/types/modules.d.ts index 0defcdeca..c3de50f72 100644 --- a/apps/app/types/modules.d.ts +++ b/apps/app/types/modules.d.ts @@ -14,6 +14,7 @@ export interface IModule { created_by: string; created_by_detail: IUserLite; id: string; + metadata: any; title: string; url: string; }[];