Skip to content

Commit 9f573d4

Browse files
authored
[WEB-1415] fix: issue attachment count mutation (#4567)
* fix: attachment count mutation * fix: attachment count update logic
1 parent 571d35c commit 9f573d4

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

web/store/issue/issue-details/attachment.store.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import set from "lodash/set";
44
import uniq from "lodash/uniq";
55
import update from "lodash/update";
66
import { action, computed, makeObservable, observable, runInAction } from "mobx";
7-
// services
8-
import { IssueAttachmentService } from "@/services/issue";
97
// types
108
import { TIssueAttachment, TIssueAttachmentMap, TIssueAttachmentIdMap } from "@plane/types";
9+
// services
10+
import { IssueAttachmentService } from "@/services/issue";
11+
import { IIssueRootStore } from "../root.store";
1112
import { IIssueDetail } from "./root.store";
1213

1314
export interface IIssueAttachmentStoreActions {
@@ -43,11 +44,12 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
4344
attachments: TIssueAttachmentIdMap = {};
4445
attachmentMap: TIssueAttachmentMap = {};
4546
// root store
47+
rootIssueStore: IIssueRootStore;
4648
rootIssueDetailStore: IIssueDetail;
4749
// services
4850
issueAttachmentService;
4951

50-
constructor(rootStore: IIssueDetail) {
52+
constructor(rootStore: IIssueRootStore) {
5153
makeObservable(this, {
5254
// observables
5355
attachments: observable,
@@ -61,7 +63,8 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
6163
removeAttachment: action,
6264
});
6365
// root store
64-
this.rootIssueDetailStore = rootStore;
66+
this.rootIssueStore = rootStore;
67+
this.rootIssueDetailStore = rootStore.issueDetail;
6568
// services
6669
this.issueAttachmentService = new IssueAttachmentService();
6770
}
@@ -87,9 +90,9 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
8790
// actions
8891
addAttachments = (issueId: string, attachments: TIssueAttachment[]) => {
8992
if (attachments && attachments.length > 0) {
90-
const _attachmentIds = attachments.map((attachment) => attachment.id);
93+
const newAttachmentIds = attachments.map((attachment) => attachment.id);
9194
runInAction(() => {
92-
update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, _attachmentIds)));
95+
update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, newAttachmentIds)));
9396
attachments.forEach((attachment) => set(this.attachmentMap, attachment.id, attachment));
9497
});
9598
}
@@ -110,12 +113,17 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
110113
createAttachment = async (workspaceSlug: string, projectId: string, issueId: string, data: FormData) => {
111114
try {
112115
const response = await this.issueAttachmentService.uploadIssueAttachment(workspaceSlug, projectId, issueId, data);
116+
const issueAttachmentsCount = this.getAttachmentsByIssueId(issueId)?.length ?? 0;
113117

114-
if (response && response.id)
118+
if (response && response.id) {
115119
runInAction(() => {
116120
update(this.attachments, [issueId], (attachmentIds = []) => uniq(concat(attachmentIds, [response.id])));
117121
set(this.attachmentMap, response.id, response);
122+
this.rootIssueStore.issues.updateIssue(issueId, {
123+
attachment_count: issueAttachmentsCount + 1, // increment attachment count
124+
});
118125
});
126+
}
119127

120128
return response;
121129
} catch (error) {
@@ -131,13 +139,17 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
131139
issueId,
132140
attachmentId
133141
);
142+
const issueAttachmentsCount = this.getAttachmentsByIssueId(issueId)?.length ?? 1;
134143

135144
runInAction(() => {
136145
update(this.attachments, [issueId], (attachmentIds = []) => {
137146
if (attachmentIds.includes(attachmentId)) pull(attachmentIds, attachmentId);
138147
return attachmentIds;
139148
});
140149
delete this.attachmentMap[attachmentId];
150+
this.rootIssueStore.issues.updateIssue(issueId, {
151+
attachment_count: issueAttachmentsCount - 1, // decrement attachment count
152+
});
141153
});
142154

143155
return response;

web/store/issue/issue-details/root.store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class IssueDetail implements IIssueDetail {
140140
this.rootIssueStore = rootStore;
141141
this.issue = new IssueStore(this);
142142
this.reaction = new IssueReactionStore(this);
143-
this.attachment = new IssueAttachmentStore(this);
143+
this.attachment = new IssueAttachmentStore(rootStore);
144144
this.activity = new IssueActivityStore(this);
145145
this.comment = new IssueCommentStore(this);
146146
this.commentReaction = new IssueCommentReactionStore(this);

0 commit comments

Comments
 (0)