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 { renderLoginForm } from "./render";
import { triggerLoginChanges } from './login'; import { triggerLoginChanges } from './login';
import { syncDictionary } from './sync'; import { syncDictionary, uploadWords } from './sync';
export function showLoginForm() { export function showLoginForm() {
renderLoginForm(); renderLoginForm();
@ -12,3 +12,7 @@ export function loginWithToken() {
triggerLoginChanges(); triggerLoginChanges();
syncDictionary(); syncDictionary();
} }
export function uploadWord(word) {
uploadWords([word]);
}

View File

@ -2,6 +2,7 @@ import { addMessage } from "../utilities";
import { saveDictionary, clearDictionary } from "../dictionaryManagement"; import { saveDictionary, clearDictionary } from "../dictionaryManagement";
import { request, saveToken } from "./helpers"; import { request, saveToken } from "./helpers";
import { renderAll } from "../render"; import { renderAll } from "../render";
import { sortWords } from "../wordManagement";
/* Outline for syncing /* Outline for syncing
login login
@ -117,17 +118,7 @@ export function syncDetails(remoteDetails = false) {
if (direction === 'up') { if (direction === 'up') {
const details = Object.assign({}, window.currentDictionary); const details = Object.assign({}, window.currentDictionary);
delete details.words; delete details.words;
return request({ return uploadDetails(details);
action: 'set-dictionary-details',
details,
}, successful => {
addMessage('Saved Details to Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not sync dictionary');
return false;
});
} else if (direction === 'down') { } else if (direction === 'down') {
window.currentDictionary = Object.assign(window.currentDictionary, remoteDetails); window.currentDictionary = Object.assign(window.currentDictionary, remoteDetails);
saveDictionary(); saveDictionary();
@ -136,6 +127,20 @@ export function syncDetails(remoteDetails = false) {
return Promise.resolve(true); 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) { export function syncWords(remoteWords, deletedWords) {
const words = window.currentDictionary.words.filter(word => { const words = window.currentDictionary.words.filter(word => {
const deleted = deletedWords.find(deletedWord => deletedWord.id === word.wordId); const deleted = deletedWords.find(deletedWord => deletedWord.id === word.wordId);
@ -166,22 +171,27 @@ export function syncWords(remoteWords, deletedWords) {
}); });
window.currentDictionary.words = words; window.currentDictionary.words = words;
sortWords();
saveDictionary(); saveDictionary();
if (localWordsToUpload.length > 0) { if (localWordsToUpload.length > 0) {
return request({ return uploadWords(words);
action: 'set-dictionary-words',
words,
}, successful => {
addMessage('Saved Words to Server');
return successful;
}, error => {
console.error(error);
addMessage('Could not sync words');
return false;
});
} }
addMessage('Words synchronized'); addMessage('Words synchronized');
return Promise.resolve(true); 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;
});
}