plane/web/store/issue/issue-details/relation.store.ts
Bavisetti Narayan c9337d4a41 feat: dashboard widgets (#3362)
* fix: created dashboard, widgets and dashboard widget model

* fix: new user home dashboard

* chore: recent projects list

* chore: recent collaborators

* chore: priority order change

* chore: payload changes

* chore: collaborator's active issue count

* chore: all dashboard widgets added with services and typs

* chore: centered metric for pie chart

* chore: widget filters

* chore: created issue filter

* fix: created and assigned issues payload change

* chore: created issue payload change

* fix: date filter change

* chore: implement filters

* fix: added expansion fields

* fix: changed issue structure with relation

* chore: new issues response

* fix: project member fix

* chore: updated issue_relation structure

* chore: code cleanup

* chore: update issues response and added empty states

* fix: button text wrap

* chore: update empty state messages

* fix: filters

* chore: update dark mode empty states

* build-error: Type check in the issue relation service

* fix: issues redirection

* fix: project empty state

* chore: project member active check

* chore: project member check in state and priority

* chore: remove console logs and replace harcoded values with constants

* fix: code refactoring

* fix: key name changed

* refactor: mapping through similar components using an array

* fix: build errors

---------

Co-authored-by: Aaryan Khandelwal <aaryankhandu123@gmail.com>
Co-authored-by: gurusainath <gurusainath007@gmail.com>
2024-01-22 13:22:09 +05:30

159 lines
4.8 KiB
TypeScript

import { action, computed, makeObservable, observable, runInAction } from "mobx";
import set from "lodash/set";
// services
import { IssueRelationService } from "services/issue";
// types
import { IIssueDetail } from "./root.store";
import { TIssueRelationIdMap, TIssueRelationMap, TIssueRelationTypes, TIssueRelation, TIssue } from "@plane/types";
export interface IIssueRelationStoreActions {
// actions
fetchRelations: (workspaceSlug: string, projectId: string, issueId: string) => Promise<TIssueRelation>;
createRelation: (
workspaceSlug: string,
projectId: string,
issueId: string,
relationType: TIssueRelationTypes,
issues: string[]
) => Promise<TIssue[]>;
removeRelation: (
workspaceSlug: string,
projectId: string,
issueId: string,
relationType: TIssueRelationTypes,
related_issue: string
) => Promise<any>;
}
export interface IIssueRelationStore extends IIssueRelationStoreActions {
// observables
relationMap: TIssueRelationMap; // Record defines relationType as key and reactions as value
// computed
issueRelations: TIssueRelationIdMap | undefined;
// helper methods
getRelationsByIssueId: (issueId: string) => TIssueRelationIdMap | undefined;
getRelationByIssueIdRelationType: (issueId: string, relationType: TIssueRelationTypes) => string[] | undefined;
}
export class IssueRelationStore implements IIssueRelationStore {
// observables
relationMap: TIssueRelationMap = {};
// root store
rootIssueDetailStore: IIssueDetail;
// services
issueRelationService;
constructor(rootStore: IIssueDetail) {
makeObservable(this, {
// observables
relationMap: observable,
// computed
issueRelations: computed,
// actions
fetchRelations: action,
createRelation: action,
removeRelation: action,
});
// root store
this.rootIssueDetailStore = rootStore;
// services
this.issueRelationService = new IssueRelationService();
}
// computed
get issueRelations() {
const issueId = this.rootIssueDetailStore.peekIssue?.issueId;
if (!issueId) return undefined;
return this.relationMap?.[issueId] ?? undefined;
}
// // helper methods
getRelationsByIssueId = (issueId: string) => {
if (!issueId) return undefined;
return this.relationMap?.[issueId] ?? undefined;
};
getRelationByIssueIdRelationType = (issueId: string, relationType: TIssueRelationTypes) => {
if (!issueId || !relationType) return undefined;
return this.relationMap?.[issueId]?.[relationType] ?? undefined;
};
// actions
fetchRelations = async (workspaceSlug: string, projectId: string, issueId: string) => {
try {
const response = await this.issueRelationService.listIssueRelations(workspaceSlug, projectId, issueId);
runInAction(() => {
Object.keys(response).forEach((key) => {
const relation_key = key as TIssueRelationTypes;
const relation_issues = response[relation_key];
const issues = relation_issues.flat().map((issue) => issue);
if (issues && issues.length > 0) this.rootIssueDetailStore.rootIssueStore.issues.addIssue(issues);
set(
this.relationMap,
[issueId, relation_key],
issues && issues.length > 0 ? issues.map((issue) => issue.id) : []
);
});
});
return response;
} catch (error) {
throw error;
}
};
createRelation = async (
workspaceSlug: string,
projectId: string,
issueId: string,
relationType: TIssueRelationTypes,
issues: string[]
) => {
try {
const response = await this.issueRelationService.createIssueRelations(workspaceSlug, projectId, issueId, {
relation_type: relationType,
issues,
});
if (response && response.length > 0)
runInAction(() => {
response.forEach((issue) => {
this.rootIssueDetailStore.rootIssueStore.issues.addIssue([issue]);
this.relationMap[issueId][relationType].push(issue.id);
});
});
return response;
} catch (error) {
throw error;
}
};
removeRelation = async (
workspaceSlug: string,
projectId: string,
issueId: string,
relationType: TIssueRelationTypes,
related_issue: string
) => {
try {
const relationIndex = this.relationMap[issueId][relationType].findIndex((_issueId) => _issueId === related_issue);
if (relationIndex >= 0)
runInAction(() => {
this.relationMap[issueId][relationType].splice(relationIndex, 1);
});
const response = await this.issueRelationService.deleteIssueRelation(workspaceSlug, projectId, issueId, {
relation_type: relationType,
related_issue,
});
return response;
} catch (error) {
this.fetchRelations(workspaceSlug, projectId, issueId);
throw error;
}
};
}