forked from github/plane
ab3c3a6cf9
* dev: improve performance for cycle apis * dev: reduce module endpoints and create a new endpoint for getting issues by list * dev: remove unwanted fields from module * dev: update module endpoints * dev: optimize cycle endpoints * change module and cycle types * dev: module optimizations * dev: fix the issues check * dev: fix issues endpoint * dev: update module detail serializer * modify adding issues to modules and cycles * dev: update cycle issues * fix module links * dev: optimize issue list endpoint * fix: removing issues from the module when removing module_id from issue peekoverview * fix: updated the tooltip and ui for cycle select (#3718) * fix: updated the tooltip and ui for module select (#3716) --------- Co-authored-by: rahulramesha <rahulramesham@gmail.com> Co-authored-by: gurusainath <gurusainath007@gmail.com>
124 lines
3.8 KiB
TypeScript
124 lines
3.8 KiB
TypeScript
import set from "lodash/set";
|
|
import isEmpty from "lodash/isEmpty";
|
|
// store
|
|
import { action, makeObservable, observable, runInAction } from "mobx";
|
|
import { computedFn } from "mobx-utils";
|
|
// types
|
|
import { TIssue } from "@plane/types";
|
|
//services
|
|
import { IssueService } from "services/issue";
|
|
|
|
export type IIssueStore = {
|
|
// observables
|
|
issuesMap: Record<string, TIssue>; // Record defines issue_id as key and TIssue as value
|
|
// actions
|
|
getIssues(workspaceSlug: string, projectId: string, issueIds: string[]): Promise<TIssue[]>;
|
|
addIssue(issues: TIssue[], shouldReplace?: boolean): void;
|
|
updateIssue(issueId: string, issue: Partial<TIssue>): void;
|
|
removeIssue(issueId: string): void;
|
|
// helper methods
|
|
getIssueById(issueId: string): undefined | TIssue;
|
|
getIssuesByIds(issueIds: string[]): undefined | Record<string, TIssue>; // Record defines issue_id as key and TIssue as value
|
|
};
|
|
|
|
export class IssueStore implements IIssueStore {
|
|
// observables
|
|
issuesMap: { [issue_id: string]: TIssue } = {};
|
|
// service
|
|
issueService;
|
|
|
|
constructor() {
|
|
makeObservable(this, {
|
|
// observable
|
|
issuesMap: observable,
|
|
// actions
|
|
addIssue: action,
|
|
updateIssue: action,
|
|
removeIssue: action,
|
|
});
|
|
|
|
this.issueService = new IssueService();
|
|
}
|
|
|
|
// actions
|
|
/**
|
|
* @description This method will add issues to the issuesMap
|
|
* @param {TIssue[]} issues
|
|
* @returns {void}
|
|
*/
|
|
addIssue = (issues: TIssue[], shouldReplace = false) => {
|
|
if (issues && issues.length <= 0) return;
|
|
runInAction(() => {
|
|
issues.forEach((issue) => {
|
|
if (!this.issuesMap[issue.id] || shouldReplace) set(this.issuesMap, issue.id, issue);
|
|
});
|
|
});
|
|
};
|
|
|
|
getIssues = async (workspaceSlug: string, projectId: string, issueIds: string[]) => {
|
|
const issues = await this.issueService.retrieveIssues(workspaceSlug, projectId, issueIds);
|
|
|
|
runInAction(() => {
|
|
issues.forEach((issue) => {
|
|
if (!this.issuesMap[issue.id]) set(this.issuesMap, issue.id, issue);
|
|
});
|
|
});
|
|
|
|
return issues;
|
|
};
|
|
|
|
/**
|
|
* @description This method will update the issue in the issuesMap
|
|
* @param {string} issueId
|
|
* @param {Partial<TIssue>} issue
|
|
* @returns {void}
|
|
*/
|
|
updateIssue = (issueId: string, issue: Partial<TIssue>) => {
|
|
if (!issue || !issueId || isEmpty(this.issuesMap) || !this.issuesMap[issueId]) return;
|
|
runInAction(() => {
|
|
Object.keys(issue).forEach((key) => {
|
|
set(this.issuesMap, [issueId, key], issue[key as keyof TIssue]);
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* @description This method will remove the issue from the issuesMap
|
|
* @param {string} issueId
|
|
* @returns {void}
|
|
*/
|
|
removeIssue = (issueId: string) => {
|
|
if (!issueId || isEmpty(this.issuesMap) || !this.issuesMap[issueId]) return;
|
|
runInAction(() => {
|
|
delete this.issuesMap[issueId];
|
|
});
|
|
};
|
|
|
|
// helper methods
|
|
/**
|
|
* @description This method will return the issue from the issuesMap
|
|
* @param {string} issueId
|
|
* @returns {TIssue | undefined}
|
|
*/
|
|
getIssueById = computedFn((issueId: string) => {
|
|
if (!issueId || isEmpty(this.issuesMap) || !this.issuesMap[issueId]) return undefined;
|
|
return this.issuesMap[issueId];
|
|
});
|
|
|
|
/**
|
|
* @description This method will return the issues from the issuesMap
|
|
* @param {string[]} issueIds
|
|
* @returns {Record<string, TIssue> | undefined}
|
|
*/
|
|
getIssuesByIds = computedFn((issueIds: string[]) => {
|
|
if (!issueIds || issueIds.length <= 0 || isEmpty(this.issuesMap)) return undefined;
|
|
const filteredIssues: { [key: string]: TIssue } = {};
|
|
Object.values(this.issuesMap).forEach((issue) => {
|
|
if (issueIds.includes(issue.id)) {
|
|
filteredIssues[issue.id] = issue;
|
|
}
|
|
});
|
|
return isEmpty(filteredIssues) ? undefined : filteredIssues;
|
|
});
|
|
}
|