Skip to content

Commit

Permalink
add undo for deleted notes
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar committed Oct 26, 2019
1 parent 329f622 commit f94360e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ export default {
const search = this.filter.search.toLowerCase()
const notes = this.notes.filter(note => {
const searchFields = [ 'title', 'category' ]
if (note.deleting === 'deleting') {
return false
}
if (this.filter.category !== null
&& this.filter.category !== note.category
&& !note.category.startsWith(this.filter.category + '/')) {
return false
}
const searchFields = [ 'title', 'category' ]
if (search !== '') {
return searchFields.some(
searchField => note[searchField].toLowerCase().indexOf(search) !== -1
Expand Down Expand Up @@ -144,7 +147,7 @@ export default {
},
routeFirst() {
const availableNotes = this.filteredNotes.filter(note => !note.error)
const availableNotes = this.filteredNotes.filter(note => !note.error && !note.deleting)
if (availableNotes.length > 0) {
this.routeToNote(availableNotes[0].id)
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/NotesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ export default {
})
},

prepareDeleteNote(noteId) {
store.commit('setNoteAttribute', { noteId: noteId, attribute: 'deleting', value: 'prepare' })
},

undoDeleteNote(noteId) {
store.commit('setNoteAttribute', { noteId: noteId, attribute: 'deleting', value: null })
},

deleteNote(noteId) {
store.commit('setNoteAttribute', { noteId: noteId, attribute: 'deleting', value: true })
store.commit('setNoteAttribute', { noteId: noteId, attribute: 'deleting', value: 'deleting' })
return axios
.delete(this.url('/notes/' + noteId))
.then(() => {
Expand All @@ -117,6 +125,7 @@ export default {
.catch(err => {
console.error(err)
this.handleSyncError(this.t('notes', 'Deleting note {id} has failed.', { id: noteId }))
this.undoDeleteNote(noteId)
throw err
})
},
Expand Down
30 changes: 25 additions & 5 deletions src/components/NavigationNoteItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
:menu-open.sync="actionsOpen"
:to="{ name: 'note', params: { noteId: note.id.toString() } }"
:class="{ actionsOpen }"
:undo="isPrepareDeleting"
@undo="onUndoDeleteNote"
>
<template slot="actions">
<template v-if="!note.deleting" slot="actions">
<ActionButton :icon="actionFavoriteIcon" @click="onToggleFavorite">
{{ actionFavoriteText }}
</ActionButton>
Expand Down Expand Up @@ -49,6 +51,7 @@ export default {
delete: false,
},
actionsOpen: false,
undoTimer: null,
}
},
Expand All @@ -63,8 +66,16 @@ export default {
return icon
},
isPrepareDeleting() {
return this.note.deleting === 'prepare'
},
title() {
return this.note.title + (this.note.unsaved ? ' *' : '')
if (this.isPrepareDeleting) {
return this.t('notes', 'Deleted {title}', { title: this.note.title })
} else {
return this.note.title + (this.note.unsaved ? ' *' : '')
}
},
actionFavoriteText() {
Expand Down Expand Up @@ -106,18 +117,27 @@ export default {
},
onDeleteNote() {
this.actionsOpen = false
NotesService.prepareDeleteNote(this.note.id)
this.undoTimer = setTimeout(this.onDeleteNoteFinally, 7000)
this.$emit('note-deleted')
},
onUndoDeleteNote() {
clearTimeout(this.undoTimer)
NotesService.undoDeleteNote(this.note.id)
},
onDeleteNoteFinally() {
this.loading.delete = true
NotesService.deleteNote(this.note.id)
.then(() => {
this.$emit('note-deleted')
})
.catch(() => {
})
.finally(() => {
this.loading.delete = false
this.actionsOpen = false
})
// TODO implement undo
},
},
}
Expand Down

0 comments on commit f94360e

Please sign in to comment.