From 3a29d560728e086606cf7c5fd5081d0d7655fb4e Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 7 May 2019 17:50:48 -0600 Subject: [PATCH] Add delete word with confirmation --- src/js/setupListeners.js | 4 +++- src/js/wordManagement.js | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/js/setupListeners.js b/src/js/setupListeners.js index 780a0ba..d2a9901 100644 --- a/src/js/setupListeners.js +++ b/src/js/setupListeners.js @@ -1,6 +1,6 @@ import {showSection} from './displayToggles'; import { renderWords, renderEditForm, renderMaximizedTextbox } from './render'; -import { validateWord, addWord, confirmEditWord, cancelEditWord } from './wordManagement'; +import { validateWord, addWord, confirmEditWord, cancelEditWord, confirmDeleteWord } from './wordManagement'; import { removeTags } from '../helpers'; import { getNextId } from './utilities'; import { openEditModal, save, saveAndClose } from './dictionaryManagement'; @@ -185,6 +185,8 @@ export function setupWordOptionSelections() { break; } case 'Delete': { + option.removeEventListener('click', confirmDeleteWord); + option.addEventListener('click', confirmDeleteWord); break; } } diff --git a/src/js/wordManagement.js b/src/js/wordManagement.js index 2de6ef5..26f227f 100644 --- a/src/js/wordManagement.js +++ b/src/js/wordManagement.js @@ -48,6 +48,14 @@ export function addWord(word, render = true) { sortWords(render); } +export function deleteWord(wordId) { + const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId); + if (wordIndex > -1) { + window.currentDictionary.words.splice(wordIndex, 1); + } + sortWords(true); +} + export function updateWord(word, wordId) { const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId); @@ -86,13 +94,27 @@ export function confirmEditWord() { export function cancelEditWord() { const wordId = parseInt(this.parentElement.id.replace('editForm_', '')); - console.log('wordId', wordId); if (confirm(`Are you sure you want to cancel?\n(Any changes will be lost!)`)) { document.getElementById('editForm_' + wordId).classList.add('done'); renderWords(); } } +export function confirmDeleteWord(wordId) { + wordId = typeof wordId.target === 'undefined' ? wordId : parseInt(wordId.target.id.replace('delete_', '')); + const word = window.currentDictionary.words.find(w => w.wordId === wordId); + + if (!word) { + console.error('Something went wrong! Couldn\'t find word with id of ' + wordId); + } else { + if (confirm(`Are you sure you want to delete "${word.name}"?`)) { + if (confirm(`Just to double-check:\nDo you really want to delete "${word.name}"?\n\nYou won't be able to undo it!`)) { + deleteWord(wordId); + } + } + } +} + export function getOpenEditForms() { const openEditForms = document.getElementsByClassName('edit-form'); const formsToReopen = [];