2023-11-23 09:17:04 +00:00
|
|
|
import { action, observable, makeObservable, computed, runInAction, autorun } from "mobx";
|
|
|
|
// base class
|
|
|
|
import { IssueBaseStore } from "store/issues";
|
|
|
|
// services
|
|
|
|
import { IssueService } from "services/issue";
|
|
|
|
import { CycleService } from "services/cycle.service";
|
|
|
|
// types
|
2023-12-08 10:26:05 +00:00
|
|
|
import { TIssueGroupByOptions } from "types";
|
2023-11-23 09:17:04 +00:00
|
|
|
import { IIssue } from "types/issues";
|
2023-11-27 08:45:33 +00:00
|
|
|
import { IIssueResponse, TLoader, IGroupedIssues, ISubGroupedIssues, TUnGroupedIssues, ViewFlags } from "../../types";
|
2023-11-23 09:17:04 +00:00
|
|
|
import { RootStore } from "store/root";
|
|
|
|
|
|
|
|
export interface ICycleIssuesStore {
|
|
|
|
// observable
|
|
|
|
loader: TLoader;
|
|
|
|
issues: { [cycle_id: string]: IIssueResponse } | undefined;
|
|
|
|
// computed
|
|
|
|
getIssues: IIssueResponse | undefined;
|
|
|
|
getIssuesIds: IGroupedIssues | ISubGroupedIssues | TUnGroupedIssues | undefined;
|
|
|
|
// actions
|
|
|
|
fetchIssues: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
loadType: TLoader,
|
|
|
|
cycleId?: string | undefined
|
|
|
|
) => Promise<IIssueResponse | undefined>;
|
|
|
|
createIssue: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
data: Partial<IIssue>,
|
|
|
|
cycleId?: string | undefined
|
|
|
|
) => Promise<IIssue | undefined>;
|
|
|
|
updateIssue: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
data: Partial<IIssue>,
|
|
|
|
cycleId?: string | undefined
|
|
|
|
) => Promise<IIssue | undefined>;
|
|
|
|
removeIssue: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
cycleId?: string | undefined
|
|
|
|
) => Promise<IIssue | undefined>;
|
|
|
|
quickAddIssue: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
data: IIssue,
|
|
|
|
cycleId?: string | undefined
|
|
|
|
) => Promise<IIssue | undefined>;
|
2023-11-28 09:20:37 +00:00
|
|
|
addIssueToCycle: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
cycleId: string,
|
|
|
|
issueIds: string[],
|
2023-12-05 19:32:18 +00:00
|
|
|
fetchAfterAddition?: boolean,
|
|
|
|
projectId?: string
|
2023-11-28 09:20:37 +00:00
|
|
|
) => Promise<IIssue>;
|
2023-11-23 09:17:04 +00:00
|
|
|
removeIssueFromCycle: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
cycleId: string,
|
|
|
|
issueId: string,
|
|
|
|
issueBridgeId: string
|
|
|
|
) => Promise<IIssue>;
|
2023-12-04 14:32:14 +00:00
|
|
|
transferIssuesFromCycle: (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
cycleId: string,
|
|
|
|
payload: {
|
|
|
|
new_cycle_id: string;
|
|
|
|
}
|
|
|
|
) => Promise<IIssue>;
|
2023-11-27 08:45:33 +00:00
|
|
|
viewFlags: ViewFlags;
|
2023-11-23 09:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CycleIssuesStore extends IssueBaseStore implements ICycleIssuesStore {
|
|
|
|
loader: TLoader = "init-loader";
|
|
|
|
issues: { [cycle_id: string]: IIssueResponse } | undefined = undefined;
|
|
|
|
// root store
|
|
|
|
rootStore;
|
|
|
|
// service
|
|
|
|
cycleService;
|
|
|
|
issueService;
|
|
|
|
|
2023-11-27 08:45:33 +00:00
|
|
|
//viewData
|
|
|
|
viewFlags = {
|
|
|
|
enableQuickAdd: true,
|
|
|
|
enableIssueCreation: true,
|
|
|
|
enableInlineEditing: true,
|
|
|
|
};
|
|
|
|
|
2023-11-23 09:17:04 +00:00
|
|
|
constructor(_rootStore: RootStore) {
|
|
|
|
super(_rootStore);
|
|
|
|
|
|
|
|
makeObservable(this, {
|
|
|
|
// observable
|
|
|
|
loader: observable.ref,
|
|
|
|
issues: observable.ref,
|
|
|
|
// computed
|
|
|
|
getIssues: computed,
|
|
|
|
getIssuesIds: computed,
|
|
|
|
// action
|
|
|
|
fetchIssues: action,
|
|
|
|
createIssue: action,
|
|
|
|
updateIssue: action,
|
|
|
|
removeIssue: action,
|
|
|
|
quickAddIssue: action,
|
2023-11-27 08:45:33 +00:00
|
|
|
addIssueToCycle: action,
|
2023-11-23 09:17:04 +00:00
|
|
|
removeIssueFromCycle: action,
|
2023-12-04 14:32:14 +00:00
|
|
|
transferIssuesFromCycle: action,
|
2023-11-23 09:17:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.rootStore = _rootStore;
|
|
|
|
this.issueService = new IssueService();
|
|
|
|
this.cycleService = new CycleService();
|
|
|
|
|
|
|
|
autorun(() => {
|
|
|
|
const workspaceSlug = this.rootStore.workspace.workspaceSlug;
|
|
|
|
const projectId = this.rootStore.project.projectId;
|
|
|
|
const cycleId = this.rootStore.cycle.cycleId;
|
2023-12-16 19:05:53 +00:00
|
|
|
const hasPermissionToCurrentProject = this.rootStore.user.hasPermissionToCurrentProject;
|
|
|
|
if (!workspaceSlug || !projectId || !hasPermissionToCurrentProject || !cycleId) return;
|
2023-11-23 09:17:04 +00:00
|
|
|
|
|
|
|
const userFilters = this.rootStore?.cycleIssuesFilter?.issueFilters?.filters;
|
|
|
|
if (userFilters) this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get getIssues() {
|
|
|
|
const cycleId = this.rootStore?.cycle?.cycleId;
|
|
|
|
if (!cycleId || !this.issues || !this.issues[cycleId]) return undefined;
|
|
|
|
|
|
|
|
return this.issues[cycleId];
|
|
|
|
}
|
|
|
|
|
|
|
|
get getIssuesIds() {
|
|
|
|
const cycleId = this.rootStore?.cycle?.cycleId;
|
|
|
|
const displayFilters = this.rootStore?.cycleIssuesFilter?.issueFilters?.displayFilters;
|
|
|
|
|
|
|
|
const subGroupBy = displayFilters?.sub_group_by;
|
|
|
|
const groupBy = displayFilters?.group_by;
|
|
|
|
const orderBy = displayFilters?.order_by;
|
|
|
|
const layout = displayFilters?.layout;
|
|
|
|
|
|
|
|
if (!cycleId || !this.issues || !this.issues[cycleId]) return undefined;
|
|
|
|
|
|
|
|
let issues: IIssueResponse | IGroupedIssues | ISubGroupedIssues | TUnGroupedIssues | undefined = undefined;
|
|
|
|
|
|
|
|
if (layout === "list" && orderBy) {
|
|
|
|
if (groupBy) issues = this.groupedIssues(groupBy, orderBy, this.issues[cycleId]);
|
|
|
|
else issues = this.unGroupedIssues(orderBy, this.issues[cycleId]);
|
|
|
|
} else if (layout === "kanban" && groupBy && orderBy) {
|
|
|
|
if (subGroupBy) issues = this.subGroupedIssues(subGroupBy, groupBy, orderBy, this.issues[cycleId]);
|
|
|
|
else issues = this.groupedIssues(groupBy, orderBy, this.issues[cycleId]);
|
|
|
|
} else if (layout === "calendar")
|
|
|
|
issues = this.groupedIssues("target_date" as TIssueGroupByOptions, "target_date", this.issues[cycleId], true);
|
|
|
|
else if (layout === "spreadsheet") issues = this.unGroupedIssues(orderBy ?? "-created_at", this.issues[cycleId]);
|
|
|
|
else if (layout === "gantt_chart") issues = this.unGroupedIssues(orderBy ?? "sort_order", this.issues[cycleId]);
|
|
|
|
|
|
|
|
return issues;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchIssues = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
loadType: TLoader = "init-loader",
|
|
|
|
cycleId: string | undefined = undefined
|
|
|
|
) => {
|
|
|
|
if (!cycleId) return undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.loader = loadType;
|
|
|
|
|
|
|
|
const params = this.rootStore?.cycleIssuesFilter?.appliedFilters;
|
2023-11-29 14:56:43 +00:00
|
|
|
const response = await this.cycleService.getCycleIssuesWithParams(workspaceSlug, projectId, cycleId, params);
|
2023-11-23 09:17:04 +00:00
|
|
|
|
|
|
|
const _issues = { ...this.issues, [cycleId]: { ...response } };
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
this.loader = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
2023-11-27 08:45:33 +00:00
|
|
|
console.error(error);
|
2023-11-23 09:17:04 +00:00
|
|
|
this.loader = undefined;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
createIssue = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
data: Partial<IIssue>,
|
|
|
|
cycleId: string | undefined = undefined
|
|
|
|
) => {
|
|
|
|
if (!cycleId) return undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await this.rootStore.projectIssues.createIssue(workspaceSlug, projectId, data);
|
2023-11-28 09:20:37 +00:00
|
|
|
const issueToCycle = await this.addIssueToCycle(workspaceSlug, cycleId, [response.id], false);
|
|
|
|
|
|
|
|
let _issues = this.issues;
|
|
|
|
if (!_issues) _issues = {};
|
|
|
|
if (!_issues[cycleId]) _issues[cycleId] = {};
|
|
|
|
_issues[cycleId] = { ..._issues[cycleId], ...{ [response.id]: response } };
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
2023-11-23 09:17:04 +00:00
|
|
|
|
|
|
|
return issueToCycle;
|
|
|
|
} catch (error) {
|
|
|
|
this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateIssue = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
data: Partial<IIssue>,
|
|
|
|
cycleId: string | undefined = undefined
|
|
|
|
) => {
|
|
|
|
if (!cycleId) return undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let _issues = { ...this.issues };
|
|
|
|
if (!_issues) _issues = {};
|
|
|
|
if (!_issues[cycleId]) _issues[cycleId] = {};
|
|
|
|
_issues[cycleId][issueId] = { ..._issues[cycleId][issueId], ...data };
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.rootStore.projectIssues.updateIssue(workspaceSlug, projectId, issueId, data);
|
|
|
|
|
2023-12-18 09:29:25 +00:00
|
|
|
runInAction(() => {
|
|
|
|
_issues = { ...this.issues };
|
|
|
|
_issues[cycleId][issueId] = { ..._issues[cycleId][issueId], ...response };
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
|
|
|
|
2023-11-23 09:17:04 +00:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
removeIssue = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
issueId: string,
|
|
|
|
cycleId: string | undefined = undefined
|
|
|
|
) => {
|
|
|
|
if (!cycleId) return undefined;
|
|
|
|
try {
|
|
|
|
let _issues = { ...this.issues };
|
|
|
|
if (!_issues) _issues = {};
|
|
|
|
if (!_issues[cycleId]) _issues[cycleId] = {};
|
|
|
|
delete _issues?.[cycleId]?.[issueId];
|
2023-12-11 16:54:41 +00:00
|
|
|
_issues[cycleId] = { ..._issues[cycleId] };
|
2023-11-23 09:17:04 +00:00
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.rootStore.projectIssues.removeIssue(workspaceSlug, projectId, issueId);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
quickAddIssue = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
data: IIssue,
|
|
|
|
cycleId: string | undefined = undefined
|
|
|
|
) => {
|
|
|
|
if (!cycleId) return;
|
|
|
|
try {
|
|
|
|
let _issues = { ...this.issues };
|
|
|
|
if (!_issues) _issues = {};
|
|
|
|
if (!_issues[cycleId]) _issues[cycleId] = {};
|
|
|
|
_issues[cycleId] = { ..._issues[cycleId], ...{ [data.id as keyof IIssue]: data } };
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.createIssue(workspaceSlug, projectId, data, cycleId);
|
|
|
|
|
|
|
|
if (this.issues) {
|
|
|
|
delete this.issues[cycleId][data.id as keyof IIssue];
|
|
|
|
|
|
|
|
let _issues = { ...this.issues };
|
|
|
|
if (!_issues) _issues = {};
|
|
|
|
if (!_issues[cycleId]) _issues[cycleId] = {};
|
|
|
|
_issues[cycleId] = { ..._issues[cycleId], ...{ [response.id as keyof IIssue]: response } };
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-05 19:32:18 +00:00
|
|
|
addIssueToCycle = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
cycleId: string,
|
|
|
|
issueIds: string[],
|
|
|
|
fetchAfterAddition = true,
|
|
|
|
projectId?: string
|
|
|
|
) => {
|
2023-12-08 10:26:05 +00:00
|
|
|
const activeProjectId = this.rootStore.project.projectId;
|
|
|
|
if (!activeProjectId && !projectId) return;
|
2023-12-05 19:32:18 +00:00
|
|
|
|
2023-12-08 10:26:05 +00:00
|
|
|
const projectIdToUpdate: string = projectId || activeProjectId || "";
|
2023-11-28 09:20:37 +00:00
|
|
|
|
2023-11-27 08:45:33 +00:00
|
|
|
try {
|
2023-12-05 19:32:18 +00:00
|
|
|
const issueToCycle = await this.issueService.addIssueToCycle(workspaceSlug, projectIdToUpdate, cycleId, {
|
2023-11-28 09:20:37 +00:00
|
|
|
issues: issueIds,
|
2023-11-27 08:45:33 +00:00
|
|
|
});
|
|
|
|
|
2023-12-05 19:32:18 +00:00
|
|
|
if (fetchAfterAddition) this.fetchIssues(workspaceSlug, projectIdToUpdate, "mutation", cycleId);
|
2023-11-27 08:45:33 +00:00
|
|
|
|
|
|
|
return issueToCycle;
|
|
|
|
} catch (error) {
|
2023-12-05 19:32:18 +00:00
|
|
|
this.fetchIssues(workspaceSlug, projectIdToUpdate, "mutation", cycleId);
|
2023-11-27 08:45:33 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-23 09:17:04 +00:00
|
|
|
removeIssueFromCycle = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
cycleId: string,
|
|
|
|
issueId: string,
|
|
|
|
issueBridgeId: string
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
let _issues = { ...this.issues };
|
|
|
|
if (!_issues) _issues = {};
|
|
|
|
if (!_issues[cycleId]) _issues[cycleId] = {};
|
|
|
|
delete _issues?.[cycleId]?.[issueId];
|
|
|
|
|
|
|
|
runInAction(() => {
|
|
|
|
this.issues = _issues;
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = await this.issueService.removeIssueFromCycle(workspaceSlug, projectId, cycleId, issueBridgeId);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
2023-12-04 14:32:14 +00:00
|
|
|
|
|
|
|
transferIssuesFromCycle = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
cycleId: string,
|
|
|
|
payload: {
|
|
|
|
new_cycle_id: string;
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const response = await this.cycleService.transferIssues(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
cycleId as string,
|
|
|
|
payload
|
|
|
|
);
|
|
|
|
await this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
this.fetchIssues(workspaceSlug, projectId, "mutation", cycleId);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
2023-11-23 09:17:04 +00:00
|
|
|
}
|