Split upload functions; sort words after sync

This commit is contained in:
Robbie Antenesse 2019-05-22 20:22:57 -06:00
parent 3fa0ac2a81
commit 53d808fe30
2 changed files with 37 additions and 23 deletions

View File

@ -2,7 +2,7 @@ import '../../scss/Account/main.scss';
import { renderLoginForm } from "./render";
import { triggerLoginChanges } from './login';
import { syncDictionary } from './sync';
import { syncDictionary, uploadWords } from './sync';
export function showLoginForm() {
renderLoginForm();
@ -12,3 +12,7 @@ export function loginWithToken() {
triggerLoginChanges();
syncDictionary();
}
export function uploadWord(word) {
uploadWords([word]);
}

View File

@ -2,6 +2,7 @@ import { addMessage } from "../utilities";
import { saveDictionary, clearDictionary } from "../dictionaryManagement";
import { request, saveToken } from "./helpers";
import { renderAll } from "../render";
import { sortWords } from "../wordManagement";
/* Outline for syncing
login
@ -117,6 +118,16 @@ export function syncDetails(remoteDetails = false) {
if (direction === 'up') {
const details = Object.assign({}, window.currentDictionary);
delete details.words;
return uploadDetails(details);
} else if (direction === 'down') {
window.currentDictionary = Object.assign(window.currentDictionary, remoteDetails);
saveDictionary();
}
addMessage('Dictionary details synchronized');
return Promise.resolve(true);
}
export function uploadDetails(details) {
return request({
action: 'set-dictionary-details',
details,
@ -128,12 +139,6 @@ export function syncDetails(remoteDetails = false) {
addMessage('Could not sync dictionary');
return false;
});
} else if (direction === 'down') {
window.currentDictionary = Object.assign(window.currentDictionary, remoteDetails);
saveDictionary();
}
addMessage('Dictionary details synchronized');
return Promise.resolve(true);
}
export function syncWords(remoteWords, deletedWords) {
@ -166,9 +171,18 @@ export function syncWords(remoteWords, deletedWords) {
});
window.currentDictionary.words = words;
sortWords();
saveDictionary();
if (localWordsToUpload.length > 0) {
return uploadWords(words);
}
addMessage('Words synchronized');
return Promise.resolve(true);
}
export function uploadWords(words) {
return request({
action: 'set-dictionary-words',
words,
@ -177,11 +191,7 @@ export function syncWords(remoteWords, deletedWords) {
return successful;
}, error => {
console.error(error);
addMessage('Could not sync words');
addMessage('Could not upload words');
return false;
});
}
addMessage('Words synchronized');
return Promise.resolve(true);
}