Set up change dictionary

This commit is contained in:
Robbie Antenesse 2019-05-23 17:00:13 -06:00
parent f8b9503960
commit 24a5baa545
8 changed files with 87 additions and 53 deletions

View File

@ -1,25 +1,27 @@
import { saveEditModal } from "../dictionaryManagement"; import { performSync } from "./sync";
import { syncDetails } from "./sync"; import { request } from "./helpers";
import { saveToken } from "./utilities";
import { addMessage } from "../utilities"; import { addMessage } from "../utilities";
export function saveEditModalAndSync() { export function changeDictionary(dictionary) {
saveEditModal(); dictionary = typeof dictionary.target !== 'undefined' ? dictionary.target.value : dictionary;
if (dictionary !== window.currentDictionary.externalID) {
return syncDetails().then(successful => { request({
if (successful) { action: 'change-dictionary',
addMessage('Synced Successfully'); dictionary,
} else { }, successData => {
addMessage('Could Not Sync'); saveToken(successData.token);
performSync(successData.dictionary);
document.getElementById('settingsModal').style.display = 'none';
}, error => {
console.error(error);
addMessage(error, undefined, 'error');
});
} }
})
.catch(err => {
console.error(err);
addMessage('Could not connect');
});
} }
export function saveAndCloseEditModalAndSync() { export function updateCurrentChangeDictionaryOption() {
saveEditModalAndSync().then(() => { const label = window.currentDictionary.name + ' ' + window.currentDictionary.specification;
document.getElementById('editModal').style.display = 'none'; document.getElementById('accountSettingsChangeDictionary')
}); .querySelector(`option[value=${window.currentDictionary.externalID}]`).innerText = label;
} }

View File

@ -10,6 +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';
export function showLoginForm() { export function showLoginForm() {
renderLoginForm(); renderLoginForm();
@ -57,3 +58,7 @@ export function deleteWord(wordId) {
}, 10000); }, 10000);
}); });
} }
export function updateChangeDictionaryOption() {
updateCurrentChangeDictionaryOption();
}

View File

