From 769c6f0f15811c117fa34406eb3d69a5579f88ab Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 6 Jun 2019 16:06:17 -0600 Subject: [PATCH] Don't upload dictionaries have not been edited --- src/js/account/sync.js | 31 ++----------------------------- src/js/account/utilities.js | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/js/account/sync.js b/src/js/account/sync.js index e9bc59e..9fd2dd0 100644 --- a/src/js/account/sync.js +++ b/src/js/account/sync.js @@ -1,41 +1,14 @@ import { addMessage } from "../utilities"; import { saveDictionary, clearDictionary } from "../dictionaryManagement"; import { request } from "./helpers"; -import { saveToken } from "./utilities"; +import { saveToken, dictionaryIsDefault } from "./utilities"; import { renderAll } from "../render"; import { sortWords } from "../wordManagement"; import { getLocalDeletedWords, clearLocalDeletedWords, saveDeletedWordsLocally } from "./utilities"; import { renderChangeDictionaryOptions } from "./render"; -/* Outline for syncing -login --> check local dictionary id - (DONE!) ? no id - -> upload dictionary - -> make new dictionary current - (Canceled) ? mismatched id - -> sync local dictionary (see 'same id' below) - -> if no matching remote id, ignore (assume deleted) - -> clear local dictionary - -> insert downloaded dictionary - (DONE!) ? same id - -> compare detail last updated timestamp - ? downloaded details are newer - -> replace local details - ? local details are newer - -> flag to upload details - -> filter deleted words from current words - -- check id and compare deletedOn with createdOn - -> compare each word and by lastUpdated/createdOn - ? downloaded word is newer - -> update local word - ? local word is newer - -> put word in an array to upload - -> upload anything that needs update - */ - export function syncDictionary(uploadAsNewIfNoExternalID = true) { - if (!window.currentDictionary.hasOwnProperty('externalID')) { + if (!window.currentDictionary.hasOwnProperty('externalID') && !dictionaryIsDefault()) { uploadWholeDictionary(uploadAsNewIfNoExternalID); } else { addMessage('Syncing...'); diff --git a/src/js/account/utilities.js b/src/js/account/utilities.js index 0bde5b1..ba59eac 100644 --- a/src/js/account/utilities.js +++ b/src/js/account/utilities.js @@ -1,11 +1,25 @@ import { setCookie } from "../StackOverflow/cookie"; import { DELETED_WORDS_LOCALSTORAGE_KEY } from "./constants"; -import { getTimestampInSeconds } from "../../helpers"; +import { getTimestampInSeconds, cloneObject } from "../../helpers"; +import { DEFAULT_DICTIONARY } from "../../constants"; export function saveToken(token) { setCookie('token', token, 30); } +export function dictionaryIsDefault() { + const defaultDictionary = cloneObject(DEFAULT_DICTIONARY); + delete defaultDictionary.lastUpdated; + delete defaultDictionary.createdOn; + delete defaultDictionary.version; + const currentDictionary = cloneObject(window.currentDictionary); + delete currentDictionary.lastUpdated; + delete currentDictionary.createdOn; + delete currentDictionary.version; + console.log(JSON.stringify(defaultDictionary) === JSON.stringify(currentDictionary)); + return JSON.stringify(defaultDictionary) === JSON.stringify(currentDictionary); +} + export function saveDeletedWordsLocally(wordIds) { let storedDeletedWords = getLocalDeletedWords(); wordIds.forEach(wordId => {