@@ -22,13 +22,15 @@ type Props = {
22
22
}
23
23
24
24
type State = {
25
+ fileToDeleteOnSuccesfulUpload : string ,
25
26
peliasPassword : string ,
26
27
peliasUsername : string ,
27
28
peliasWebhookUrl : string
28
29
}
29
30
30
31
class PeliasPanel extends Component < Props , State > {
31
32
state = {
33
+ fileToDeleteOnSuccesfulUpload : null ,
32
34
peliasWebhookUrl : '' ,
33
35
peliasPassword : '' ,
34
36
peliasUsername : ''
@@ -78,13 +80,16 @@ class PeliasPanel extends Component<Props, State> {
78
80
*/
79
81
_onConfirmUpload = async ( files : Array < File > ) => {
80
82
const { user, deployment } = this . props
81
- const peliasCsvFiles = deployment . peliasCsvFiles || [ ]
83
+ const { fileToDeleteOnSuccesfulUpload } = this . state
84
+
85
+ let peliasCsvFiles = deployment . peliasCsvFiles || [ ]
82
86
83
87
if ( ! user . token || user . token === null ) {
84
88
return false
85
89
}
86
90
87
91
const file = files [ 0 ]
92
+ // TODO: more extensive csv validation?
88
93
if ( file . type === 'text/csv' ) {
89
94
const url = `${ SECURE_API_PREFIX } deployments/${ deployment . id } /upload`
90
95
const formData = new FormData ( )
@@ -97,6 +102,13 @@ class PeliasPanel extends Component<Props, State> {
97
102
body : formData
98
103
} )
99
104
const s3UploadUrl = await s3UploadResponse . text ( )
105
+
106
+ // If a file is set to delete on successful upload, delete it here
107
+ if ( fileToDeleteOnSuccesfulUpload ) {
108
+ peliasCsvFiles = peliasCsvFiles . filter ( csv => csv !== fileToDeleteOnSuccesfulUpload )
109
+ this . setState ( { fileToDeleteOnSuccesfulUpload : null } )
110
+ }
111
+
100
112
this . _updateDeployment ( { 'peliasCsvFiles' : [ ...peliasCsvFiles , s3UploadUrl ] } )
101
113
return true
102
114
} else {
@@ -140,10 +152,17 @@ class PeliasPanel extends Component<Props, State> {
140
152
return (
141
153
< li key = { url } >
142
154
{ fileName } { ' ' }
143
- < Button disabled = { ! enabled } bsSize = 'xsmall' onClick = { ( ) => { this . refs . uploadModal . open ( ) ; this . _deleteCsvFile ( url ) } } >
155
+ < Button disabled = { ! enabled } bsSize = 'xsmall' onClick = { ( ) => {
156
+ this . refs . uploadModal . open ( )
157
+ this . setState ( { fileToDeleteOnSuccesfulUpload : url } )
158
+ } } >
144
159
Replace
145
160
</ Button >
146
- < Button disabled = { ! enabled } bsSize = 'xsmall' onClick = { ( ) => this . _deleteCsvFile ( url ) } > Delete</ Button >
161
+ < Button disabled = { ! enabled } bsSize = 'xsmall' onClick = { ( ) => {
162
+ if ( confirm ( 'Are you sure you want to delete this CSV file?' ) ) {
163
+ this . _deleteCsvFile ( url )
164
+ }
165
+ } } > Delete</ Button >
147
166
</ li >
148
167
)
149
168
}
@@ -213,6 +232,7 @@ class PeliasPanel extends Component<Props, State> {
213
232
title = 'Upload CSV File'
214
233
body = 'Select a CSV file of POIs to upload:'
215
234
onConfirm = { this . _onConfirmUpload }
235
+ onCancel = { ( ) => this . setState ( { fileToDeleteOnSuccesfulUpload : null } ) }
216
236
errorMessage = 'Uploaded file must be a valid csv file (.csv).'
217
237
/>
218
238
< ul >
0 commit comments