@ -1,4 +1,5 @@
import { setupLoginModal } from "./setupListeners"; import { setupLoginModal, setupChangeDictionary } from "./setupListeners";
import { request } from "./helpers";
export function renderLoginForm() { export function renderLoginForm() {
const loginModal = document.createElement('section'); const loginModal = document.createElement('section');
@ -55,9 +56,9 @@ export function renderLoginForm() {
export function renderAccountSettings() { export function renderAccountSettings() {
const accountSettingsColumn = document.getElementById('accountSettings'); const accountSettingsColumn = document.getElementById('accountSettings');
const accountSettingsHTML = `<h3>Account Settings</h3> const accountSettingsHTML = `<h3>Account Settings</h3>
<label>Email Address<br><input id="accountSettingsEmail" required maxlength="100"></label> <label>Email Address<br><input id="accountSettingsEmail" required maxlength="100" value="${window.account.email}"></label>
<label>Public Name<br><input id="accountSettingsPublicName" placeholder="Someone" maxlength="50"></label> <label>Public Name<br><input id="accountSettingsPublicName" placeholder="Someone" maxlength="50" value="${window.account.publicName}"></label>
<label>Allow Emails <input type="checkbox" id="accountSettingsAllowEmails"></label> <label>Allow Emails <input type="checkbox" id="accountSettingsAllowEmails"${window.account.allowEmails ? ' checked' : ''}></label>
<label>Change Dictionary<br><select id="accountSettingsChangeDictionary"></select></label> <label>Change Dictionary<br><select id="accountSettingsChangeDictionary"></select></label>
<h4>Request Your Data</h4> <h4>Request Your Data</h4>
<p> <p>
@ -73,4 +74,18 @@ export function renderAccountSettings() {
</p> </p>
`; `;
accountSettingsColumn.innerHTML = accountSettingsHTML; accountSettingsColumn.innerHTML = accountSettingsHTML;
renderChangeDictionaryOptions();
}
export function renderChangeDictionaryOptions() {
request({
action: 'get-all-dictionary-names',
}, dictionaries => {
const changeDictionarySelect = document.getElementById('accountSettingsChangeDictionary');
const optionsHTML = dictionaries.map(dictionary => `<option value="${dictionary.id}">${dictionary.name}</option>`).join('');
changeDictionarySelect.innerHTML = optionsHTML;
changeDictionarySelect.value = window.currentDictionary.externalID;
setupChangeDictionary();
}, error => console.error(error));
} }

View File

@ -1,5 +1,6 @@
import { logIn, createAccount } from "./login"; import { logIn, createAccount } from "./login";
import { setCookie } from "../StackOverflow/cookie"; import { setCookie } from "../StackOverflow/cookie";
import { changeDictionary } from "./dictionaryManagement";
export function setupLoginModal(modal) { export function setupLoginModal(modal) {
const closeElements = modal.querySelectorAll('.modal-background, .close-button'); const closeElements = modal.querySelectorAll('.modal-background, .close-button');
@ -19,3 +20,7 @@ export function setupLogoutButton(logoutButton) {
window.location.reload(); window.location.reload();
}); });
} }
export function setupChangeDictionary() {
document.getElementById('accountSettingsChangeDictionary').addEventListener('change', changeDictionary);
}

View File

@ -5,6 +5,7 @@ import { saveToken } from "./utilities";
import { renderAll } from "../render"; import { renderAll } from "../render";
import { sortWords } from "../wordManagement"; import { sortWords } from "../wordManagement";
import { getLocalDeletedWords, clearLocalDeletedWords, saveDeletedWordsLocally } from "./utilities"; import { getLocalDeletedWords, clearLocalDeletedWords, saveDeletedWordsLocally } from "./utilities";
import { renderChangeDictionaryOptions } from "./render";
/* Outline for syncing /* Outline for syncing
login login
@ -41,11 +42,18 @@ export function syncDictionary() {
request({ request({
action: 'get-current-dictionary', action: 'get-current-dictionary',
}, remote => { }, remote => {
// console.log(remote); performSync(remote);
if (remote.details.externalID !== window.currentDictionary.externalID) { }, error => {
console.error(error);
}).catch(err => console.error(err));
}
}
export function performSync(remoteDictionary) {
if (remoteDictionary.details.externalID !== window.currentDictionary.externalID) {
clearDictionary(); clearDictionary();
} }
const detailsSynced = syncDetails(remote.details); const detailsSynced = syncDetails(remoteDictionary.details);
if (detailsSynced === false) { if (detailsSynced === false) {
addMessage('Could not sync', 10000, 'error'); addMessage('Could not sync', 10000, 'error');
@ -53,7 +61,7 @@ export function syncDictionary() {
detailsSynced.then(success => { detailsSynced.then(success => {
renderAll(); renderAll();
if (success) { if (success) {
syncWords(remote.words, remote.deletedWords).then(success => { syncWords(remoteDictionary.words, remoteDictionary.deletedWords).then(success => {
if (success) { if (success) {
renderAll(); renderAll();
} else { } else {
@ -65,10 +73,6 @@ export function syncDictionary() {
} }
}); });
} }
}, error => {
console.error(error);
}).catch(err => console.error(err));
}
} }
export function uploadWholeDictionary(asNew = false) { export function uploadWholeDictionary(asNew = false) {
@ -97,6 +101,7 @@ export function uploadWholeDictionary(asNew = false) {
window.currentDictionary.externalID = remoteId; window.currentDictionary.externalID = remoteId;
saveDictionary(false); saveDictionary(false);
addMessage('Dictionary Uploaded Successfully'); addMessage('Dictionary Uploaded Successfully');
renderChangeDictionaryOptions();
}, errorData => { }, errorData => {
console.error(errorData); console.error(errorData);
addMessage(errorData, 10000, 'error'); addMessage(errorData, 10000, 'error');

View File

@ -72,6 +72,7 @@ export function saveEditModal() {
if (hasToken()) { if (hasToken()) {
import('./account/index.js').then(account => { import('./account/index.js').then(account => {
account.uploadDetailsDirect(); account.uploadDetailsDirect();
account.updateChangeDictionaryOption();
}) })
} }
} }

View File

@ -149,9 +149,10 @@ VALUES (?, ?, ?, ?, ?)';
if ($dictionary_id !== false) { if ($dictionary_id !== false) {
$changed_dictionary = $this->dictionary->changeCurrent($id, $dictionary_id); $changed_dictionary = $this->dictionary->changeCurrent($id, $dictionary_id);
if ($changed_dictionary !== false) { if ($changed_dictionary !== false) {
$new_token = $this->generateUserToken($id, $changed_dictionary);
return array( return array(
'token' => $this->generateUserToken($id, $changed_dictionary), 'token' => $new_token,
'dictionary' => $this->getCurrentDictionary($token), 'dictionary' => $this->getCurrentDictionary($new_token),
); );
} }
} }

View File

@ -183,12 +183,12 @@ switch ($action) {
), 200); ), 200);
} }
return Response::json(array( return Response::json(array(
'data' => 'Could not create dictionary: incorrect data', 'data' => 'Could not change dictionary: incorrect data',
'error' => true, 'error' => true,
), 401); ), 401);
} }
return Response::json(array( return Response::json(array(
'data' => 'Could not create dictionary: no token provided', 'data' => 'Could not change dictionary: no token provided',
'error' => true, 'error' => true,
), 400); ), 400);
} }