From e6916f0f245255153c0d1aba5270e7ca46539e6f Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 1 Jul 2018 16:08:01 -0600 Subject: [PATCH] Enable creating new dictionaries from frontend Also change how dictionaries are created, i.e. set random unique id so exposing them without hashing is ok. This is to allow displaying public dictionaries the same way --- public/api/Dictionary.php | 27 ++++++++++++----- public/api/User.php | 5 ++-- .../management/AccountManager/MyAccount.jsx | 13 ++++++-- .../management/AccountManager/index.jsx | 2 ++ src/components/structure/Header.jsx | 5 +++- src/index.jsx | 1 + src/managers/DictionaryData.js | 30 +++++++++++++++++++ 7 files changed, 70 insertions(+), 13 deletions(-) diff --git a/public/api/Dictionary.php b/public/api/Dictionary.php index c0f8075..c3ea484 100644 --- a/public/api/Dictionary.php +++ b/public/api/Dictionary.php @@ -26,22 +26,33 @@ class Dictionary { ); } + private function checkIfIdExists ($id) { + $query = "SELECT id FROM dictionaries WHERE id=?"; + $results = $this->db->query($query, array($id))->fetchAll(); + return count($results) > 0; + } + public function create ($user) { - $insert_dictionary_query = "INSERT INTO dictionaries (user, created_on) VALUES (?, ?)"; - $insert_dictionary = $this->db->execute($insert_dictionary_query, array($user, time())); + $new_id = mt_rand(1000, 999999999); + $id_exists = $this->checkIfIdExists($new_id); + while ($id_exists) { + $new_id = mt_rand(1000, 999999999); + $id_exists = $this->checkIfIdExists($new_id); + } + + $insert_dictionary_query = "INSERT INTO dictionaries (id, user, created_on) VALUES (?, ?, ?)"; + $insert_dictionary = $this->db->execute($insert_dictionary_query, array($new_id, $user, time())); if ($insert_dictionary === true) { - $new_dictionary_id = $this->db->lastInsertId(); - $insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary, parts_of_speech, phonology) -VALUES ($new_dictionary_id, ?, ?)"; +VALUES ($new_id, ?, ?)"; $insert_linguistics = $this->db->execute($insert_linguistics_query, array( json_encode($this->defaults['partsOfSpeech']), json_encode($this->defaults['phonology']), )); if ($insert_linguistics === true) { - return $this->changeCurrent($user, $new_dictionary_id); + return $this->changeCurrent($user, $new_id); } else { return array( 'error' => '"INSERT INTO dictionary_linguistics" failed: ' . $this->db->last_error_info[2], @@ -69,7 +80,7 @@ VALUES ($new_dictionary_id, ?, ?)"; if ($results) { return array_map(function($result) { return array( - 'id' => $this->token->hash($result['id']), + 'id' => $result['id'], 'name' => $result['name'] . ' ' . $result['specification'], ); }, $results); @@ -86,7 +97,7 @@ VALUES ($new_dictionary_id, ?, ?)"; $phonology = $result['phonology'] !== '' ? json_decode($result['phonology']) : $this->defaults['phonology']; return array( - 'id' => $this->token->hash($result['id']), + 'id' => $result['id'], 'name' => $result['name'], 'specification' => $result['specification'], 'description' => $result['description'], diff --git a/public/api/User.php b/public/api/User.php index 34ebb39..7acc1fc 100644 --- a/public/api/User.php +++ b/public/api/User.php @@ -115,9 +115,10 @@ VALUES (?, ?, ?, ?, ?)'; $id = $user_data->id; $new_dictionary = $this->dictionary->create($id); if (!isset($new_dictionary['error'])) { + $new_token = $this->generateUserToken($id, $new_dictionary); return array( - 'token' => $this->generateUserToken($id, $new_dictionary), - 'dictionary' => $this->getCurrentDictionary($token), + 'token' => $new_token, + 'dictionary' => $this->getCurrentDictionary($new_token), ); } else { return $new_dictionary; diff --git a/src/components/management/AccountManager/MyAccount.jsx b/src/components/management/AccountManager/MyAccount.jsx index 6397176..c3f8b69 100644 --- a/src/components/management/AccountManager/MyAccount.jsx +++ b/src/components/management/AccountManager/MyAccount.jsx @@ -11,6 +11,7 @@ export class MyAccount extends Component { publicName: PropTypes.string.isRequired, allowEmails: PropTypes.bool.isRequired, userDictionaries: PropTypes.array.isRequired, + dictionary: PropTypes.object, sendUserData: PropTypes.func, changeDictionary: PropTypes.func, }, props, 'prop', 'MyAccount'); @@ -136,15 +137,23 @@ export class MyAccount extends Component {

Account Actions

+
+
+ +
+
+
- {this.props.userDictionaries.map(item => { - return ; + return ; })}
diff --git a/src/components/management/AccountManager/index.jsx b/src/components/management/AccountManager/index.jsx index 53c0521..f80e41b 100644 --- a/src/components/management/AccountManager/index.jsx +++ b/src/components/management/AccountManager/index.jsx @@ -17,6 +17,7 @@ export class AccountManager extends Component { PropTypes.checkPropTypes({ updater: PropTypes.object.isRequired, + dictionary: PropTypes.object, }, props, 'prop', 'AccountManager'); const userData = store.get('LexicongaUserData'); @@ -163,6 +164,7 @@ export class AccountManager extends Component { publicName={ userData.publicName } allowEmails={ userData.allowEmails } userDictionaries={ this.state.userDictionaries } + dictionary={ this.props.dictionary } sendUserData={ this.sendUserData.bind(this) } changeDictionary={ () => {} } /> diff --git a/src/components/structure/Header.jsx b/src/components/structure/Header.jsx index 59a98d9..916819d 100644 --- a/src/components/structure/Header.jsx +++ b/src/components/structure/Header.jsx @@ -17,6 +17,7 @@ export class Header extends Component { partsOfSpeech: PropTypes.array.isRequired, search: PropTypes.func.isRequired, updater: PropTypes.object.isRequired, + dictionary: PropTypes.object, }, props, 'prop', 'Header'); this.state = { @@ -49,7 +50,9 @@ export class Header extends Component {
- + diff --git a/src/index.jsx b/src/index.jsx index 3a0b368..7e9ad3f 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -209,6 +209,7 @@ class App extends Component { partsOfSpeech={ this.state.partsOfSpeech } search={ (searchConfig) => this.search(searchConfig) } updater={ this.updater } + dictionary={ dictionary } /> { + window.location.reload(); + }).catch(err => { + console.error('Could not delete words db: ', err); + }); + } + + createNew () { + request('create-new-dictionary', { + token: store.get('LexicongaToken'), + }, response => { + const {data, error} = response; + if (error) { + console.error(data); + } else { + console.log(data); + store.set('LexicongaToken', data.token); + this.reset(data.dictionary.details); + } + }); + } } export default new DictionaryData; \ No newline at end of file