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();
@ -11,4 +11,8 @@ export function showLoginForm() {
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,17 +118,7 @@ export function syncDetails(remoteDetails = false) {
if (direction === 'up') {
const details = Object.assign({}, window.currentDictionary);
delete details.words;
return request({
action: 'set-dictionary-details',
details,
}, successful => {
addMessage('Saved Details to Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not sync dictionary');
return false;
});
return uploadDetails(details);
} else if (direction === 'down') {
window.currentDictionary = Object.assign(window.currentDictionary, remoteDetails);
saveDictionary();
@ -136,6 +127,20 @@ export function syncDetails(remoteDetails = false) {
return Promise.resolve(true);
}
export function uploadDetails(details) {
return request({
action: 'set-dictionary-details',
details,
}, successful => {
addMessage('Saved Details to Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not sync dictionary');
return false;
});
}
export function syncWords(remoteWords, deletedWords) {
const words = window.currentDictionary.words.filter(word => {
const deleted = deletedWords.find(deletedWord => deletedWord.id === word.wordId);
@ -166,22 +171,27 @@ export function syncWords(remoteWords, deletedWords) {
});
window.currentDictionary.words = words;
sortWords();
saveDictionary();
if (localWordsToUpload.length > 0) {
return request({
action: 'set-dictionary-words',
words,
}, successful => {
addMessage('Saved Words to Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not sync words');
return false;
});
return uploadWords(words);
}
addMessage('Words synchronized');
return Promise.resolve(true);
}
export function uploadWords(words) {
return request({
action: 'set-dictionary-words',
words,
}, successful => {
addMessage('Saved Words to Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not upload words');
return false;
});
}