@@ -29,14 +29,13 @@ interface IPreviewImageCollection {
29
29
30
30
export class ListItemAttachments extends React . Component < IListItemAttachmentsProps , IListItemAttachmentsState > {
31
31
private _spservice : SPservice ;
32
- private previewImages : IPreviewImageCollection ;
32
+ private previewImages : IPreviewImageCollection = { } ;
33
33
private _utilities : utilities ;
34
34
35
35
constructor ( props : IListItemAttachmentsProps ) {
36
36
super ( props ) ;
37
37
38
38
telemetry . track ( 'ReactListItemAttachments' , { } ) ;
39
-
40
39
this . state = {
41
40
file : null ,
42
41
hideDialog : true ,
@@ -45,7 +44,9 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
45
44
deleteAttachment : false ,
46
45
disableButton : false ,
47
46
showPlaceHolder : false ,
48
- fireUpload : false
47
+ fireUpload : false ,
48
+ filesToUpload : [ ] ,
49
+ itemId : props . itemId
49
50
} ;
50
51
51
52
// Get SPService Factory
@@ -73,26 +74,43 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
73
74
} ) ;
74
75
}
75
76
76
- /**
77
- * Load Item Attachments
78
- */
79
- private async loadAttachments ( ) {
80
- this . _spservice . getListItemAttachments ( this . props . listId , this . props . itemId ) . then ( ( files : IListItemAttachmentFile [ ] ) => {
81
- const filePreviewImages = files . map ( file => this . loadAttachmentPreview ( file ) ) ;
82
- return Promise . all ( filePreviewImages ) . then ( filePreviews => {
83
- this . previewImages = { } ;
84
- filePreviews . forEach ( preview => {
85
- this . previewImages [ preview . name ] = preview ;
86
- } ) ;
77
+ public async uploadAttachments ( itemId : number ) {
78
+ if ( this . state . filesToUpload ) {
79
+ await Promise . all ( this . state . filesToUpload . map ( file => this . _spservice . addAttachment (
80
+ this . props . listId ,
81
+ itemId ,
82
+ file . name ,
83
+ file ,
84
+ this . props . webUrl ) ) ) ;
85
+ }
86
+ this . setState ( {
87
+ filesToUpload : [ ] ,
88
+ itemId : itemId
89
+ } , this . loadAttachments ) ;
90
+ }
91
+ protected loadAttachmentsPreview ( files : IListItemAttachmentFile [ ] ) {
92
+ const filePreviewImages = files . map ( file => this . loadAttachmentPreview ( file ) ) ;
93
+ return Promise . all ( filePreviewImages ) . then ( filePreviews => {
94
+ filePreviews . forEach ( preview => {
95
+ this . previewImages [ preview . name ] = preview ;
96
+ } ) ;
87
97
88
- this . setState ( {
98
+ this . setState ( {
89
99
fireUpload : false ,
90
100
hideDialog : true ,
91
101
dialogMessage : '' ,
92
102
attachments : files ,
93
103
showPlaceHolder : files . length === 0 ? true : false
94
104
} ) ;
95
- } ) ;
105
+ } ) ;
106
+ }
107
+ /**
108
+ * Load Item Attachments
109
+ */
110
+ private async loadAttachments ( ) {
111
+ if ( this . state . itemId ) {
112
+ this . _spservice . getListItemAttachments ( this . props . listId , this . state . itemId ) . then ( async ( files : IListItemAttachmentFile [ ] ) => {
113
+ await this . loadAttachmentsPreview ( files ) ;
96
114
} ) . catch ( ( error : Error ) => {
97
115
this . setState ( {
98
116
fireUpload : false ,
@@ -101,6 +119,22 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
101
119
} ) ;
102
120
} ) ;
103
121
}
122
+ else if ( this . state . filesToUpload && this . state . filesToUpload . length > 0 ) {
123
+ let files = this . state . filesToUpload . map ( file => ( {
124
+ FileName : file . name ,
125
+ ServerRelativeUrl : undefined
126
+ } ) ) ;
127
+ await this . loadAttachmentsPreview ( files ) ;
128
+ }
129
+ else {
130
+ this . setState ( {
131
+ fireUpload : false ,
132
+ hideDialog : true ,
133
+ dialogMessage : '' ,
134
+ showPlaceHolder : true
135
+ } ) ;
136
+ }
137
+ }
104
138
105
139
/**
106
140
* Close the dialog
@@ -120,8 +154,15 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
120
154
/**
121
155
* Attachment uploaded event handler
122
156
*/
123
- private _onAttachmentUpload = ( ) => {
157
+ private _onAttachmentUpload = ( file : File ) => {
124
158
// load Attachments
159
+ if ( ! this . state . itemId ) {
160
+ let files = this . state . filesToUpload || [ ] ;
161
+ files . push ( file ) ;
162
+ this . setState ( {
163
+ filesToUpload : [ ...files ]
164
+ } ) ;
165
+ }
125
166
this . loadAttachments ( ) ;
126
167
}
127
168
@@ -153,8 +194,26 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
153
194
} ) ;
154
195
155
196
try {
156
- await this . _spservice . deleteAttachment ( file . FileName , this . props . listId , this . props . itemId , this . props . webUrl ) ;
197
+ if ( this . state . itemId ) {
198
+ await this . _spservice . deleteAttachment ( file . FileName , this . props . listId , this . state . itemId , this . props . webUrl ) ;
199
+ }
200
+ else {
201
+ let filesToUpload = this . state . filesToUpload ;
202
+ let fileToRemove = filesToUpload . find ( f => f . name === file . FileName ) ;
203
+ if ( fileToRemove ) {
204
+ filesToUpload . splice ( filesToUpload . indexOf ( fileToRemove ) , 1 ) ;
205
+ }
206
+ let attachments = this . state . attachments ;
207
+ let attachmentToRemove = attachments . find ( attachment => attachment . FileName === file . FileName ) ;
208
+ if ( attachmentToRemove ) {
209
+ attachments . splice ( attachments . indexOf ( attachmentToRemove ) , 1 ) ;
157
210
211
+ }
212
+ this . setState ( {
213
+ filesToUpload : [ ...filesToUpload ] ,
214
+ attachments : [ ...attachments ]
215
+ } ) ;
216
+ }
158
217
this . setState ( {
159
218
fireUpload : false ,
160
219
hideDialog : false ,
@@ -183,7 +242,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
183
242
< div className = { styles . ListItemAttachments } >
184
243
< UploadAttachment
185
244
listId = { this . props . listId }
186
- itemId = { this . props . itemId }
245
+ itemId = { this . state . itemId }
187
246
disabled = { this . props . disabled }
188
247
context = { this . props . context }
189
248
onAttachmentUpload = { this . _onAttachmentUpload }
0 commit comments