Set up dictionary details sync
This commit is contained in:
parent
6aef828f42
commit
e2066e6168
|
@ -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';
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import { request, saveToken } from "./helpers";
|
import { request, saveToken } from "./helpers";
|
||||||
import { addMessage } from "../utilities";
|
import { addMessage } from "../utilities";
|
||||||
import { setupLogoutButton } from "./setupListeners";
|
import { setupLogoutButton, setupEditFormButtonOverrides } from "./setupListeners";
|
||||||
import { renderAccountSettings } from "./render";
|
import { renderAccountSettings } from "./render";
|
||||||
import { uploadWholeDictionary } from "./sync";
|
import { uploadWholeDictionary } from "./sync";
|
||||||
|
|
||||||
|
@ -119,6 +119,7 @@ export function triggerLoginChanges() {
|
||||||
loginButton.parentElement.appendChild(logoutButton);
|
loginButton.parentElement.appendChild(logoutButton);
|
||||||
loginButton.parentElement.removeChild(loginButton);
|
loginButton.parentElement.removeChild(loginButton);
|
||||||
setupLogoutButton(logoutButton);
|
setupLogoutButton(logoutButton);
|
||||||
|
setupEditFormButtonOverrides();
|
||||||
|
|
||||||
renderAccountSettings();
|
renderAccountSettings();
|
||||||
}
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
import { logIn, createAccount } from "./login";
|
import { logIn, createAccount } from "./login";
|
||||||
|
import { saveEditModal, saveAndCloseEditModal } from "../dictionaryManagement";
|
||||||
|
import { saveEditModalAndSync, saveAndCloseEditModalAndSync } from "./dictionaryManagement";
|
||||||
|
|
||||||
export function setupLoginModal(modal) {
|
export function setupLoginModal(modal) {
|
||||||
const closeElements = modal.querySelectorAll('.modal-background, .close-button');
|
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
|
document.cookie = 'token=;expires=' + expire.toGMTString() + ';domain=' + document.domain + ';path=' + path; // + in front of `new Date` converts to a number
|
||||||
window.location.reload();
|
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();
|
||||||
}
|
}
|
|
@ -33,19 +33,25 @@ export function syncDictionary() {
|
||||||
if (!window.currentDictionary.hasOwnProperty('externalId')) {
|
if (!window.currentDictionary.hasOwnProperty('externalId')) {
|
||||||
uploadWholeDictionary(true);
|
uploadWholeDictionary(true);
|
||||||
} else {
|
} else {
|
||||||
console.log('Do a sync comparison!');
|
addMessage('Syncing...');
|
||||||
// request({
|
request({
|
||||||
// action: 'get-current-dictionary',
|
action: 'get-current-dictionary',
|
||||||
// }, remote => {
|
}, remote => {
|
||||||
// console.log(remote);
|
console.log(remote);
|
||||||
// syncDetails(remote.details).then(success => {
|
const detailsSynced = syncDetails(remote.details);
|
||||||
// if (success) {
|
|
||||||
|
if (detailsSynced === false) {
|
||||||
// }
|
addMessage('Could not sync');
|
||||||
// });
|
} else {
|
||||||
// }, error => {
|
detailsSynced.then(success => {
|
||||||
// console.error(error);
|
if (success) {
|
||||||
// }).catch(err => console.error(err));
|
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));
|
.catch(err => console.error('create-new-dictionary: ', err));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function syncDetails(remoteDetails) {
|
export function syncDetails(remoteDetails = false) {
|
||||||
if (remoteDetails.hasOwnProperty('lastUpdated') && remoteDetails.lastUpdated > window.currentDictionary.lastUpdated) {
|
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) {
|
export function syncWords(remoteWords, deletedWords) {
|
||||||
|
|
Loading…
Reference in New Issue