Only save dictionary and settings when changed

This commit is contained in:
Robbie Antenesse 2019-07-09 17:03:52 -06:00 committed by Robbie Antenesse
parent aa956e9820
commit 56ce6c30a6
3 changed files with 81 additions and 45 deletions

View File

@ -3,7 +3,7 @@ import { renderDictionaryDetails, renderPartsOfSpeech } from "./render/details";
import { renderAll, renderTheme } from "./render"; import { renderAll, renderTheme } from "./render";
import { removeTags, cloneObject, getTimestampInSeconds, download, slugify } from "../helpers"; import { removeTags, cloneObject, getTimestampInSeconds, download, slugify } from "../helpers";
import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY } from "../constants"; import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY } from "../constants";
import { addMessage, getNextId, hasToken } from "./utilities"; import { addMessage, getNextId, hasToken, objectValuesAreDifferent } from "./utilities";
import { addWord, sortWords } from "./wordManagement"; import { addWord, sortWords } from "./wordManagement";
import { migrateDictionary } from './migration'; import { migrateDictionary } from './migration';
@ -49,48 +49,56 @@ export function openEditModal() {
} }
export function saveEditModal() { export function saveEditModal() {
window.currentDictionary.name = removeTags(document.getElementById('editName').value.trim()); const updatedDictionary = cloneObject(window.currentDictionary);
window.currentDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim()); delete updatedDictionary.words;
window.currentDictionary.description = removeTags(document.getElementById('editDescription').value.trim()); updatedDictionary.name = removeTags(document.getElementById('editName').value.trim());
window.currentDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim());
window.currentDictionary.alphabeticalOrder = document.getElementById('editAlphabeticalOrder').value.split(' ').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.description = removeTags(document.getElementById('editDescription').value.trim());
updatedDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim()).filter(val => val !== '');
updatedDictionary.alphabeticalOrder = document.getElementById('editAlphabeticalOrder').value.split(' ').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.consonants = document.getElementById('editConsonants').value.split(' ').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.phonology.consonants = document.getElementById('editConsonants').value.split(' ').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.vowels = document.getElementById('editVowels').value.split(' ').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.phonology.vowels = document.getElementById('editVowels').value.split(' ').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(' ').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(' ').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonotactics.coda = document.getElementById('editCoda').value.split(',').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.phonotactics.coda = document.getElementById('editCoda').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonotactics.notes = removeTags(document.getElementById('editPhonotacticsNotes').value.trim()); updatedDictionary.details.phonotactics.notes = removeTags(document.getElementById('editPhonotacticsNotes').value.trim());
window.currentDictionary.details.orthography.translations = document.getElementById('editTranslations').value.split('\n').map(val => val.trim()).filter(val => val !== ''); updatedDictionary.details.orthography.translations = document.getElementById('editTranslations').value.split('\n').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim()); updatedDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim());
window.currentDictionary.details.grammar.notes = removeTags(document.getElementById('editGrammar').value.trim()); updatedDictionary.details.grammar.notes = removeTags(document.getElementById('editGrammar').value.trim());
window.currentDictionary.settings.allowDuplicates = !document.getElementById('editPreventDuplicates').checked; updatedDictionary.settings.allowDuplicates = !document.getElementById('editPreventDuplicates').checked;
window.currentDictionary.settings.caseSensitive = document.getElementById('editCaseSensitive').checked; updatedDictionary.settings.caseSensitive = document.getElementById('editCaseSensitive').checked;
window.currentDictionary.settings.sortByDefinition = document.getElementById('editSortByDefinition').checked; updatedDictionary.settings.sortByDefinition = document.getElementById('editSortByDefinition').checked;
window.currentDictionary.settings.theme = document.getElementById('editTheme').value; updatedDictionary.settings.theme = document.getElementById('editTheme').value;
if (hasToken()) { if (hasToken()) {
window.currentDictionary.settings.isPublic = document.getElementById('editIsPublic').checked; updatedDictionary.settings.isPublic = document.getElementById('editIsPublic').checked;
} else { } else {
window.currentDictionary.settings.isPublic = false; updatedDictionary.settings.isPublic = false;
} }
renderTheme();
renderDictionaryDetails();
renderPartsOfSpeech();
sortWords(true);
addMessage('Saved ' + window.currentDictionary.specification + ' Successfully'); if (objectValuesAreDifferent(updatedDictionary, window.currentDictionary)) {
saveDictionary(); window.currentDictionary = Object.assign(window.currentDictionary, updatedDictionary);
if (hasToken()) { renderTheme();
import('./account/index.js').then(account => { renderDictionaryDetails();
account.uploadDetailsDirect(); renderPartsOfSpeech();
account.updateChangeDictionaryOption(); sortWords(true);
})
addMessage('Saved ' + window.currentDictionary.specification + ' Successfully');
saveDictionary();
if (hasToken()) {
import('./account/index.js').then(account => {
account.uploadDetailsDirect();
account.updateChangeDictionaryOption();
})
}
} else {
addMessage('No changes made to Dictionary Settings.');
} }
} }

