From fb3f7841959dfcef487519c4311fa2a4a04b1d9c Mon Sep 17 00:00:00 2001 From: Anmol Singh Bhatia <121005188+anmolsinghbhatia@users.noreply.github.com> Date: Thu, 13 Feb 2025 23:40:11 +0530 Subject: [PATCH] [WEB-3362] fix: create sub work item operation (#2491) * fix: create sub work item operation * chore: epic sub work item modal operation updated --- .../main/overview-section/modals-root.tsx | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/web/ee/components/epics/details/main/overview-section/modals-root.tsx b/web/ee/components/epics/details/main/overview-section/modals-root.tsx index 577e34aade..9fa2abea68 100644 --- a/web/ee/components/epics/details/main/overview-section/modals-root.tsx +++ b/web/ee/components/epics/details/main/overview-section/modals-root.tsx @@ -33,26 +33,50 @@ export const EpicOverviewWidgetModals: FC = observer((props) => { createRelation, issueCrudOperationState, setIssueCrudOperationState, - createSubIssues, + createSubIssues: createEpicSubIssues, } = useIssueDetail(EIssueServiceType.EPICS); + const { createSubIssues } = useIssueDetail(); const { fetchEpicAnalytics } = useIssueTypes(); + const handleAddSubIssueResponse = (successMessage: string) => { + setToast({ + type: TOAST_TYPE.SUCCESS, + title: "Success!", + message: successMessage, + }); + }; + + const handleAddSubIssueError = () => { + setToast({ + type: TOAST_TYPE.ERROR, + title: "Error!", + message: "Error adding work item", + }); + }; + + const addSubIssueToEpic = async ( + workspaceSlug: string, + projectId: string, + parentIssueId: string, + issueIds: string[] + ) => { + try { + await createEpicSubIssues(workspaceSlug, projectId, parentIssueId, issueIds).then(() => { + fetchEpicAnalytics(workspaceSlug, projectId, epicId); + handleAddSubIssueResponse("Work items added successfully"); + }); + } catch (error) { + handleAddSubIssueError(); + } + }; + const addSubIssue = async (workspaceSlug: string, projectId: string, parentIssueId: string, issueIds: string[]) => { try { await createSubIssues(workspaceSlug, projectId, parentIssueId, issueIds).then(() => { - fetchEpicAnalytics(workspaceSlug, projectId, epicId); - setToast({ - type: TOAST_TYPE.SUCCESS, - title: "Success!", - message: "Work items added successfully", - }); + handleAddSubIssueResponse("Work items added successfully"); }); } catch (error) { - setToast({ - type: TOAST_TYPE.ERROR, - title: "Error!", - message: "Error adding work item", - }); + handleAddSubIssueError(); } }; @@ -79,7 +103,7 @@ export const EpicOverviewWidgetModals: FC = observer((props) => { }; const handleExistingIssuesModalOnSubmit = async (_issue: ISearchIssueResponse[]) => - await addSubIssue( + await addSubIssueToEpic( workspaceSlug, projectId, epicId, @@ -94,9 +118,13 @@ export const EpicOverviewWidgetModals: FC = observer((props) => { const handleCreateUpdateModalOnSubmit = async (_issue: TIssue) => { if (_issue.parent_id) { - await addSubIssue(workspaceSlug, projectId, epicId, [_issue.id]).then(() => { - fetchEpicAnalytics(workspaceSlug, projectId, epicId); - }); + if (_issue.parent_id === epicId) { + await addSubIssueToEpic(workspaceSlug, projectId, epicId, [_issue.id]).then(() => { + fetchEpicAnalytics(workspaceSlug, projectId, epicId); + }); + } else { + await addSubIssue(workspaceSlug, projectId, _issue.parent_id, [_issue.id]); + } } };