Skip to content

feat: remove snippets in trash #80 #82

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
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
34 changes: 32 additions & 2 deletions src/renderer/components/snippets/SnippetListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<script>
import { mapGetters } from 'vuex'
import { format, isSameDay } from 'date-fns'
import { menu } from '@@/lib'
import { menu, dialog } from '@@/lib'
import { track } from '@@/lib/analytics'

export default {
Expand Down Expand Up @@ -71,6 +71,7 @@ export default {
'snippetsBySort',
'sort'
]),
...mapGetters('folders', { selectedFolderId: 'selectedId' }),
isSelected () {
if (!this.selectedId) return null

Expand Down Expand Up @@ -198,7 +199,7 @@ export default {
isFavorites = this.selectedSnippets.some(i => i.isFavorites)
}

const menuItems = [
let menuItems = [
{
label: 'Add to Favorites',
click: () => {
Expand Down Expand Up @@ -302,6 +303,35 @@ export default {
menuItems.splice(1, 0, removeFromFavorites)
}

if (this.selectedFolderId === 'trash') {
const deleteNow = {
label: 'Delete Now',
click: async () => {
const plural = ids.length > 1 ? 'snippets' : 'snippet'
const buttonId = dialog.showMessageBoxSync({
message: `Are you sure you want to permanently delete ${plural}?`,
detail: 'You cannot undo this action.',
buttons: ['Delete', 'Cancel'],
defaultId: 0,
cancelId: 1
})
if (buttonId === 0) {
await this.$store.dispatch('snippets/deleteSnippets', ids)
const firstSnippet = this.snippetsBySort[0]

if (firstSnippet) {
this.$store.dispatch('snippets/setSelected', firstSnippet)
} else {
this.$store.dispatch('snippets/setSelected', null)
}

track('snippets/delete-now')
}
}
}
menuItems = [deleteNow]
}

const contextMenu = menu.popup(menuItems)
contextMenu.addListener('menu-will-close', () => {
this.context = false
Expand Down
11 changes: 11 additions & 0 deletions src/renderer/store/modules/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ export default {
commit('SET_NEW', null)
})
},
deleteSnippets ({ dispatch, rootGetters }, ids) {
const foldersIds = rootGetters['folders/selectedIds']
const folderId = rootGetters['folders/selectedId']
const defaultQuery = { folderId: { $in: foldersIds } }
const query = defaultLibraryQuery(defaultQuery, folderId)

db.snippets.remove({ _id: { $in: ids } }, { multi: true }, (err, num) => {
if (err) return
dispatch('getSnippets', query)
})
},
emptyTrash ({ dispatch, rootGetters }) {
const ids = rootGetters['folders/selectedIds']
const folderId = rootGetters['folders/selectedId']
Expand Down