Upload dictionary if creating an account/not previously uploaded

This commit is contained in:
Robbie Antenesse 2019-05-21 16:29:05 -06:00
parent 33ac88dd6c
commit 654bfab004
7 changed files with 133 additions and 16 deletions

View File

@ -2,12 +2,17 @@ import './main.scss';
import setupListeners from './js/setupListeners';
import { renderAll } from './js/render';
import { generateRandomWords, addMessage } from './js/utilities';
import { generateRandomWords, addMessage, hasToken } from './js/utilities';
import { loadDictionary } from './js/dictionaryManagement';
import { loadSettings } from './js/settings';
function initialize() {
addMessage('Loading...');
if (hasToken()) {
import('./js/account/index.js').then(account => {
account.loginWithToken();
});
}
loadDictionary();
loadSettings();
// generateRandomWords(100);

View File

@ -1,7 +1,14 @@
import '../../scss/Account/main.scss';
import { renderLoginForm } from "./render";
import { triggerLoginChanges } from './login';
import { syncDictionary } from './sync';
export function showLoginForm() {
renderLoginForm();
}
export function loginWithToken() {
triggerLoginChanges();
syncDictionary();
}

View File

@ -2,6 +2,7 @@ import { request, saveToken } from "./helpers";
import { addMessage } from "../utilities";
import { setupLogoutButton } from "./setupListeners";
import { renderAccountSettings } from "./render";
import { uploadWholeDictionaryAsNew } from "./sync";
export function logIn() {
const email = document.getElementById('loginEmail').value.trim(),
@ -86,9 +87,13 @@ export function createAccount() {
},
}, responseData => {
saveToken(responseData.token);
if (responseData.hasOwnProperty('dictionary')) {
uploadWholeDictionaryAsNew(); // Saves external id
}
return responseData;
}, errorData => {
errorHTML += `<p class="bold red">${errorData}</p>`;
return errorData;
}).then(responseData => {
console.log(responseData);
createAccountErrorMessages.innerHTML = errorHTML;

69
src/js/account/sync.js Normal file
View File

@ -0,0 +1,69 @@
import { addMessage } from "../utilities";
import { saveDictionary } from "../dictionaryManagement";
import { request } from "./helpers";
export function syncDictionary() {
if (!window.currentDictionary.hasOwnProperty('externalId')) {
uploadWholeDictionaryAsNew();
} 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));
}
}
export function uploadWholeDictionaryAsNew() {
const dictionary = {
details: Object.assign({}, window.currentDictionary),
words: window.currentDictionary.words,
};
delete dictionary.details.words; // Ugly way to easily get the data I need.
request({
action: 'set-whole-current-dictionary',
dictionary,
}, remoteId => {
window.currentDictionary.externalId = remoteId;
saveDictionary();
addMessage('Dictionary Uploaded Successfully');
}, errorData => {
console.error(errorData);
addMessage(errorData);
})
.catch(err => console.error(err));
}
export function syncDetails(remoteDetails) {
if (remoteDetails.hasOwnProperty('lastUpdated') && remoteDetails.lastUpdated > window.currentDictionary.lastUpdated) {
}
}
export function syncWords(remoteWords, deletedWords) {
const words = window.currentDictionary.words.filter(word => {
const deleted = deletedWords.find(deletedWord => deletedWord.id === word.wordId);
if (deleted) {
return deleted.deletedOn < word.createdOn;
}
return true;
});
const newLocalWords = words.filter(word => {
const remote = remoteWords.find(remoteWord => remoteWord.id === word.wordId);
return typeof remote === 'undefined';
});
remoteWords.forEach(remoteWord => {
const localWord = words.find(word => word.wordId === remoteWord.wordId);
if (localWord) {
}
});
}

View File

