plane/web/store/issue/issue-details/reaction.store.ts
Anmol Singh Bhatia efd3ebf067 chore: bug fixes and improvement (#3303)
* refactor: updated preloaded function for the list view quick add

* fix: resolved bug in the assignee dropdown

* chore: issue sidebar link improvement

* fix: resolved subscription store bug

* chore: updated preloaded function for the kanban layout quick add

* chore: resolved issues in the list filters and component

* chore: filter store updated

* fix: issue serializer changed

* chore: quick add preload function updated

* fix: build error

* fix: serializer changed

* fix: minor request change

* chore: resolved build issues and updated the prepopulated data in the quick add issue.

* fix: build fix and code refactor

* fix: spreadsheet layout quick add fix

* fix: issue peek overview link section updated

* fix: cycle status bug fix

* fix: serializer changes

* fix: assignee and labels listing

* chore: issue modal parent_id default value updated

* fix: cycle and module issue serializer change

* fix: cycle list serializer changed

* chore: prepopulated validation in both list and kanban for quick add and group header add issues

* chore: group header validation added

* fix: issue response payload change

* dev: make cycle and module issue create response simillar

* chore: custom control link component added

* dev: make issue create and update response simillar to list and retrieve

* fix: build error

* chore: control link component improvement

* chore: globalise issue peek overview

* chore: control link component improvement

* chore: made changes and optimised the issue peek overview root

* build-error: resolved build erros for issueId dependancy from issue detail store

* chore: peek overview link fix

* dev: update state nullable rule

---------

Co-authored-by: gurusainath <gurusainath007@gmail.com>
Co-authored-by: NarayanBavisetti <narayan3119@gmail.com>
Co-authored-by: pablohashescobar <nikhilschacko@gmail.com>
2024-01-22 13:19:43 +05:30

124 lines
3.9 KiB
TypeScript

import { action, computed, makeObservable, observable, runInAction } from "mobx";
import set from "lodash/set";
// services
import { IssueReactionService } from "services/issue";
// types
import { IIssueDetail } from "./root.store";
import { TIssueReaction, TIssueReactionMap, TIssueReactionIdMap } from "@plane/types";
export interface IIssueReactionStoreActions {
// actions
fetchReactions: (workspaceSlug: string, projectId: string, issueId: string) => Promise<TIssueReaction[]>;
createReaction: (workspaceSlug: string, projectId: string, issueId: string, reaction: string) => Promise<any>;
removeReaction: (workspaceSlug: string, projectId: string, issueId: string, reaction: string) => Promise<any>;
}
export interface IIssueReactionStore extends IIssueReactionStoreActions {
// observables
reactions: TIssueReactionIdMap;
reactionMap: TIssueReactionMap;
// computed
issueReactions: string[] | undefined;
// helper methods
getReactionsByIssueId: (issueId: string) => string[] | undefined;
getReactionById: (reactionId: string) => TIssueReaction | undefined;
}
export class IssueReactionStore implements IIssueReactionStore {
// observables
reactions: TIssueReactionIdMap = {};
reactionMap: TIssueReactionMap = {};
// root store
rootIssueDetailStore: IIssueDetail;
// services
issueReactionService;
constructor(rootStore: IIssueDetail) {
makeObservable(this, {
// observables
reactions: observable,
reactionMap: observable,
// computed
issueReactions: computed,
// actions
fetchReactions: action,
createReaction: action,
removeReaction: action,
});
// root store
this.rootIssueDetailStore = rootStore;
// services
this.issueReactionService = new IssueReactionService();
}
// computed
get issueReactions() {
const issueId = this.rootIssueDetailStore.peekIssue?.issueId;
if (!issueId) return undefined;
return this.reactions[issueId] ?? undefined;
}
// helper methods
getReactionsByIssueId = (issueId: string) => {
if (!issueId) return undefined;
return this.reactions[issueId] ?? undefined;
};
getReactionById = (reactionId: string) => {
if (!reactionId) return undefined;
return this.reactionMap[reactionId] ?? undefined;
};
// actions
fetchReactions = async (workspaceSlug: string, projectId: string, issueId: string) => {
try {
const response = await this.issueReactionService.listIssueReactions(workspaceSlug, projectId, issueId);
runInAction(() => {
this.reactions[issueId] = response.map((reaction) => reaction.id);
response.forEach((reaction) => set(this.reactionMap, reaction.id, reaction));
});
return response;
} catch (error) {
throw error;
}
};
createReaction = async (workspaceSlug: string, projectId: string, issueId: string, reaction: string) => {
try {
const response = await this.issueReactionService.createIssueReaction(workspaceSlug, projectId, issueId, {
reaction,
});
runInAction(() => {
this.reactions[issueId].push(response.id);
set(this.reactionMap, response.id, response);
});
return response;
} catch (error) {
throw error;
}
};
removeReaction = async (workspaceSlug: string, projectId: string, issueId: string, reaction: string) => {
try {
const reactionIndex = this.reactions[issueId].findIndex((_reaction) => _reaction === reaction);
if (reactionIndex >= 0)
runInAction(() => {
this.reactions[issueId].splice(reactionIndex, 1);
delete this.reactionMap[reaction];
});
const response = await this.issueReactionService.deleteIssueReaction(workspaceSlug, projectId, issueId, reaction);
return response;
} catch (error) {
// TODO: Replace with fetch issue details
// this.fetchReactions(workspaceSlug, projectId, issueId);
throw error;
}
};
}