fix: update/delete flow

This commit is contained in:
dakshesh14 2023-11-06 16:57:59 +05:30
parent f9586ede31
commit 24b82e518c
2 changed files with 23 additions and 49 deletions

View File

@ -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<IssuesModalProps> = observer(
const {
project: projectStore,
draftIssues: draftIssueStore,
draftIssueFilters: draftIssueFilterStore,
issueDetail: issueDetailStore,
issue: issueStore,
user: userStore,
@ -165,8 +168,21 @@ export const CreateUpdateDraftIssueModal: React.FC<IssuesModalProps> = 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);

View File

@ -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;