Upload new, updated, and imported words/dictionaries
This commit is contained in:
		
							parent
							
								
									6a6b545979
								
							
						
					
					
						commit
						f753bd66f3
					
				
					 5 changed files with 55 additions and 28 deletions
				
			
		| 
						 | 
				
			
			@ -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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(`<a href="#${word.wordId}">${word.name}</a> 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);
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue