@@ -22,9 +22,13 @@ import { Spinner, SpinnerSize } from 'office-ui-fabric-react/lib/Spinner';
22
22
import utilities from './utilities' ;
23
23
import { Placeholder } from "../placeholder" ;
24
24
25
+ interface IPreviewImageCollection {
26
+ [ fileName : string ] : IDocumentCardPreviewImage ;
27
+ }
28
+
25
29
export class ListItemAttachments extends React . Component < IListItemAttachmentsProps , IListItemAttachmentsState > {
26
30
private _spservice : SPservice ;
27
- private previewImages : IDocumentCardPreviewImage [ ] ;
31
+ private previewImages : IPreviewImageCollection ;
28
32
private _utilities : utilities ;
29
33
30
34
constructor ( props : IListItemAttachmentsProps ) {
@@ -53,45 +57,54 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
53
57
this . loadAttachments ( ) ;
54
58
}
55
59
60
+ private async loadAttachmentPreview ( file : IListItemAttachmentFile ) : Promise < IDocumentCardPreviewImage > {
61
+ return this . _utilities . GetFileImageUrl ( file ) . then ( previewImageUrl => {
62
+ return {
63
+ name : file . FileName ,
64
+ previewImageSrc : previewImageUrl ,
65
+ iconSrc : '' ,
66
+ imageFit : ImageFit . center ,
67
+ width : 187 ,
68
+ height : 130 ,
69
+ } ;
70
+ } ) ;
71
+ }
72
+
56
73
/**
57
74
* Load Item Attachments
58
75
*/
59
76
private async loadAttachments ( ) {
60
- this . previewImages = [ ] ;
61
- try {
62
- const files : IListItemAttachmentFile [ ] = await this . _spservice . getListItemAttachments ( this . props . listId , this . props . itemId ) ;
63
-
64
- for ( const file of files ) {
65
- const _previewImage = await this . _utilities . GetFileImageUrl ( file ) ;
66
- this . previewImages . push ( {
67
- name : file . FileName ,
68
- previewImageSrc : _previewImage ,
69
- iconSrc : '' ,
70
- imageFit : ImageFit . center ,
71
- width : 187 ,
72
- height : 130 ,
77
+ this . _spservice . getListItemAttachments ( this . props . listId , this . props . itemId ) . then ( ( files : IListItemAttachmentFile [ ] ) => {
78
+ const filePreviewImages = files . map ( file => this . loadAttachmentPreview ( file ) ) ;
79
+ return Promise . all ( filePreviewImages ) . then ( filePreviews => {
80
+ this . previewImages = { } ;
81
+ filePreviews . forEach ( preview => {
82
+ this . previewImages [ preview . name ] = preview ;
73
83
} ) ;
74
- }
75
84
76
- this . setState ( {
77
- hideDialog : true ,
78
- dialogMessage : '' ,
79
- attachments : files ,
80
- showPlaceHolder : files . length === 0 ? true : false
85
+ this . setState ( {
86
+ fireUpload : false ,
87
+ hideDialog : true ,
88
+ dialogMessage : '' ,
89
+ attachments : files ,
90
+ showPlaceHolder : files . length === 0 ? true : false
91
+ } ) ;
81
92
} ) ;
82
- } catch ( error ) {
93
+ } ) . catch ( ( error : Error ) => {
83
94
this . setState ( {
84
- hideDialog : true ,
95
+ fireUpload : false ,
96
+ hideDialog : false ,
85
97
dialogMessage : strings . ListItemAttachmentserrorLoadAttachments . replace ( '{0}' , error . message )
86
98
} ) ;
87
- }
99
+ } ) ;
88
100
}
89
101
90
102
/**
91
103
* Close the dialog
92
104
*/
93
105
private _closeDialog = ( ) => {
94
106
this . setState ( {
107
+ fireUpload : false ,
95
108
hideDialog : true ,
96
109
dialogMessage : '' ,
97
110
file : null ,
@@ -116,6 +129,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
116
129
*/
117
130
private onDeleteAttachment = ( file : IListItemAttachmentFile ) => {
118
131
this . setState ( {
132
+ fireUpload : false ,
119
133
hideDialog : false ,
120
134
deleteAttachment : true ,
121
135
file : file ,
@@ -131,13 +145,15 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
131
145
const { file } = this . state ;
132
146
133
147
this . setState ( {
148
+ fireUpload : false ,
134
149
disableButton : true ,
135
150
} ) ;
136
151
137
152
try {
138
153
await this . _spservice . deleteAttachment ( file . FileName , this . props . listId , this . props . itemId , this . props . webUrl ) ;
139
154
140
155
this . setState ( {
156
+ fireUpload : false ,
141
157
hideDialog : false ,
142
158
deleteAttachment : false ,
143
159
disableButton : false ,
@@ -146,6 +162,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
146
162
} ) ;
147
163
} catch ( error ) {
148
164
this . setState ( {
165
+ fireUpload : false ,
149
166
hideDialog : false ,
150
167
file : null ,
151
168
deleteAttachment : false ,
@@ -179,22 +196,22 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
179
196
onConfigure = { ( ) => this . setState ( { fireUpload : true } ) } />
180
197
:
181
198
182
- this . state . attachments . map ( ( file , i : number ) => {
199
+ this . state . attachments . map ( file => {
200
+ const fileName = file . FileName ;
201
+ const previewImage = this . previewImages [ fileName ] ;
183
202
return (
184
- < div className = { styles . documentCardWrapper } >
203
+ < div key = { fileName } className = { styles . documentCardWrapper } >
185
204
< TooltipHost
186
- content = { file . FileName }
205
+ content = { fileName }
187
206
calloutProps = { { gapSpace : 0 , isBeakVisible : true } }
188
207
closeDelay = { 200 }
189
208
directionalHint = { DirectionalHint . rightCenter } >
190
209
191
210
< DocumentCard
192
211
onClickHref = { `${ file . ServerRelativeUrl } ?web=1` }
193
212
className = { styles . documentCard } >
194
- < DocumentCardPreview previewImages = { [ this . previewImages [ i ] ] } />
195
- < Label className = { styles . fileLabel } >
196
- { file . FileName }
197
- </ Label >
213
+ < DocumentCardPreview previewImages = { [ previewImage ] } />
214
+ < Label className = { styles . fileLabel } > { fileName } </ Label >
198
215
< DocumentCardActions
199
216
actions = {
200
217
[
0 commit comments