Skip to content

Asking for confirmation to remove and overwrite files #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 81 additions & 32 deletions ui/arduino/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function store(state, emitter) {
state.serialPath = null
state.isTerminalOpen = false
state.serialFiles = []
emitter.emit('close-panel')
emitter.emit('render')
})
emitter.on('connect', async (path) => {
log('connect')
Expand Down Expand Up @@ -150,17 +150,20 @@ function store(state, emitter) {
}

emitter.emit('update-files')
emitter.emit('message', `${filename} is saved on ${state.selectedDevice}.`, 1000)
})
emitter.on('remove', async () => {
log('remove')
if (state.selectedDevice === 'serial') {
await serial.removeFile(state.selectedFile)
}
if (state.selectedDevice === 'disk') {
await disk.removeFile(state.diskPath, state.selectedFile)
if (confirm(`Do you want to remove ${state.selectedFile} from ${state.selectedDevice}?`)) {
if (state.selectedDevice === 'serial') {
await serial.removeFile(state.selectedFile)
}
if (state.selectedDevice === 'disk') {
await disk.removeFile(state.diskPath, state.selectedFile)
}
emitter.emit('update-files')
emitter.emit('render')
}
emitter.emit('update-files')
emitter.emit('render')
})
emitter.on('select-file', async (device, filename) => {
log('select-file')
Expand Down Expand Up @@ -226,19 +229,37 @@ function store(state, emitter) {
})
emitter.on('upload', async () => {
log('upload')
emitter.emit('message', 'Uploading file... Please wait')
await serial.uploadFile(state.diskPath, state.selectedFile)
emitter.emit('message', 'File uploaded!', 500)
emitter.emit('update-files')
emitter.emit('render')
let confirmation = true
if (state.serialFiles.indexOf(state.selectedFile) !== -1) {
confirmation = confirm(`Do you want to overwrite ${state.selectedFile} on board?`)
}
if (confirmation) {
emitter.emit('message', 'Uploading file... Please wait')
let editor = state.cache(AceEditor, 'editor').editor
let contents = editor.getValue()
await disk.saveFileContent(state.diskPath, state.selectedFile, contents)
await serial.uploadFile(state.diskPath, state.selectedFile)
emitter.emit('message', 'File uploaded!', 500)
emitter.emit('update-files')
emitter.emit('render')
}
})
emitter.on('download', async () => {
log('download')
emitter.emit('message', 'Downloading file... Please wait')
await serial.downloadFile(state.diskPath, state.selectedFile)
emitter.emit('message', 'File downloaded!', 500)
emitter.emit('update-files')
emitter.emit('render')
let confirmation = true
if (state.diskFiles.indexOf(state.selectedFile) !== -1) {
confirmation = confirm(`Do you want to overwrite ${state.selectedFile} on disk?`)
}
if (confirmation) {
emitter.emit('message', 'Downloading file... Please wait')
let editor = state.cache(AceEditor, 'editor').editor
let contents = editor.getValue()
await serial.saveFileContent(state.selectedFile, contents)
await serial.downloadFile(state.diskPath, state.selectedFile)
emitter.emit('message', 'File downloaded!', 500)
emitter.emit('update-files')
emitter.emit('render')
}
})

// PANEL MANAGEMENT
Expand Down Expand Up @@ -298,31 +319,59 @@ function store(state, emitter) {
let contents = editor.getValue()

if (state.selectedDevice === 'serial') {
if (state.serialFiles.indexOf(oldFilename) !== -1) {
// If old name exists, rename file
await serial.renameFile(oldFilename, filename)
// Ask for confirmation to overwrite existing file
let confirmation = true
if (state.serialFiles.indexOf(filename) !== -1) {
confirmation = confirm(`Do you want to overwrite ${filename} on ${state.selectedDevice}?`)
}

if (confirmation) {
if (state.serialFiles.indexOf(oldFilename) !== -1) {
// If old name exists, save old file and rename
await serial.saveFileContent(oldFilename, contents)
await serial.renameFile(oldFilename, filename)
} else {
// If old name doesn't exist create new file
await serial.saveFileContent(filename, contents)
}
state.isEditingFilename = false
emitter.emit('update-files')
emitter.emit('render')

emitter.emit('message', `${filename} is saved on ${state.selectedDevice}.`, 1000)
} else {
// If old name doesn't exist create new file
await serial.saveFileContent(filename, contents)
state.isEditingFilename = false
emitter.emit('render')
}
}

if (state.diskPath !== null && state.selectedDevice === 'disk') {
// Ask for confirmation to overwrite existing file
let confirmation = true
if (state.diskFiles.indexOf(filename) !== -1) {
confirmation = confirm(`Do you want to overwrite ${filename} on ${state.selectedDevice}?`)
}
if (confirmation) {
if (state.diskFiles.indexOf(oldFilename) !== -1) {
// If old name exists, save old file and rename
await disk.saveFileContent(state.diskPath, oldFilename, contents)
await disk.renameFile(state.diskPath, oldFilename, filename)
} else {
// If old name doesn't exist create new file
await disk.saveFileContent(state.diskPath, filename, contents)
}
state.isEditingFilename = false
emitter.emit('update-files')
emitter.emit('render')

if (state.diskFiles.indexOf(oldFilename) !== -1) {
// If old name exists, rename file
await disk.renameFile(state.diskPath, oldFilename, filename)
emitter.emit('message', `${filename} is saved on ${state.selectedDevice}.`, 1000)
} else {
// If old name doesn't exist create new file
await disk.saveFileContent(state.diskPath, filename, contents)
state.isEditingFilename = false
emitter.emit('render')
}
}

state.isEditingFilename = false
emitter.emit('update-files')
emitter.emit('render')

emitter.emit('message', "Filename is saved.", 1000)
})

emitter.on('message', (text, timeout) => {
Expand Down