[WEB-1415] fix: issue attachment count mutation (#4567)

* fix: attachment count mutation

* fix: attachment count update logic
This commit is contained in:
Aaryan Khandelwal 2024-05-24 14:33:30 +05:30 committed by GitHub
parent 571d35cd8b
commit 9f573d4299
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 8 deletions

View File

@ -4,10 +4,11 @@ import set from "lodash/set";
import uniq from "lodash/uniq"; import uniq from "lodash/uniq";
import update from "lodash/update"; import update from "lodash/update";
import { action, computed, makeObservable, observable, runInAction } from "mobx"; import { action, computed, makeObservable, observable, runInAction } from "mobx";
// services
import { IssueAttachmentService } from "@/services/issue";
// types // types
import { TIssueAttachment, TIssueAttachmentMap, TIssueAttachmentIdMap } from "@plane/types"; import { TIssueAttachment, TIssueAttachmentMap, TIssueAttachmentIdMap } from "@plane/types";
// services
import { IssueAttachmentService } from "@/services/issue";
import { IIssueRootStore } from "../root.store";
import { IIssueDetail } from "./root.store"; import { IIssueDetail } from "./root.store";
export interface IIssueAttachmentStoreActions { export interface IIssueAttachmentStoreActions {
@ -43,11 +44,12 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
attachments: TIssueAttachmentIdMap = {}; attachments: TIssueAttachmentIdMap = {};
attachmentMap: TIssueAttachmentMap = {}; attachmentMap: TIssueAttachmentMap = {};
// root store // root store
rootIssueStore: IIssueRootStore;
rootIssueDetailStore: IIssueDetail; rootIssueDetailStore: IIssueDetail;
// services // services
issueAttachmentService; issueAttachmentService;
constructor(rootStore: IIssueDetail) { constructor(rootStore: IIssueRootStore) {
makeObservable(this, { makeObservable(this, {
// observables // observables
attachments: observable, attachments: observable,
@ -61,7 +63,8 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
removeAttachment: action, removeAttachment: action,
}); });
// root store // root store
this.rootIssueDetailStore = rootStore; this.rootIssueStore = rootStore;
this.rootIssueDetailStore = rootStore.issueDetail;
// services // services
this.issueAttachmentService = new IssueAttachmentService(); this.issueAttachmentService = new IssueAttachmentService();
} }
@ -87,9 +90,9 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
// actions // actions
addAttachments = (issueId: string, attachments: TIssueAttachment[]) => { addAttachments = (issueId: string, attachments: TIssueAttachment[]) => {
if (attachments && attachments.length > 0) { if (attachments && attachments.length > 0) {
const _attachmentIds = attachments.map((attachment) => attachment.id); const newAttachmentIds = attachments.map((attachment) => attachment.id);
runInAction(() => { runInAction(() => {
update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, _attachmentIds))); update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, newAttachmentIds)));
attachments.forEach((attachment) => set(this.attachmentMap, attachment.id, attachment)); attachments.forEach((attachment) => set(this.attachmentMap, attachment.id, attachment));
}); });
} }
@ -110,12 +113,17 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
createAttachment = async (workspaceSlug: string, projectId: string, issueId: string, data: FormData) => { createAttachment = async (workspaceSlug: string, projectId: string, issueId: string, data: FormData) => {
try { try {
const response = await this.issueAttachmentService.uploadIssueAttachment(workspaceSlug, projectId, issueId, data); const response = await this.issueAttachmentService.uploadIssueAttachment(workspaceSlug, projectId, issueId, data);
const issueAttachmentsCount = this.getAttachmentsByIssueId(issueId)?.length ?? 0;
if (response && response.id) if (response && response.id) {
runInAction(() => { runInAction(() => {
update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, [response.id]))); update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, [response.id])));
set(this.attachmentMap, response.id, response); set(this.attachmentMap, response.id, response);
this.rootIssueStore.issues.updateIssue(issueId, {
attachment_count: issueAttachmentsCount + 1, // increment attachment count
});
}); });
}
return response; return response;
} catch (error) { } catch (error) {
@ -131,6 +139,7 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
issueId, issueId,
attachmentId attachmentId
); );
const issueAttachmentsCount = this.getAttachmentsByIssueId(issueId)?.length ?? 1;
runInAction(() => { runInAction(() => {
update(this.attachments, [issueId], (attachmentIds = []) => { update(this.attachments, [issueId], (attachmentIds = []) => {
@ -138,6 +147,9 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
return attachmentIds; return attachmentIds;
}); });
delete this.attachmentMap[attachmentId]; delete this.attachmentMap[attachmentId];
this.rootIssueStore.issues.updateIssue(issueId, {
attachment_count: issueAttachmentsCount - 1, // decrement attachment count
});
}); });
return response; return response;

View File

@ -140,7 +140,7 @@ export class IssueDetail implements IIssueDetail {
this.rootIssueStore = rootStore; this.rootIssueStore = rootStore;
this.issue = new IssueStore(this); this.issue = new IssueStore(this);
this.reaction = new IssueReactionStore(this); this.reaction = new IssueReactionStore(this);
this.attachment = new IssueAttachmentStore(this); this.attachment = new IssueAttachmentStore(rootStore);
this.activity = new IssueActivityStore(this); this.activity = new IssueActivityStore(this);
this.comment = new IssueCommentStore(this); this.comment = new IssueCommentStore(this);
this.commentReaction = new IssueCommentReactionStore(this); this.commentReaction = new IssueCommentReactionStore(this);