From f753bd66f3a3ec979aa597723afa0e63a42172a8 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 23 May 2019 12:03:25 -0600 Subject: [PATCH] Upload new, updated, and imported words/dictionaries --- src/js/account/index.js | 14 +++++++++++++- src/js/account/login.js | 3 +-- src/js/account/setupListeners.js | 17 ----------------- src/js/dictionaryManagement.js | 31 +++++++++++++++++++++++++------ src/js/wordManagement.js | 18 ++++++++++++++++-- 5 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/js/account/index.js b/src/js/account/index.js index b3ce222..811db2a 100644 --- a/src/js/account/index.js +++ b/src/js/account/index.js @@ -2,7 +2,7 @@ import '../../scss/Account/main.scss'; import { renderLoginForm } from "./render"; import { triggerLoginChanges } from './login'; -import { syncDictionary, uploadWords } from './sync'; +import { syncDictionary, uploadWords, uploadDetails, uploadWholeDictionary } from './sync'; export function showLoginForm() { renderLoginForm(); @@ -13,6 +13,18 @@ export function loginWithToken() { syncDictionary(); } +export function syncImportedDictionary() { + uploadWholeDictionary(true); +} + +export function uploadDetailsDirect() { + uploadDetails(); +} + export function uploadWord(word) { uploadWords([word]); +} + +export function syncImportedWords(words) { + uploadWords(words); } \ No newline at end of file diff --git a/src/js/account/login.js b/src/js/account/login.js index dc9b436..ae3e35c 100644 --- a/src/js/account/login.js +++ b/src/js/account/login.js @@ -1,6 +1,6 @@ import { request, saveToken } from "./helpers"; import { addMessage } from "../utilities"; -import { setupLogoutButton, setupEditFormButtonOverrides } from "./setupListeners"; +import { setupLogoutButton } from "./setupListeners"; import { renderAccountSettings } from "./render"; import { uploadWholeDictionary } from "./sync"; @@ -119,7 +119,6 @@ export function triggerLoginChanges() { loginButton.parentElement.appendChild(logoutButton); loginButton.parentElement.removeChild(loginButton); setupLogoutButton(logoutButton); - setupEditFormButtonOverrides(); renderAccountSettings(); } \ No newline at end of file diff --git a/src/js/account/setupListeners.js b/src/js/account/setupListeners.js index 19e9292..7934ac6 100644 --- a/src/js/account/setupListeners.js +++ b/src/js/account/setupListeners.js @@ -1,6 +1,4 @@ import { logIn, createAccount } from "./login"; -import { saveEditModal, saveAndCloseEditModal } from "../dictionaryManagement"; -import { saveEditModalAndSync, saveAndCloseEditModalAndSync } from "./dictionaryManagement"; import { setCookie } from "../StackOverflow/cookie"; export function setupLoginModal(modal) { @@ -21,18 +19,3 @@ export function setupLogoutButton(logoutButton) { 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(); -} diff --git a/src/js/dictionaryManagement.js b/src/js/dictionaryManagement.js index 80d2862..e497fe0 100644 --- a/src/js/dictionaryManagement.js +++ b/src/js/dictionaryManagement.js @@ -1,7 +1,7 @@ import { renderDictionaryDetails, renderPartsOfSpeech, renderAll } from "./render"; import { removeTags, cloneObject, getTimestampInSeconds, download, slugify } from "../helpers"; import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY, MIGRATE_VERSION } from "../constants"; -import { addMessage, getNextId } from "./utilities"; +import { addMessage, getNextId, hasToken } from "./utilities"; import { addWord } from "./wordManagement"; export function updateDictionary () { @@ -68,6 +68,12 @@ export function saveEditModal() { saveDictionary(); renderDictionaryDetails(); renderPartsOfSpeech(); + + if (hasToken()) { + import('./account/index.js').then(account => { + account.uploadDetailsDirect(); + }) + } } export function saveAndCloseEditModal() { @@ -131,6 +137,12 @@ export function importDictionary() { importDictionaryField.value = ''; document.getElementById('editModal').style.display = 'none'; addMessage('Dictionary Imported Successfully'); + + if (hasToken()) { + import('./account/index.js').then(account => { + account.syncImportedDictionary(); + }); + } } else { addMessage('Dictionary could not be imported', 10000); } @@ -148,7 +160,7 @@ export function importWords() { if (confirm('Importing a CSV file with words will add all of the words in the file to your dictionary regardless of duplication!\nDo you want to continue?')) { addMessage('Importing words...'); import('papaparse').then(papa => { - let wordsImported = 0; + const importedWords = []; papa.parse(importWordsField.files[0], { header: true, encoding: "utf-8", @@ -160,15 +172,16 @@ export function importWords() { }); } else { const row = results.data[0]; - addWord({ + const importedWord = addWord({ name: removeTags(row.word).trim(), pronunciation: removeTags(row.pronunciation).trim(), partOfSpeech: removeTags(row['part of speech']).trim(), definition: removeTags(row.definition).trim(), details: removeTags(row.explanation).trim(), wordId: getNextId(), - }, false, false); - wordsImported++; + }, false, false, false); + + importedWords.push(importedWord); } }, complete: () => { @@ -176,7 +189,13 @@ export function importWords() { renderAll(); importWordsField.value = ''; document.getElementById('editModal').style.display = 'none'; - addMessage(`Done Importing ${wordsImported} Words`); + addMessage(`Done Importing ${importedWords.length} Words`); + + if (hasToken()) { + import('./account/index.js').then(account => { + account.syncImportedWords(importedWords); + }); + } }, error: err => { addMessage('Error Importing Words: ' + err); diff --git a/src/js/wordManagement.js b/src/js/wordManagement.js index 72ae7ea..3a230ef 100644 --- a/src/js/wordManagement.js +++ b/src/js/wordManagement.js @@ -1,5 +1,5 @@ import { renderWords } from "./render"; -import { wordExists, addMessage, getNextId } from "./utilities"; +import { wordExists, addMessage, getNextId, hasToken } from "./utilities"; import removeDiacritics from "./StackOverflow/removeDiacritics"; import { removeTags, getTimestampInSeconds } from "../helpers"; import { saveDictionary } from "./dictionaryManagement"; @@ -78,7 +78,7 @@ export function clearWordForm() { document.getElementById('wordName').focus(); } -export function addWord(word, render = true, message = true) { +export function addWord(word, render = true, message = true, upload = true) { const timestamp = getTimestampInSeconds(); word.lastUpdated = timestamp; word.createdOn = timestamp; @@ -87,6 +87,14 @@ export function addWord(word, render = true, message = true) { addMessage(`${word.name} Created Successfully`, 10000); } sortWords(render); + + if (upload && hasToken()) { + import('./account/index.js').then(account => { + account.uploadWord(word); + }); + } + + return word; } export function deleteWord(wordId) { @@ -111,6 +119,12 @@ export function updateWord(word, wordId) { window.currentDictionary.words[wordIndex] = word; addMessage('Word Updated Successfully'); sortWords(true); + + if (hasToken()) { + import('./account/index.js').then(account => { + account.uploadWord(word); + }); + } } }