From 654bfab00401a629cd7f14f1c173af18fd472a28 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 21 May 2019 16:29:05 -0600 Subject: [PATCH] Upload dictionary if creating an account/not previously uploaded --- src/index.js | 7 +++- src/js/account/index.js | 7 ++++ src/js/account/login.js | 5 +++ src/js/account/sync.js | 69 ++++++++++++++++++++++++++++++++++++++ src/php/api/Dictionary.php | 44 +++++++++++++++++------- src/php/api/User.php | 7 +++- src/php/api/index.php | 10 ++++-- 7 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 src/js/account/sync.js diff --git a/src/index.js b/src/index.js index 68f8f34..669f53a 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/src/js/account/index.js b/src/js/account/index.js index bcb1285..f7f7fb2 100644 --- a/src/js/account/index.js +++ b/src/js/account/index.js @@ -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(); } \ No newline at end of file diff --git a/src/js/account/login.js b/src/js/account/login.js index 47e4e18..69361a5 100644 --- a/src/js/account/login.js +++ b/src/js/account/login.js @@ -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 += `

${errorData}

`; + return errorData; }).then(responseData => { console.log(responseData); createAccountErrorMessages.innerHTML = errorHTML; diff --git a/src/js/account/sync.js b/src/js/account/sync.js new file mode 100644 index 0000000..d9f3625 --- /dev/null +++ b/src/js/account/sync.js @@ -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) { + + } + }); +} \ No newline at end of file diff --git a/src/php/api/Dictionary.php b/src/php/api/Dictionary.php index cc98718..0e01890 100644 --- a/src/php/api/Dictionary.php +++ b/src/php/api/Dictionary.php @@ -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) { diff --git a/src/php/api/User.php b/src/php/api/User.php index 89d1379..bc1b5b1 100644 --- a/src/php/api/User.php +++ b/src/php/api/User.php @@ -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; } diff --git a/src/php/api/index.php b/src/php/api/index.php index a9e6cff..aff5717 100644 --- a/src/php/api/index.php +++ b/src/php/api/index.php @@ -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,