From 56ce6c30a658142459c9d5d8cccee5fca4195c8a Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 9 Jul 2019 17:03:52 -0600 Subject: [PATCH] Only save dictionary and settings when changed --- src/js/dictionaryManagement.js | 76 +++++++++++++++++++--------------- src/js/settings.js | 34 ++++++++++----- src/js/utilities.js | 16 +++++++ 3 files changed, 81 insertions(+), 45 deletions(-) diff --git a/src/js/dictionaryManagement.js b/src/js/dictionaryManagement.js index 373b8ce..a5d2e90 100644 --- a/src/js/dictionaryManagement.js +++ b/src/js/dictionaryManagement.js @@ -3,7 +3,7 @@ import { renderDictionaryDetails, renderPartsOfSpeech } from "./render/details"; import { renderAll, renderTheme } from "./render"; import { removeTags, cloneObject, getTimestampInSeconds, download, slugify } from "../helpers"; 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 { migrateDictionary } from './migration'; @@ -49,48 +49,56 @@ export function openEditModal() { } export function saveEditModal() { - window.currentDictionary.name = removeTags(document.getElementById('editName').value.trim()); - window.currentDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim()); - window.currentDictionary.description = removeTags(document.getElementById('editDescription').value.trim()); - window.currentDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim()).filter(val => val !== ''); - window.currentDictionary.alphabeticalOrder = document.getElementById('editAlphabeticalOrder').value.split(' ').map(val => val.trim()).filter(val => val !== ''); + const updatedDictionary = cloneObject(window.currentDictionary); + delete updatedDictionary.words; + updatedDictionary.name = removeTags(document.getElementById('editName').value.trim()); + updatedDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim()); + 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 !== ''); - window.currentDictionary.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 !== ''); - window.currentDictionary.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 !== ''); - window.currentDictionary.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.phonology.consonants = document.getElementById('editConsonants').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 !== ''); + updatedDictionary.details.phonology.blends = document.getElementById('editBlends').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 !== ''); + updatedDictionary.details.phonotactics.nucleus = document.getElementById('editNucleus').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 !== ''); + 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 !== ''); - window.currentDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim()); - window.currentDictionary.details.grammar.notes = removeTags(document.getElementById('editGrammar').value.trim()); + updatedDictionary.details.orthography.translations = document.getElementById('editTranslations').value.split('\n').map(val => val.trim()).filter(val => val !== ''); + updatedDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim()); + updatedDictionary.details.grammar.notes = removeTags(document.getElementById('editGrammar').value.trim()); - window.currentDictionary.settings.allowDuplicates = !document.getElementById('editPreventDuplicates').checked; - window.currentDictionary.settings.caseSensitive = document.getElementById('editCaseSensitive').checked; - window.currentDictionary.settings.sortByDefinition = document.getElementById('editSortByDefinition').checked; - window.currentDictionary.settings.theme = document.getElementById('editTheme').value; + updatedDictionary.settings.allowDuplicates = !document.getElementById('editPreventDuplicates').checked; + updatedDictionary.settings.caseSensitive = document.getElementById('editCaseSensitive').checked; + updatedDictionary.settings.sortByDefinition = document.getElementById('editSortByDefinition').checked; + updatedDictionary.settings.theme = document.getElementById('editTheme').value; if (hasToken()) { - window.currentDictionary.settings.isPublic = document.getElementById('editIsPublic').checked; + updatedDictionary.settings.isPublic = document.getElementById('editIsPublic').checked; } else { - window.currentDictionary.settings.isPublic = false; + updatedDictionary.settings.isPublic = false; } - - renderTheme(); - renderDictionaryDetails(); - renderPartsOfSpeech(); - sortWords(true); - addMessage('Saved ' + window.currentDictionary.specification + ' Successfully'); - saveDictionary(); + if (objectValuesAreDifferent(updatedDictionary, window.currentDictionary)) { + window.currentDictionary = Object.assign(window.currentDictionary, updatedDictionary); - if (hasToken()) { - import('./account/index.js').then(account => { - account.uploadDetailsDirect(); - account.updateChangeDictionaryOption(); - }) + renderTheme(); + renderDictionaryDetails(); + renderPartsOfSpeech(); + 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.'); } } diff --git a/src/js/settings.js b/src/js/settings.js index 26c9bff..8276d1c 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -2,7 +2,7 @@ import { SETTINGS_KEY, DEFAULT_SETTINGS } from "../constants"; import { cloneObject, removeTags } from "../helpers"; import { usePhondueDigraphs } from "./KeyboardFire/phondue/ipaField"; import { renderWords } from "./render/words"; -import { addMessage, hasToken } from "./utilities"; +import { addMessage, hasToken, objectValuesAreDifferent } from "./utilities"; import { enableHotKeys, disableHotKeys } from "./hotkeys"; export function loadSettings() { @@ -27,9 +27,10 @@ export function openSettingsModal() { } export function saveSettingsModal() { - window.settings.useIPAPronunciationField = document.getElementById('settingsUseIPA').checked; - window.settings.useHotkeys = document.getElementById('settingsUseHotkeys').checked; - window.settings.defaultTheme = document.getElementById('settingsDefaultTheme').value; + const updatedSettings = cloneObject(window.settings); + updatedSettings.useIPAPronunciationField = document.getElementById('settingsUseIPA').checked; + updatedSettings.useHotkeys = document.getElementById('settingsUseHotkeys').checked; + updatedSettings.defaultTheme = document.getElementById('settingsDefaultTheme').value; if (hasToken()) { import('./account/index.js').then(account => { @@ -40,19 +41,30 @@ export function saveSettingsModal() { email = window.account.email; emailField.value = email; } - window.account.email = email; - window.account.publicName = removeTags(publicName.value).trim(); - window.account.allowEmails = document.getElementById('accountSettingsAllowEmails').checked; + const updatedAccount = cloneObject(window.account); + updatedAccount.email = email; + updatedAccount.publicName = removeTags(publicName.value).trim(); + updatedAccount.allowEmails = document.getElementById('accountSettingsAllowEmails').checked; 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(); - toggleHotkeysEnabled(); - toggleIPAPronunciationFields(); + if (objectValuesAreDifferent(updatedSettings, window.settings)) { + window.settings = Object.assign(window.settings, updatedSettings); + saveSettings(); + toggleHotkeysEnabled(); + toggleIPAPronunciationFields(); + } else { + addMessage('No changes made to Settings.'); + } } export function saveAndCloseSettingsModal() { diff --git a/src/js/utilities.js b/src/js/utilities.js index 0c02534..a82f4b3 100644 --- a/src/js/utilities.js +++ b/src/js/utilities.js @@ -176,3 +176,19 @@ export function hideAllModals() { export function hasToken() { 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; +}