mirror of
https://github.com/Alamantus/Lexiconga.git
synced 2025-06-08 10:16:37 +02:00
Upload dictionary if creating an account/not previously uploaded
This commit is contained in:
parent
33ac88dd6c
commit
654bfab004
7 changed files with 133 additions and 16 deletions
|
@ -2,12 +2,17 @@ import './main.scss';
|
||||||
|
|
||||||
import setupListeners from './js/setupListeners';
|
import setupListeners from './js/setupListeners';
|
||||||
import { renderAll } from './js/render';
|
import { renderAll } from './js/render';
|
||||||
import { generateRandomWords, addMessage } from './js/utilities';
|
import { generateRandomWords, addMessage, hasToken } from './js/utilities';
|
||||||
import { loadDictionary } from './js/dictionaryManagement';
|
import { loadDictionary } from './js/dictionaryManagement';
|
||||||
import { loadSettings } from './js/settings';
|
import { loadSettings } from './js/settings';
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
addMessage('Loading...');
|
addMessage('Loading...');
|
||||||
|
if (hasToken()) {
|
||||||
|
import('./js/account/index.js').then(account => {
|
||||||
|
account.loginWithToken();
|
||||||
|
});
|
||||||
|
}
|
||||||
loadDictionary();
|
loadDictionary();
|
||||||
loadSettings();
|
loadSettings();
|
||||||
// generateRandomWords(100);
|
// generateRandomWords(100);
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
import '../../scss/Account/main.scss';
|
import '../../scss/Account/main.scss';
|
||||||
|
|
||||||
import { renderLoginForm } from "./render";
|
import { renderLoginForm } from "./render";
|
||||||
|
import { triggerLoginChanges } from './login';
|
||||||
|
import { syncDictionary } from './sync';
|
||||||
|
|
||||||
export function showLoginForm() {
|
export function showLoginForm() {
|
||||||
renderLoginForm();
|
renderLoginForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function loginWithToken() {
|
||||||
|
triggerLoginChanges();
|
||||||
|
syncDictionary();
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ import { request, saveToken } from "./helpers";
|
||||||
import { addMessage } from "../utilities";
|
import { addMessage } from "../utilities";
|
||||||
import { setupLogoutButton } from "./setupListeners";
|
import { setupLogoutButton } from "./setupListeners";
|
||||||
import { renderAccountSettings } from "./render";
|
import { renderAccountSettings } from "./render";
|
||||||
|
import { uploadWholeDictionaryAsNew } from "./sync";
|
||||||
|
|
||||||
export function logIn() {
|
export function logIn() {
|
||||||
const email = document.getElementById('loginEmail').value.trim(),
|
const email = document.getElementById('loginEmail').value.trim(),
|
||||||
|
@ -86,9 +87,13 @@ export function createAccount() {
|
||||||
},
|
},
|
||||||
}, responseData => {
|
}, responseData => {
|
||||||
saveToken(responseData.token);
|
saveToken(responseData.token);
|
||||||
|
if (responseData.hasOwnProperty('dictionary')) {
|
||||||
|
uploadWholeDictionaryAsNew(); // Saves external id
|
||||||
|
}
|
||||||
return responseData;
|
return responseData;
|
||||||
}, errorData => {
|
}, errorData => {
|
||||||
errorHTML += `<p class="bold red">${errorData}</p>`;
|
errorHTML += `<p class="bold red">${errorData}</p>`;
|
||||||
|
return errorData;
|
||||||
}).then(responseData => {
|
}).then(responseData => {
|
||||||
console.log(responseData);
|
console.log(responseData);
|
||||||
createAccountErrorMessages.innerHTML = errorHTML;
|
createAccountErrorMessages.innerHTML = errorHTML;
|
||||||
|
|
69
src/js/account/sync.js
Normal file
69
src/js/account/sync.js
Normal 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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
|
@ -135,11 +135,11 @@ WHERE user=$user AND id=$dictionary";
|
||||||
':name' => $dictionary_object['name'],
|
':name' => $dictionary_object['name'],
|
||||||
':specification' => $dictionary_object['specification'],
|
':specification' => $dictionary_object['specification'],
|
||||||
':description' => $dictionary_object['description'],
|
':description' => $dictionary_object['description'],
|
||||||
':allow_duplicates' => $dictionary_object['settings']['allowDuplicates'],
|
':allow_duplicates' => $dictionary_object['settings']['allowDuplicates'] ? 1 : 0,
|
||||||
':case_sensitive' => $dictionary_object['settings']['caseSensitive'],
|
':case_sensitive' => $dictionary_object['settings']['caseSensitive'] ? 1 : 0,
|
||||||
':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'],
|
':sort_by_definition' => $dictionary_object['settings']['sortByDefinition'] ? 1 : 0,
|
||||||
':is_complete' => $dictionary_object['settings']['isComplete'],
|
':is_complete' => $dictionary_object['settings']['isComplete'] ? 1 : 0,
|
||||||
':is_public' => $dictionary_object['settings']['isPublic'],
|
':is_public' => $dictionary_object['settings']['isPublic'] ? 1 : 0,
|
||||||
':last_updated' => $dictionary_object['lastUpdated'],
|
':last_updated' => $dictionary_object['lastUpdated'],
|
||||||
':created_on' => $dictionary_object['createdOn'],
|
':created_on' => $dictionary_object['createdOn'],
|
||||||
));
|
));
|
||||||
|
@ -148,15 +148,27 @@ WHERE user=$user AND id=$dictionary";
|
||||||
$linguistics = $dictionary_object['details'];
|
$linguistics = $dictionary_object['details'];
|
||||||
$query2 = "UPDATE dictionary_linguistics
|
$query2 = "UPDATE dictionary_linguistics
|
||||||
SET parts_of_speech=:parts_of_speech,
|
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,
|
orthography_notes=:orthography_notes,
|
||||||
grammar_notes=:grammar_notes
|
grammar_notes=:grammar_notes
|
||||||
WHERE dictionary=$dictionary";
|
WHERE dictionary=$dictionary";
|
||||||
|
|
||||||
// $result2 = $this->db->query($query2, array(
|
// $result2 = $this->db->query($query2, array(
|
||||||
$result2 = $this->db->execute($query2, array(
|
$result2 = $this->db->execute($query2, array(
|
||||||
':parts_of_speech' => json_encode($dictionary_object['partsOfSpeech']),
|
':parts_of_speech' => implode(',', $dictionary_object['partsOfSpeech']),
|
||||||
':phonology' => json_encode($linguistics['phonology']),
|
':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'],
|
':orthography_notes' => $linguistics['orthography']['notes'],
|
||||||
':grammar_notes' => $linguistics['grammar']['notes'],
|
':grammar_notes' => $linguistics['grammar']['notes'],
|
||||||
));
|
));
|
||||||
|
@ -165,10 +177,9 @@ WHERE dictionary=$dictionary";
|
||||||
if ($result2 === true) {
|
if ($result2 === true) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// return $result2->errorInfo();
|
|
||||||
}
|
}
|
||||||
// return $result1->errorInfo();
|
return $this->db->last_error_info;
|
||||||
return false;
|
// return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getWords ($user, $dictionary) {
|
public function getWords ($user, $dictionary) {
|
||||||
|
@ -206,6 +217,10 @@ WHERE dictionary=$dictionary";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setWords ($user, $dictionary, $words = array()) {
|
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 ';
|
$query = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
|
||||||
$params = array();
|
$params = array();
|
||||||
$word_ids = array();
|
$word_ids = array();
|
||||||
|
@ -247,8 +262,13 @@ last_updated=VALUES(last_updated)';
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
if ($results) {
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
return array(
|
||||||
|
'error' => $this->db->last_error_info,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function deleteWords ($dictionary, $word_ids) {
|
public function deleteWords ($dictionary, $word_ids) {
|
||||||
$insert_query = 'INSERT INTO deleted_words (dictionary, word_id, deleted_on) VALUES ';
|
$insert_query = 'INSERT INTO deleted_words (dictionary, word_id, deleted_on) VALUES ';
|
||||||
|
|
|
@ -179,7 +179,12 @@ VALUES (?, ?, ?, ?, ?)';
|
||||||
$dictionary = $user_data->dictionary;
|
$dictionary = $user_data->dictionary;
|
||||||
$details_updated = $this->dictionary->setDetails($user, $dictionary, $dictionary_data['details']);
|
$details_updated = $this->dictionary->setDetails($user, $dictionary, $dictionary_data['details']);
|
||||||
$words_updated = $this->dictionary->setWords($dictionary, $dictionary_data['words']);
|
$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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -196,12 +196,18 @@ switch ($action) {
|
||||||
if ($token !== false && isset($request['dictionary'])) {
|
if ($token !== false && isset($request['dictionary'])) {
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$dictionary_data = $user->saveWholeCurrentDictionary($token, $request['dictionary']);
|
$dictionary_data = $user->saveWholeCurrentDictionary($token, $request['dictionary']);
|
||||||
if ($dictionary_data !== false) {
|
if ($dictionary_data !== false && !isset($dictionary_data['error'])) {
|
||||||
return Response::json(array(
|
return Response::json(array(
|
||||||
'data' => 'Updated successfully',
|
'data' => $dictionary_data,
|
||||||
'error' => false,
|
'error' => false,
|
||||||
), 200);
|
), 200);
|
||||||
}
|
}
|
||||||
|
if (isset($dictionary_data['error'])) {
|
||||||
|
return Response::json(array(
|
||||||
|
'data' => $dictionary_data['message'],
|
||||||
|
'error' => true,
|
||||||
|
), 500);
|
||||||
|
}
|
||||||
return Response::json(array(
|
return Response::json(array(
|
||||||
'data' => 'Could not set dictionary: invalid token',
|
'data' => 'Could not set dictionary: invalid token',
|
||||||
'error' => true,
|
'error' => true,
|
||||||
|
|
Loading…
Add table
Reference in a new issue