@ -135,11 +135,11 @@ WHERE user=$user AND id=$dictionary";
':name' => $dictionary_object['name'],
':specification' => $dictionary_object['specification'],
':description' => $dictionary_object['description'],
':allow_duplicates' => $dictionary_object['settings']['allowDuplicates'],
':case_sensitive' => $dictionary_object['settings']['caseSensitive'],
':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'],
':is_complete' => $dictionary_object['settings']['isComplete'],
':is_public' => $dictionary_object['settings']['isPublic'],
':allow_duplicates' => $dictionary_object['settings']['allowDuplicates'] ? 1 : 0,
':case_sensitive' => $dictionary_object['settings']['caseSensitive'] ? 1 : 0,
':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'] ? 1 : 0,
':is_complete' => $dictionary_object['settings']['isComplete'] ? 1 : 0,
':is_public' => $dictionary_object['settings']['isPublic'] ? 1 : 0,
':last_updated' => $dictionary_object['lastUpdated'],
':created_on' => $dictionary_object['createdOn'],
));
@ -148,15 +148,27 @@ WHERE user=$user AND id=$dictionary";
$linguistics = $dictionary_object['details'];
$query2 = "UPDATE dictionary_linguistics
SET parts_of_speech=:parts_of_speech,
phonology=:phonology,
consonants=:consonants,
vowels=:vowels,
blends=:blends,
onset=:onset,
nucleus=:nucleus,
coda=:coda,
exceptions=:exceptions,
orthography_notes=:orthography_notes,
grammar_notes=:grammar_notes
WHERE dictionary=$dictionary";
// $result2 = $this->db->query($query2, array(
$result2 = $this->db->execute($query2, array(
':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']),
':phonology' => json_encode($linguistics['phonology']),
':parts_of_speech' => implode(',', $dictionary_object['partsOfSpeech']),
':consonants' => implode(' ', $linguistics['phonology']['consonants']),
':vowels' => implode(' ', $linguistics['phonology']['vowels']),
':blends' => implode(' ', $linguistics['phonology']['blends']),
':onset' => implode(',', $linguistics['phonology']['phonotactics']['onset']),
':nucleus' => implode(',', $linguistics['phonology']['phonotactics']['nucleus']),
':coda' => implode(',', $linguistics['phonology']['phonotactics']['coda']),
':exceptions' => $linguistics['phonology']['phonotactics']['exceptions'],
':orthography_notes' => $linguistics['orthography']['notes'],
':grammar_notes' => $linguistics['grammar']['notes'],
));
@ -165,10 +177,9 @@ WHERE dictionary=$dictionary";
if ($result2 === true) {
return true;
}
// return $result2->errorInfo();
}
// return $result1->errorInfo();
return false;
return $this->db->last_error_info;
// return false;
}
public function getWords ($user, $dictionary) {
@ -206,6 +217,10 @@ WHERE dictionary=$dictionary";
}
public function setWords ($user, $dictionary, $words = array()) {
if (count($words) < 1) {
return true;
}
$query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
$params = array();
$word_ids = array();
@ -247,7 +262,12 @@ last_updated=VALUES(last_updated)';
// }
// }
return $results;
if ($results) {
return $results;
}
return array(
'error' => $this->db->last_error_info,
);
}
public function deleteWords ($dictionary, $word_ids) {

View File

@ -179,7 +179,12 @@ VALUES (?, ?, ?, ?, ?)';
$dictionary = $user_data->dictionary;
$details_updated = $this->dictionary->setDetails($user, $dictionary, $dictionary_data['details']);
$words_updated = $this->dictionary->setWords($dictionary, $dictionary_data['words']);
return $details_updated && $words_updated;
if ($details_updated === true && $words_updated === true) {
return $this->token->hash($dictionary);
}
return array(
'error' => ($details_updated !== true ? $details_updated . ' ' : '') . ($words_updated !== true ? $words_updated : ''),
);
}
return false;
}

View File

@ -196,12 +196,18 @@ switch ($action) {
if ($token !== false && isset($request['dictionary'])) {
$user = new User();
$dictionary_data = $user->saveWholeCurrentDictionary($token, $request['dictionary']);
if ($dictionary_data !== false) {
if ($dictionary_data !== false && !isset($dictionary_data['error'])) {
return Response::json(array(
'data' => 'Updated successfully',
'data' => $dictionary_data,
'error' => false,
), 200);
}
if (isset($dictionary_data['error'])) {
return Response::json(array(
'data' => $dictionary_data['message'],
'error' => true,
), 500);
}
return Response::json(array(
'data' => 'Could not set dictionary: invalid token',
'error' => true,