Skip to content

Commit 1f67a66

Browse files
improvement(PeliasPanel): confirm before delete or replace
1 parent 64d1802 commit 1f67a66

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/manager/components/deployment/PeliasPanel.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ type Props = {
2222
}
2323

2424
type State = {
25+
fileToDeleteOnSuccesfulUpload: string,
2526
peliasPassword: string,
2627
peliasUsername: string,
2728
peliasWebhookUrl: string
2829
}
2930

3031
class PeliasPanel extends Component<Props, State> {
3132
state = {
33+
fileToDeleteOnSuccesfulUpload: null,
3234
peliasWebhookUrl: '',
3335
peliasPassword: '',
3436
peliasUsername: ''
@@ -78,13 +80,16 @@ class PeliasPanel extends Component<Props, State> {
7880
*/
7981
_onConfirmUpload = async (files: Array<File>) => {
8082
const { user, deployment } = this.props
81-
const peliasCsvFiles = deployment.peliasCsvFiles || []
83+
const { fileToDeleteOnSuccesfulUpload } = this.state
84+
85+
let peliasCsvFiles = deployment.peliasCsvFiles || []
8286

8387
if (!user.token || user.token === null) {
8488
return false
8589
}
8690

8791
const file = files[0]
92+
// TODO: more extensive csv validation?
8893
if (file.type === 'text/csv') {
8994
const url = `${SECURE_API_PREFIX}deployments/${deployment.id}/upload`
9095
const formData = new FormData()
@@ -97,6 +102,13 @@ class PeliasPanel extends Component<Props, State> {
97102
body: formData
98103
})
99104
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+
100112
this._updateDeployment({ 'peliasCsvFiles': [ ...peliasCsvFiles, s3UploadUrl ] })
101113
return true
102114
} else {
@@ -140,10 +152,17 @@ class PeliasPanel extends Component<Props, State> {
140152
return (
141153
<li key={url}>
142154
{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+
}}>
144159
Replace
145160
</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>
147166
</li>
148167
)
149168
}
@@ -213,6 +232,7 @@ class PeliasPanel extends Component<Props, State> {
213232
title='Upload CSV File'
214233
body='Select a CSV file of POIs to upload:'
215234
onConfirm={this._onConfirmUpload}
235+
onCancel={() => this.setState({fileToDeleteOnSuccesfulUpload: null})}
216236
errorMessage='Uploaded file must be a valid csv file (.csv).'
217237
/>
218238
<ul>

0 commit comments

Comments
 (0)