plane/web/store/issue/issue-details/attachment.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

134 lines
4.0 KiB
TypeScript

import { action, computed, makeObservable, observable, runInAction } from "mobx";
import set from "lodash/set";
// services
import { IssueAttachmentService } from "services/issue";
// types
import { IIssueDetail } from "./root.store";
import { TIssueAttachment, TIssueAttachmentMap, TIssueAttachmentIdMap } from "@plane/types";
export interface IIssueAttachmentStoreActions {
fetchAttachments: (workspaceSlug: string, projectId: string, issueId: string) => Promise<TIssueAttachment[]>;
createAttachment: (
workspaceSlug: string,
projectId: string,
issueId: string,
data: FormData
) => Promise<TIssueAttachment>;
removeAttachment: (
workspaceSlug: string,
projectId: string,
issueId: string,
attachmentId: string
) => Promise<TIssueAttachment>;
}
export interface IIssueAttachmentStore extends IIssueAttachmentStoreActions {
// observables
attachments: TIssueAttachmentIdMap;
attachmentMap: TIssueAttachmentMap;
// computed
issueAttachments: string[] | undefined;
// helper methods
getAttachmentsByIssueId: (issueId: string) => string[] | undefined;
getAttachmentById: (attachmentId: string) => TIssueAttachment | undefined;
}
export class IssueAttachmentStore implements IIssueAttachmentStore {
// observables
attachments: TIssueAttachmentIdMap = {};
attachmentMap: TIssueAttachmentMap = {};
// root store
rootIssueDetailStore: IIssueDetail;
// services
issueAttachmentService;
constructor(rootStore: IIssueDetail) {
makeObservable(this, {
// observables
attachments: observable,
attachmentMap: observable,
// computed
issueAttachments: computed,
// actions
fetchAttachments: action,
createAttachment: action,
removeAttachment: action,
});
// root store
this.rootIssueDetailStore = rootStore;
// services
this.issueAttachmentService = new IssueAttachmentService();
}
// computed
get issueAttachments() {
const issueId = this.rootIssueDetailStore.peekIssue?.issueId;
if (!issueId) return undefined;
return this.attachments[issueId] ?? undefined;
}
// helper methods
getAttachmentsByIssueId = (issueId: string) => {
if (!issueId) return undefined;
return this.attachments[issueId] ?? undefined;
};
getAttachmentById = (attachmentId: string) => {
if (!attachmentId) return undefined;
return this.attachmentMap[attachmentId] ?? undefined;
};
// actions
fetchAttachments = async (workspaceSlug: string, projectId: string, issueId: string) => {
try {
const response = await this.issueAttachmentService.getIssueAttachment(workspaceSlug, projectId, issueId);
runInAction(() => {
this.attachments[issueId] = response.map((attachment) => attachment.id);
response.forEach((attachment) => set(this.attachmentMap, attachment.id, attachment));
});
return response;
} catch (error) {
throw error;
}
};
createAttachment = async (workspaceSlug: string, projectId: string, issueId: string, data: FormData) => {
try {
const response = await this.issueAttachmentService.uploadIssueAttachment(workspaceSlug, projectId, issueId, data);
runInAction(() => {
this.attachments[issueId].push(response.id);
set(this.attachmentMap, response.id, response);
});
return response;
} catch (error) {
throw error;
}
};
removeAttachment = async (workspaceSlug: string, projectId: string, issueId: string, attachmentId: string) => {
try {
const response = await this.issueAttachmentService.deleteIssueAttachment(
workspaceSlug,
projectId,
issueId,
attachmentId
);
const reactionIndex = this.attachments[issueId].findIndex((_comment) => _comment === attachmentId);
if (reactionIndex >= 0)
runInAction(() => {
this.attachments[issueId].splice(reactionIndex, 1);
delete this.attachmentMap[attachmentId];
});
return response;
} catch (error) {
throw error;
}
};
}