View File

@ -2,7 +2,7 @@ import { SETTINGS_KEY, DEFAULT_SETTINGS } from "../constants";
import { cloneObject, removeTags } from "../helpers"; import { cloneObject, removeTags } from "../helpers";
import { usePhondueDigraphs } from "./KeyboardFire/phondue/ipaField"; import { usePhondueDigraphs } from "./KeyboardFire/phondue/ipaField";
import { renderWords } from "./render/words"; import { renderWords } from "./render/words";
import { addMessage, hasToken } from "./utilities"; import { addMessage, hasToken, objectValuesAreDifferent } from "./utilities";
import { enableHotKeys, disableHotKeys } from "./hotkeys"; import { enableHotKeys, disableHotKeys } from "./hotkeys";
export function loadSettings() { export function loadSettings() {
@ -27,9 +27,10 @@ export function openSettingsModal() {
} }
export function saveSettingsModal() { export function saveSettingsModal() {
window.settings.useIPAPronunciationField = document.getElementById('settingsUseIPA').checked; const updatedSettings = cloneObject(window.settings);
window.settings.useHotkeys = document.getElementById('settingsUseHotkeys').checked; updatedSettings.useIPAPronunciationField = document.getElementById('settingsUseIPA').checked;
window.settings.defaultTheme = document.getElementById('settingsDefaultTheme').value; updatedSettings.useHotkeys = document.getElementById('settingsUseHotkeys').checked;
updatedSettings.defaultTheme = document.getElementById('settingsDefaultTheme').value;
if (hasToken()) { if (hasToken()) {
import('./account/index.js').then(account => { import('./account/index.js').then(account => {
@ -40,19 +41,30 @@ export function saveSettingsModal() {
email = window.account.email; email = window.account.email;
emailField.value = email; emailField.value = email;
} }
window.account.email = email; const updatedAccount = cloneObject(window.account);
window.account.publicName = removeTags(publicName.value).trim(); updatedAccount.email = email;
window.account.allowEmails = document.getElementById('accountSettingsAllowEmails').checked; updatedAccount.publicName = removeTags(publicName.value).trim();
updatedAccount.allowEmails = document.getElementById('accountSettingsAllowEmails').checked;
const newPassword = document.getElementById('accountSettingsNewPassword').value; const newPassword = document.getElementById('accountSettingsNewPassword').value;
account.editAccount(Object.assign({ newPassword }, window.account)); if (objectValuesAreDifferent(updatedAccount, window.account)) {
window.account = Object.assign(window.account, updatedAccount);
account.editAccount(Object.assign({ newPassword }, window.account));
} else {
addMessage('No changes made to Account.');
}
}); });
} }
saveSettings(); if (objectValuesAreDifferent(updatedSettings, window.settings)) {
toggleHotkeysEnabled(); window.settings = Object.assign(window.settings, updatedSettings);
toggleIPAPronunciationFields(); saveSettings();
toggleHotkeysEnabled();
toggleIPAPronunciationFields();
} else {
addMessage('No changes made to Settings.');
}
} }
export function saveAndCloseSettingsModal() { export function saveAndCloseSettingsModal() {

View File

@ -176,3 +176,19 @@ export function hideAllModals() {
export function hasToken() { export function hasToken() {
return window.isOffline !== true && getCookie('token') !== ''; return window.isOffline !== true && getCookie('token') !== '';
} }
export function objectValuesAreDifferent(newObject, oldObject) {
let valuesAreDifferent = false;
for (let property in newObject) {
if (!oldObject.hasOwnProperty(property) || JSON.stringify(newObject[property]) !== JSON.stringify(oldObject[property])) {
valuesAreDifferent = true;
}
if (typeof newObject[property] === 'object' && !Array.isArray(newObject[property])) {
valuesAreDifferent = objectValuesAreDifferent(newObject[property], oldObject[property]);
}
if (valuesAreDifferent) break;
}
return valuesAreDifferent;
}