From 24b82e518c6c801cafa3ed34b66bab8d8d602be7 Mon Sep 17 00:00:00 2001 From: dakshesh14 Date: Mon, 6 Nov 2023 16:57:59 +0530 Subject: [PATCH] fix: update/delete flow --- web/components/issues/draft-issue-modal.tsx | 20 +++++++- web/store/draft-issues/issue.store.ts | 52 ++------------------- 2 files changed, 23 insertions(+), 49 deletions(-) diff --git a/web/components/issues/draft-issue-modal.tsx b/web/components/issues/draft-issue-modal.tsx index be3cb1f1c..43b0ef74f 100644 --- a/web/components/issues/draft-issue-modal.tsx +++ b/web/components/issues/draft-issue-modal.tsx @@ -13,6 +13,8 @@ import useToast from "hooks/use-toast"; import useLocalStorage from "hooks/use-local-storage"; // components import { DraftIssueForm } from "components/issues"; +// constants +import { ISSUE_PRIORITIES, ISSUE_STATE_GROUPS, getValueFromObject } from "constants/issue"; // types import type { IIssue } from "types"; // fetch-keys @@ -59,6 +61,7 @@ export const CreateUpdateDraftIssueModal: React.FC = observer( const { project: projectStore, draftIssues: draftIssueStore, + draftIssueFilters: draftIssueFilterStore, issueDetail: issueDetailStore, issue: issueStore, user: userStore, @@ -165,8 +168,21 @@ export const CreateUpdateDraftIssueModal: React.FC = observer( .then((response) => { if (!createMore) onClose(); - // replace with actual group id and sub group id - draftIssueStore.updateIssueStructure(null, null, response); + const userDisplayFilters = draftIssueFilterStore?.userDisplayFilters; + const groupBy = userDisplayFilters?.group_by || null; + + let groupById: null | string = null; + + if (groupBy === "priority") { + groupById = getValueFromObject(ISSUE_PRIORITIES, "key") as string; + } else if (groupBy === "labels") { + groupById = getValueFromObject(projectStore?.projectLabels ?? [], "id") as string; + } else if (groupBy === "state_detail.group") { + groupById = getValueFromObject(ISSUE_STATE_GROUPS, "key") as string; + } + + draftIssueStore.updateIssueStructure(groupById, null, response); + draftIssueStore.fetchIssues(workspaceSlug.toString(), activeProject); if (!payload.is_draft) { if (payload.cycle && payload.cycle !== "") addIssueToCycle(response.id, payload.cycle); diff --git a/web/store/draft-issues/issue.store.ts b/web/store/draft-issues/issue.store.ts index 3286bc272..d50b42ff1 100644 --- a/web/store/draft-issues/issue.store.ts +++ b/web/store/draft-issues/issue.store.ts @@ -91,8 +91,6 @@ export class IssueDraftStore implements IIssueDraftStore { } get getIssueType() { - // TODO: fix this such that we only have the - // conditions for layouts that are actually allowed in draft issues const groupedLayouts = ["kanban", "list", "calendar"]; const ungroupedLayouts = ["spreadsheet", "gantt_chart"]; @@ -187,6 +185,7 @@ export class IssueDraftStore implements IIssueDraftStore { updateIssueStructure = async (group_id: string | null, sub_group_id: string | null, issue: IIssue) => { const projectId: string | null = issue?.project; const issueType = this.getIssueType; + if (!projectId || !issueType) return null; let issues: IIssueGroupedStructure | IIssueGroupWithSubGroupsStructure | IIssueUnGroupedStructure | null = @@ -289,64 +288,23 @@ export class IssueDraftStore implements IIssueDraftStore { } }; - convertDraftIssueToIssue = async (workspaceSlug: string, projectId: string, issueId: string) => - // TODO: add removing item from draft issue list + convertDraftIssueToIssue = async (workspaceSlug: string, projectId: string, issueId: string) => { await this.updateDraftIssue(workspaceSlug, projectId, { id: issueId, is_draft: false }); + await this.fetchIssues(workspaceSlug, projectId); + }; deleteDraftIssue = async (workspaceSlug: string, projectId: string, issueId: string) => { const originalIssues = { ...this.draftIssues }; - const issueType = this.getIssueType; - runInAction(() => { this.loader = true; this.error = null; }); - // FIXME: use real group_id and sub_group_id from filters - const group_id = "1"; - const sub_group_id = "1"; - - if (issueType) { - let issues = originalIssues?.[projectId]?.[issueType] || null; - if (!issues) return null; - - if (issueType === "grouped") { - issues = issues as IIssueGroupedStructure; - issues = { - ...issues, - [group_id]: issues[group_id].filter((i) => i?.id !== issueId), - }; - } - - if (issueType === "groupWithSubGroups") { - issues = issues as IIssueGroupWithSubGroupsStructure; - issues = { - ...issues, - [sub_group_id]: { - ...issues[sub_group_id], - [group_id]: issues[sub_group_id][group_id].filter((i) => i?.id !== issueId), - }, - }; - } - - if (issueType === "ungrouped") { - issues = issues as IIssueUnGroupedStructure; - issues = issues.filter((i) => i?.id !== issueId); - } - - // optimistic removing draft issue - runInAction(() => { - this.draftIssues = { - ...this.draftIssues, - [projectId]: { ...this.draftIssues[projectId], [issueType]: issues }, - }; - }); - } - try { // deleting using api await this.draftIssueService.deleteDraftIssue(workspaceSlug, projectId, issueId); + await this.fetchIssues(workspaceSlug, projectId); runInAction(() => { this.loader = false;