Set up dictionary details sync

This commit is contained in:
Robbie Antenesse 2019-05-21 18:57:29 -06:00
parent 6aef828f42
commit e2066e6168
4 changed files with 83 additions and 17 deletions

View File

@ -0,0 +1,25 @@
import { saveEditModal } from "../dictionaryManagement";
import { syncDetails } from "./sync";
import { addMessage } from "../utilities";
export function saveEditModalAndSync() {
saveEditModal();
return syncDetails().then(successful => {
if (successful) {
addMessage('Synced Successfully');
} else {
addMessage('Could Not Sync');
}
})
.catch(err => {
console.error(err);
addMessage('Could not connect');
});
}
export function saveAndCloseEditModalAndSync() {
saveEditModalAndSync().then(() => {
document.getElementById('editModal').style.display = 'none';
});
}

View File

@ -1,6 +1,6 @@
import { request, saveToken } from "./helpers";
import { addMessage } from "../utilities";
import { setupLogoutButton } from "./setupListeners";
import { setupLogoutButton, setupEditFormButtonOverrides } from "./setupListeners";
import { renderAccountSettings } from "./render";
import { uploadWholeDictionary } from "./sync";
@ -119,6 +119,7 @@ export function triggerLoginChanges() {
loginButton.parentElement.appendChild(logoutButton);
loginButton.parentElement.removeChild(loginButton);
setupLogoutButton(logoutButton);
setupEditFormButtonOverrides();
renderAccountSettings();
}

View File

@ -1,4 +1,6 @@
import { logIn, createAccount } from "./login";
import { saveEditModal, saveAndCloseEditModal } from "../dictionaryManagement";
import { saveEditModalAndSync, saveAndCloseEditModalAndSync } from "./dictionaryManagement";
export function setupLoginModal(modal) {
const closeElements = modal.querySelectorAll('.modal-background, .close-button');
@ -19,4 +21,19 @@ export function setupLogoutButton(logoutButton) {
document.cookie = 'token=;expires=' + expire.toGMTString() + ';domain=' + document.domain + ';path=' + path; // + in front of `new Date` converts to a number
window.location.reload();
});
}
export function setupEditFormButtonOverrides() {
document.getElementById('editSave').removeEventListener('click', saveEditModal);
document.getElementById('editSave').addEventListener('click', saveEditModalAndSync);
document.getElementById('editSaveAndClose').removeEventListener('click', saveAndCloseEditModal);
document.getElementById('editSaveAndClose').addEventListener('click', saveAndCloseEditModalAndSync);
// document.getElementById('importDictionaryFile').addEventListener('change', importDictionary);
// document.getElementById('importWordsCSV').addEventListener('change', importWords);
// document.getElementById('exportDictionaryButton').addEventListener('click', exportDictionary);
// document.getElementById('exportWordsButton').addEventListener('click', exportWords);
// document.getElementById('deleteDictionaryButton').addEventListener('click', confirmDeleteDictionary);
// setupMaximizeButtons();
}

View File

@ -33,19 +33,25 @@ export function syncDictionary() {
if (!window.currentDictionary.hasOwnProperty('externalId')) {
uploadWholeDictionary(true);
} else {
console.log('Do a sync comparison!');
// request({
// action: 'get-current-dictionary',
// }, remote => {
// console.log(remote);
// syncDetails(remote.details).then(success => {
// if (success) {
// }
// });
// }, error => {
// console.error(error);
// }).catch(err => console.error(err));
addMessage('Syncing...');
request({
action: 'get-current-dictionary',
}, remote => {
console.log(remote);
const detailsSynced = syncDetails(remote.details);
if (detailsSynced === false) {
addMessage('Could not sync');
} else {
detailsSynced.then(success => {
if (success) {
console.log('Do a word comparison!');
}
});
}
}, error => {
console.error(error);
}).catch(err => console.error(err));
}
}
@ -84,10 +90,27 @@ export function uploadWholeDictionary(asNew = false) {
.catch(err => console.error('create-new-dictionary: ', err));
}
export function syncDetails(remoteDetails) {
if (remoteDetails.hasOwnProperty('lastUpdated') && remoteDetails.lastUpdated > window.currentDictionary.lastUpdated) {
export function syncDetails(remoteDetails = false) {
if (remoteDetails === false || remoteDetails.lastUpdated < window.currentDictionary.lastUpdated) {
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;
});
} else if (remoteDetails.lastUpdated > window.currentDictionary.lastUpdated) {
window.currentDictionary = Object.assign(window.currentDictionary, remoteDetails);
saveDictionary();
}
addMessage('Dictionary details synchronized');
return Promise.resolve();
}
export function syncWords(remoteWords, deletedWords) {