Add delete dictionary from server with switching
This commit is contained in:
parent
cf796a1043
commit
33e9193abb
|
@ -2,15 +2,16 @@ import { clearDictionary, saveDictionary } from "../dictionaryManagement";
|
||||||
import { uploadWholeDictionary, performSync } from "./sync";
|
import { uploadWholeDictionary, performSync } from "./sync";
|
||||||
import { request } from "./helpers";
|
import { request } from "./helpers";
|
||||||
import { saveToken } from "./utilities";
|
import { saveToken } from "./utilities";
|
||||||
import { addMessage } from "../utilities";
|
import { addMessage, hideAllModals } from "../utilities";
|
||||||
import { renderAll } from "../render";
|
import { renderAll } from "../render";
|
||||||
|
import { renderDeletedDictionaryChangeModal, renderChangeDictionaryOptions } from "./render";
|
||||||
|
|
||||||
export function createNewDictionary() {
|
export function createNewDictionary() {
|
||||||
clearDictionary();
|
clearDictionary();
|
||||||
saveDictionary();
|
saveDictionary();
|
||||||
renderAll();
|
renderAll();
|
||||||
uploadWholeDictionary(true);
|
uploadWholeDictionary(true);
|
||||||
document.getElementById('settingsModal').style.display = 'none';
|
hideAllModals();
|
||||||
addMessage('New Dictionary Created!');
|
addMessage('New Dictionary Created!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ export function changeDictionary(dictionary) {
|
||||||
}, successData => {
|
}, successData => {
|
||||||
saveToken(successData.token);
|
saveToken(successData.token);
|
||||||
performSync(successData.dictionary);
|
performSync(successData.dictionary);
|
||||||
document.getElementById('settingsModal').style.display = 'none';
|
hideAllModals();
|
||||||
}, error => {
|
}, error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
addMessage(error, undefined, 'error');
|
addMessage(error, undefined, 'error');
|
||||||
|
@ -36,3 +37,14 @@ export function updateCurrentChangeDictionaryOption() {
|
||||||
document.getElementById('accountSettingsChangeDictionary')
|
document.getElementById('accountSettingsChangeDictionary')
|
||||||
.querySelector(`option[value=${window.currentDictionary.externalID}]`).innerText = label;
|
.querySelector(`option[value=${window.currentDictionary.externalID}]`).innerText = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function deleteDictionary(deletedId) {
|
||||||
|
request({
|
||||||
|
action: 'delete-current-dictionary',
|
||||||
|
}, successful => {
|
||||||
|
if (successful) {
|
||||||
|
renderChangeDictionaryOptions();
|
||||||
|
renderDeletedDictionaryChangeModal(deletedId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {
|
||||||
} from './sync';
|
} from './sync';
|
||||||
import { saveDeletedWordLocally } from './utilities';
|
import { saveDeletedWordLocally } from './utilities';
|
||||||
import { addMessage } from '../utilities';
|
import { addMessage } from '../utilities';
|
||||||
import { updateCurrentChangeDictionaryOption } from './dictionaryManagement';
|
import { updateCurrentChangeDictionaryOption, deleteDictionary } from './dictionaryManagement';
|
||||||
|
|
||||||
export function showLoginForm() {
|
export function showLoginForm() {
|
||||||
renderLoginForm();
|
renderLoginForm();
|
||||||
|
@ -66,3 +66,7 @@ export function deleteWord(wordId) {
|
||||||
export function updateChangeDictionaryOption() {
|
export function updateChangeDictionaryOption() {
|
||||||
updateCurrentChangeDictionaryOption();
|
updateCurrentChangeDictionaryOption();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function deleteCurrentDictionary(deletedId) {
|
||||||
|
deleteDictionary(deletedId);
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import { setupLoginModal, setupChangeDictionary, setupCreateNewDictionary } from "./setupListeners";
|
import { setupLoginModal, setupChangeDictionary, setupCreateNewDictionary, setupDeletedDictionaryChangeModal } from "./setupListeners";
|
||||||
import { request } from "./helpers";
|
import { request } from "./helpers";
|
||||||
|
|
||||||
export function renderLoginForm() {
|
export function renderLoginForm() {
|
||||||
|
@ -99,3 +99,32 @@ export function renderChangeDictionaryOptions() {
|
||||||
setupChangeDictionary();
|
setupChangeDictionary();
|
||||||
}, error => console.error(error));
|
}, 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 = '<option></option>';
|
||||||
|
lazyFilterOptions.forEach(option => {
|
||||||
|
lazyFilter.appendChild(option);
|
||||||
|
});
|
||||||
|
const otherDictionariesHTML = lazyFilter.innerHTML;
|
||||||
|
const modal = document.createElement('section');
|
||||||
|
modal.classList.add('modal');
|
||||||
|
modal.innerHTML = `<div class="modal-background"></div>
|
||||||
|
<div class="modal-content">
|
||||||
|
<section class="info-modal">
|
||||||
|
<h2>Dictionary Deleted</h2>
|
||||||
|
${lazyFilterOptions.length < 1 ? ''
|
||||||
|
: `<label>Select dictionary to load<br>
|
||||||
|
<select id="selectDictionaryToLoad">${otherDictionariesHTML}</select>
|
||||||
|
</label>
|
||||||
|
<p>OR</p>`}
|
||||||
|
<p><a class="button" id="createNewDictionaryAfterDelete">Create a New Dictionary</a></p>
|
||||||
|
</section>
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
document.body.appendChild(modal);
|
||||||
|
|
||||||
|
setupDeletedDictionaryChangeModal();
|
||||||
|
}
|
|
@ -28,3 +28,11 @@ export function setupChangeDictionary() {
|
||||||
export function setupCreateNewDictionary() {
|
export function setupCreateNewDictionary() {
|
||||||
document.getElementById('accountSettingsCreateNewDictionary').addEventListener('click', createNewDictionary);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ export function performSync(remoteDictionary) {
|
||||||
syncWords(remoteDictionary.words, remoteDictionary.deletedWords).then(success => {
|
syncWords(remoteDictionary.words, remoteDictionary.deletedWords).then(success => {
|
||||||
if (success) {
|
if (success) {
|
||||||
renderAll();
|
renderAll();
|
||||||
|
document.getElementById('accountSettingsChangeDictionary').value = window.currentDictionary.externalID;
|
||||||
} else {
|
} else {
|
||||||
console.error('word sync failed');
|
console.error('word sync failed');
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,10 +104,17 @@ export function clearDictionary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteDictionary() {
|
export function deleteDictionary() {
|
||||||
|
const deletedId = window.currentDictionary.externalID;
|
||||||
clearDictionary();
|
clearDictionary();
|
||||||
saveDictionary();
|
saveDictionary();
|
||||||
addMessage('Dictionary Deleted!');
|
addMessage('Dictionary Deleted!');
|
||||||
renderAll();
|
renderAll();
|
||||||
|
|
||||||
|
if (hasToken()) {
|
||||||
|
import('./account/index.js').then(account => {
|
||||||
|
account.deleteCurrentDictionary(deletedId);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function confirmDeleteDictionary() {
|
export function confirmDeleteDictionary() {
|
||||||
|
|
|
@ -65,6 +65,15 @@ VALUES ($new_id, ?, ?, ?, ?)";
|
||||||
return false;
|
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) {
|
public function getAllNames ($user) {
|
||||||
$query = "SELECT id, name, specification FROM dictionaries WHERE user=$user";
|
$query = "SELECT id, name, specification FROM dictionaries WHERE user=$user";
|
||||||
$results = $this->db->query($query)->fetchAll();
|
$results = $this->db->query($query)->fetchAll();
|
||||||
|
|
|
@ -205,6 +205,19 @@ VALUES (?, ?, ?, ?, ?)';
|
||||||
return false;
|
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) {
|
public function updateCurrentDictionaryDetails ($token, $dictionary_details) {
|
||||||
$user_data = $this->token->decode($token);
|
$user_data = $this->token->decode($token);
|
||||||
if ($user_data !== false) {
|
if ($user_data !== false) {
|
||||||
|
|
|
@ -244,6 +244,26 @@ switch ($action) {
|
||||||
'error' => true,
|
'error' => true,
|
||||||
), 400);
|
), 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': {
|
case 'set-dictionary-details': {
|
||||||
if ($token !== false && isset($request['details'])) {
|
if ($token !== false && isset($request['details'])) {
|
||||||
$user = new User();
|
$user = new User();
|
||||||
|
|
Loading…
Reference in New Issue