diff --git a/src/js/account/dictionaryManagement.js b/src/js/account/dictionaryManagement.js index a1b2390..6b00431 100644 --- a/src/js/account/dictionaryManagement.js +++ b/src/js/account/dictionaryManagement.js @@ -2,15 +2,16 @@ import { clearDictionary, saveDictionary } from "../dictionaryManagement"; import { uploadWholeDictionary, performSync } from "./sync"; import { request } from "./helpers"; import { saveToken } from "./utilities"; -import { addMessage } from "../utilities"; +import { addMessage, hideAllModals } from "../utilities"; import { renderAll } from "../render"; +import { renderDeletedDictionaryChangeModal, renderChangeDictionaryOptions } from "./render"; export function createNewDictionary() { clearDictionary(); saveDictionary(); renderAll(); uploadWholeDictionary(true); - document.getElementById('settingsModal').style.display = 'none'; + hideAllModals(); addMessage('New Dictionary Created!'); } @@ -23,7 +24,7 @@ export function changeDictionary(dictionary) { }, successData => { saveToken(successData.token); performSync(successData.dictionary); - document.getElementById('settingsModal').style.display = 'none'; + hideAllModals(); }, error => { console.error(error); addMessage(error, undefined, 'error'); @@ -36,3 +37,14 @@ export function updateCurrentChangeDictionaryOption() { document.getElementById('accountSettingsChangeDictionary') .querySelector(`option[value=${window.currentDictionary.externalID}]`).innerText = label; } + +export function deleteDictionary(deletedId) { + request({ + action: 'delete-current-dictionary', + }, successful => { + if (successful) { + renderChangeDictionaryOptions(); + renderDeletedDictionaryChangeModal(deletedId); + } + }) +} diff --git a/src/js/account/index.js b/src/js/account/index.js index d1a6fe7..7ade075 100644 --- a/src/js/account/index.js +++ b/src/js/account/index.js @@ -10,7 +10,7 @@ import { } from './sync'; import { saveDeletedWordLocally } from './utilities'; import { addMessage } from '../utilities'; -import { updateCurrentChangeDictionaryOption } from './dictionaryManagement'; +import { updateCurrentChangeDictionaryOption, deleteDictionary } from './dictionaryManagement'; export function showLoginForm() { renderLoginForm(); @@ -65,4 +65,8 @@ export function deleteWord(wordId) { export function updateChangeDictionaryOption() { updateCurrentChangeDictionaryOption(); +} + +export function deleteCurrentDictionary(deletedId) { + deleteDictionary(deletedId); } \ No newline at end of file diff --git a/src/js/account/render.js b/src/js/account/render.js index 4e44120..6b5387f 100644 --- a/src/js/account/render.js +++ b/src/js/account/render.js @@ -1,4 +1,4 @@ -import { setupLoginModal, setupChangeDictionary, setupCreateNewDictionary } from "./setupListeners"; +import { setupLoginModal, setupChangeDictionary, setupCreateNewDictionary, setupDeletedDictionaryChangeModal } from "./setupListeners"; import { request } from "./helpers"; export function renderLoginForm() { @@ -98,4 +98,33 @@ export function renderChangeDictionaryOptions() { changeDictionarySelect.value = window.currentDictionary.externalID; setupChangeDictionary(); }, error => console.error(error)); +} + +export function renderDeletedDictionaryChangeModal(deletedId) { + const changeDictionarySelect = document.getElementById('accountSettingsChangeDictionary'); + const lazyFilterOptions = changeDictionarySelect.querySelectorAll(`option:not([value="${deletedId}"])`); + const lazyFilter = document.createElement('select'); + lazyFilter.innerHTML = ''; + lazyFilterOptions.forEach(option => { + lazyFilter.appendChild(option); + }); + const otherDictionariesHTML = lazyFilter.innerHTML; + const modal = document.createElement('section'); + modal.classList.add('modal'); + modal.innerHTML = ` + `; + + document.body.appendChild(modal); + + setupDeletedDictionaryChangeModal(); } \ No newline at end of file diff --git a/src/js/account/setupListeners.js b/src/js/account/setupListeners.js index 77b9088..f17816b 100644 --- a/src/js/account/setupListeners.js +++ b/src/js/account/setupListeners.js @@ -28,3 +28,11 @@ export function setupChangeDictionary() { export function setupCreateNewDictionary() { document.getElementById('accountSettingsCreateNewDictionary').addEventListener('click', createNewDictionary); } + +export function setupDeletedDictionaryChangeModal() { + const selectDictionaryToLoad = document.getElementById('selectDictionaryToLoad') + if (selectDictionaryToLoad) { + selectDictionaryToLoad.addEventListener('change', changeDictionary); + } + document.getElementById('createNewDictionaryAfterDelete').addEventListener('click', createNewDictionary); +} diff --git a/src/js/account/sync.js b/src/js/account/sync.js index 42ee985..ed7066e 100644 --- a/src/js/account/sync.js +++ b/src/js/account/sync.js @@ -64,6 +64,7 @@ export function performSync(remoteDictionary) { syncWords(remoteDictionary.words, remoteDictionary.deletedWords).then(success => { if (success) { renderAll(); + document.getElementById('accountSettingsChangeDictionary').value = window.currentDictionary.externalID; } else { console.error('word sync failed'); } diff --git a/src/js/dictionaryManagement.js b/src/js/dictionaryManagement.js index 466993e..bcc3642 100644 --- a/src/js/dictionaryManagement.js +++ b/src/js/dictionaryManagement.js @@ -104,10 +104,17 @@ export function clearDictionary() { } export function deleteDictionary() { + const deletedId = window.currentDictionary.externalID; clearDictionary(); saveDictionary(); addMessage('Dictionary Deleted!'); renderAll(); + + if (hasToken()) { + import('./account/index.js').then(account => { + account.deleteCurrentDictionary(deletedId); + }); + } } export function confirmDeleteDictionary() { diff --git a/src/php/api/Dictionary.php b/src/php/api/Dictionary.php index d1ed813..784b3b7 100644 --- a/src/php/api/Dictionary.php +++ b/src/php/api/Dictionary.php @@ -65,6 +65,15 @@ VALUES ($new_id, ?, ?, ?, ?)"; return false; } + public function deleteDictionary ($user, $dictionary) { + $update_query = 'DELETE FROM dictionaries WHERE id=? AND user=?'; + $update = $this->db->query($update_query, array($dictionary, $user)); + if ($update->rowCount() > 0) { + return true; + } + return false; + } + public function getAllNames ($user) { $query = "SELECT id, name, specification FROM dictionaries WHERE user=$user"; $results = $this->db->query($query)->fetchAll(); diff --git a/src/php/api/User.php b/src/php/api/User.php index 629e9a2..4163c61 100644 --- a/src/php/api/User.php +++ b/src/php/api/User.php @@ -205,6 +205,19 @@ VALUES (?, ?, ?, ?, ?)'; return false; } + public function deleteCurrentDictionary ($token) { + $user_data = $this->token->decode($token); + if ($user_data !== false) { + $user = $user_data->id; + $dictionary = $user_data->dictionary; + $deleted = $this->dictionary->deleteDictionary($user, $dictionary); + if ($deleted) { + return true; + } + } + return false; + } + public function updateCurrentDictionaryDetails ($token, $dictionary_details) { $user_data = $this->token->decode($token); if ($user_data !== false) { diff --git a/src/php/api/index.php b/src/php/api/index.php index 3212c6d..ce8bc08 100644 --- a/src/php/api/index.php +++ b/src/php/api/index.php @@ -244,6 +244,26 @@ switch ($action) { 'error' => true, ), 400); } + case 'delete-current-dictionary': { + if ($token !== false) { + $user = new User(); + $dictionary_deleted = $user->deleteCurrentDictionary($token); + if ($dictionary_deleted !== false) { + return Response::json(array( + 'data' => $dictionary_deleted, + 'error' => false, + ), 200); + } + return Response::json(array( + 'data' => 'Could not delete dictionary: invalid token', + 'error' => true, + ), 401); + } + return Response::json(array( + 'data' => 'Could not delete dictionary: no token provided', + 'error' => true, + ), 400); + } case 'set-dictionary-details': { if ($token !== false && isset($request['details'])) { $user = new User();