@@ -4,10 +4,11 @@ import set from "lodash/set";
4
4
import uniq from "lodash/uniq" ;
5
5
import update from "lodash/update" ;
6
6
import { action , computed , makeObservable , observable , runInAction } from "mobx" ;
7
- // services
8
- import { IssueAttachmentService } from "@/services/issue" ;
9
7
// types
10
8
import { TIssueAttachment , TIssueAttachmentMap , TIssueAttachmentIdMap } from "@plane/types" ;
9
+ // services
10
+ import { IssueAttachmentService } from "@/services/issue" ;
11
+ import { IIssueRootStore } from "../root.store" ;
11
12
import { IIssueDetail } from "./root.store" ;
12
13
13
14
export interface IIssueAttachmentStoreActions {
@@ -43,11 +44,12 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
43
44
attachments : TIssueAttachmentIdMap = { } ;
44
45
attachmentMap : TIssueAttachmentMap = { } ;
45
46
// root store
47
+ rootIssueStore : IIssueRootStore ;
46
48
rootIssueDetailStore : IIssueDetail ;
47
49
// services
48
50
issueAttachmentService ;
49
51
50
- constructor ( rootStore : IIssueDetail ) {
52
+ constructor ( rootStore : IIssueRootStore ) {
51
53
makeObservable ( this , {
52
54
// observables
53
55
attachments : observable ,
@@ -61,7 +63,8 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
61
63
removeAttachment : action ,
62
64
} ) ;
63
65
// root store
64
- this . rootIssueDetailStore = rootStore ;
66
+ this . rootIssueStore = rootStore ;
67
+ this . rootIssueDetailStore = rootStore . issueDetail ;
65
68
// services
66
69
this . issueAttachmentService = new IssueAttachmentService ( ) ;
67
70
}
@@ -87,9 +90,9 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
87
90
// actions
88
91
addAttachments = ( issueId : string , attachments : TIssueAttachment [ ] ) => {
89
92
if ( attachments && attachments . length > 0 ) {
90
- const _attachmentIds = attachments . map ( ( attachment ) => attachment . id ) ;
93
+ const newAttachmentIds = attachments . map ( ( attachment ) => attachment . id ) ;
91
94
runInAction ( ( ) => {
92
- update ( this . attachments , [ issueId ] , ( attachmentIds = [ ] ) => uniq ( concat ( attachmentIds , _attachmentIds ) ) ) ;
95
+ update ( this . attachments , [ issueId ] , ( attachmentIds = [ ] ) => uniq ( concat ( attachmentIds , newAttachmentIds ) ) ) ;
93
96
attachments . forEach ( ( attachment ) => set ( this . attachmentMap , attachment . id , attachment ) ) ;
94
97
} ) ;
95
98
}
@@ -110,12 +113,17 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
110
113
createAttachment = async ( workspaceSlug : string , projectId : string , issueId : string , data : FormData ) => {
111
114
try {
112
115
const response = await this . issueAttachmentService . uploadIssueAttachment ( workspaceSlug , projectId , issueId , data ) ;
116
+ const issueAttachmentsCount = this . getAttachmentsByIssueId ( issueId ) ?. length ?? 0 ;
113
117
114
- if ( response && response . id )
118
+ if ( response && response . id ) {
115
119
runInAction ( ( ) => {
116
120
update ( this . attachments , [ issueId ] , ( attachmentIds = [ ] ) => uniq ( concat ( attachmentIds , [ response . id ] ) ) ) ;
117
121
set ( this . attachmentMap , response . id , response ) ;
122
+ this . rootIssueStore . issues . updateIssue ( issueId , {
123
+ attachment_count : issueAttachmentsCount + 1 , // increment attachment count
124
+ } ) ;
118
125
} ) ;
126
+ }
119
127
120
128
return response ;
121
129
} catch ( error ) {
@@ -131,13 +139,17 @@ export class IssueAttachmentStore implements IIssueAttachmentStore {
131
139
issueId ,
132
140
attachmentId
133
141
) ;
142
+ const issueAttachmentsCount = this . getAttachmentsByIssueId ( issueId ) ?. length ?? 1 ;
134
143
135
144
runInAction ( ( ) => {
136
145
update ( this . attachments , [ issueId ] , ( attachmentIds = [ ] ) => {
137
146
if ( attachmentIds . includes ( attachmentId ) ) pull ( attachmentIds , attachmentId ) ;
138
147
return attachmentIds ;
139
148
} ) ;
140
149
delete this . attachmentMap [ attachmentId ] ;
150
+ this . rootIssueStore . issues . updateIssue ( issueId , {
151
+ attachment_count : issueAttachmentsCount - 1 , // decrement attachment count
152
+ } ) ;
141
153
} ) ;
142
154
143
155
return response ;
0 commit comments