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 { 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 => {

View File

@ -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) {