Create new dictionary if logged in and local has no externalId

This commit is contained in:
Robbie Antenesse 2019-05-21 16:56:11 -06:00
parent 654bfab004
commit d4d46c7afe
2 changed files with 31 additions and 16 deletions

View File

@ -2,7 +2,7 @@ import { request, saveToken } from "./helpers";
import { addMessage } from "../utilities"; import { addMessage } from "../utilities";
import { setupLogoutButton } from "./setupListeners"; import { setupLogoutButton } from "./setupListeners";
import { renderAccountSettings } from "./render"; import { renderAccountSettings } from "./render";
import { uploadWholeDictionaryAsNew } from "./sync"; import { uploadWholeDictionary } from "./sync";
export function logIn() { export function logIn() {
const email = document.getElementById('loginEmail').value.trim(), const email = document.getElementById('loginEmail').value.trim(),
@ -88,7 +88,7 @@ export function createAccount() {
}, responseData => { }, responseData => {
saveToken(responseData.token); saveToken(responseData.token);
if (responseData.hasOwnProperty('dictionary')) { if (responseData.hasOwnProperty('dictionary')) {
uploadWholeDictionaryAsNew(); // Saves external id uploadWholeDictionary(); // Saves external id
} }
return responseData; return responseData;
}, errorData => { }, errorData => {

View File

@ -1,10 +1,10 @@
import { addMessage } from "../utilities"; import { addMessage } from "../utilities";
import { saveDictionary } from "../dictionaryManagement"; import { saveDictionary } from "../dictionaryManagement";
import { request } from "./helpers"; import { request, saveToken } from "./helpers";
export function syncDictionary() { export function syncDictionary() {
if (!window.currentDictionary.hasOwnProperty('externalId')) { if (!window.currentDictionary.hasOwnProperty('externalId')) {
uploadWholeDictionaryAsNew(); uploadWholeDictionary(true);
} else { } else {
console.log('Do a sync comparison!'); console.log('Do a sync comparison!');
// request({ // 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 = { const dictionary = {
details: Object.assign({}, window.currentDictionary), details: Object.assign({}, window.currentDictionary),
words: window.currentDictionary.words, words: window.currentDictionary.words,
}; };
delete dictionary.details.words; // Ugly way to easily get the data I need. delete dictionary.details.words; // Ugly way to easily get the data I need.
request({ promise.then(() => {
action: 'set-whole-current-dictionary', request({
dictionary, action: 'set-whole-current-dictionary',
}, remoteId => { dictionary,
window.currentDictionary.externalId = remoteId; }, remoteId => {
saveDictionary(); window.currentDictionary.externalId = remoteId;
addMessage('Dictionary Uploaded Successfully'); saveDictionary();
}, errorData => { addMessage('Dictionary Uploaded Successfully');
console.error(errorData); }, errorData => {
addMessage(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) { export function syncDetails(remoteDetails) {