diff --git a/src/js/account/login.js b/src/js/account/login.js index 69361a5..ae3e35c 100644 --- a/src/js/account/login.js +++ b/src/js/account/login.js @@ -2,7 +2,7 @@ import { request, saveToken } from "./helpers"; import { addMessage } from "../utilities"; import { setupLogoutButton } from "./setupListeners"; import { renderAccountSettings } from "./render"; -import { uploadWholeDictionaryAsNew } from "./sync"; +import { uploadWholeDictionary } from "./sync"; export function logIn() { const email = document.getElementById('loginEmail').value.trim(), @@ -88,7 +88,7 @@ export function createAccount() { }, responseData => { saveToken(responseData.token); if (responseData.hasOwnProperty('dictionary')) { - uploadWholeDictionaryAsNew(); // Saves external id + uploadWholeDictionary(); // Saves external id } return responseData; }, errorData => { diff --git a/src/js/account/sync.js b/src/js/account/sync.js index d9f3625..d7adab5 100644 --- a/src/js/account/sync.js +++ b/src/js/account/sync.js @@ -1,10 +1,10 @@ import { addMessage } from "../utilities"; import { saveDictionary } from "../dictionaryManagement"; -import { request } from "./helpers"; +import { request, saveToken } from "./helpers"; export function syncDictionary() { if (!window.currentDictionary.hasOwnProperty('externalId')) { - uploadWholeDictionaryAsNew(); + uploadWholeDictionary(true); } else { console.log('Do a sync comparison!'); // request({ @@ -22,24 +22,39 @@ export function syncDictionary() { } } -export function uploadWholeDictionaryAsNew() { +export function uploadWholeDictionary(asNew = false) { + let promise; + if (asNew) { + promise = request({ + action: 'create-new-dictionary', + }, successData => { + saveToken(successData.token); + }, errorData => { + console.error(errorData); + }); + } else { + promise = Promise.resolve(); + } const dictionary = { details: Object.assign({}, window.currentDictionary), words: window.currentDictionary.words, }; delete dictionary.details.words; // Ugly way to easily get the data I need. - request({ - action: 'set-whole-current-dictionary', - dictionary, - }, remoteId => { - window.currentDictionary.externalId = remoteId; - saveDictionary(); - addMessage('Dictionary Uploaded Successfully'); - }, errorData => { - console.error(errorData); - addMessage(errorData); + promise.then(() => { + request({ + action: 'set-whole-current-dictionary', + dictionary, + }, remoteId => { + window.currentDictionary.externalId = remoteId; + saveDictionary(); + addMessage('Dictionary Uploaded Successfully'); + }, errorData => { + console.error(errorData); + addMessage(errorData); + }) + .catch(err => console.error('set-whole-current-dictionary: ', err)); }) - .catch(err => console.error(err)); + .catch(err => console.error('create-new-dictionary: ', err)); } export function syncDetails(remoteDetails) {