From 80fdea0d4c70b8bd33740e1b32ca8c0c70be4289 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 20 Apr 2017 13:02:12 -0600 Subject: [PATCH] Start adding words to db via search form --- src/components/management/SearchBox.jsx | 4 +- src/components/management/WordForm.jsx | 134 ++++++++++++++++++++++-- src/managers/DictionaryData.js | 10 ++ src/managers/IDManager.js | 17 +++ src/managers/Word.js | 45 ++++++++ src/managers/WordDatabase.js | 6 +- 6 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 src/managers/IDManager.js create mode 100644 src/managers/Word.js diff --git a/src/components/management/SearchBox.jsx b/src/components/management/SearchBox.jsx index 115a541..0611a24 100644 --- a/src/components/management/SearchBox.jsx +++ b/src/components/management/SearchBox.jsx @@ -47,7 +47,7 @@ export class SearchBox extends Component { + { + this.setState({ wordName: event.target.value }); + }} /> + {(!this.state.nameIsValid) + ? ( + You must specify the word. + ) : null}

- + { + this.setState({ wordPronunciation: event.target.value }); + }} />

@@ -27,8 +112,11 @@ export class WordForm extends Component {

- { + this.setState({ wordPartOfSpeech: event.target.value }); + }}> + {this.props.partsOfSpeech.map((partOfSpeech) => { return ( @@ -42,14 +130,48 @@ export class WordForm extends Component {

- + { + this.setState({ wordDefinition: event.target.value }) + }} /> + {(!this.state.definitionIsValid) + ? ( + + You must at least enter a Definition if excluding Details. + + ) : null}

- + {(!this.state.detailsIsValid) + ? ( + + You must at least enter Details if excluding a Definition. + + ) : null} +

+
+ +
+

+ { + this.createWord(); + }}> + Create +

diff --git a/src/managers/DictionaryData.js b/src/managers/DictionaryData.js index c5130a4..1781ad6 100644 --- a/src/managers/DictionaryData.js +++ b/src/managers/DictionaryData.js @@ -1,6 +1,7 @@ import assert from 'assert'; import store from 'store'; import wordDb from './WordDatabase'; +import idManager from './IDManager'; const defaultDictionary = { name: 'New' @@ -17,6 +18,15 @@ class DictionaryData { if (!store.get('Lexiconga')) { store.set('Lexiconga', defaultDictionary); + } else { + const largestId = wordDb.words + .orderBy('id').reverse() + .first((word) => { + return word.id; + }); + + idManager.setId('word', ++largestId); + console.log('First word ID: ' + idManager.next('word').toString()); } } diff --git a/src/managers/IDManager.js b/src/managers/IDManager.js new file mode 100644 index 0000000..0d9525f --- /dev/null +++ b/src/managers/IDManager.js @@ -0,0 +1,17 @@ +class IDManager { + constructor () { + this['key'] = 0; + this['word'] = 0; + // Add IDs here as needed. + } + + setID (id, value) { + this[id] = value; + } + + next (id) { + return this[id]++; + } +} + +export default new IDManager; \ No newline at end of file diff --git a/src/managers/Word.js b/src/managers/Word.js new file mode 100644 index 0000000..3735f98 --- /dev/null +++ b/src/managers/Word.js @@ -0,0 +1,45 @@ +import assert from 'assert'; +import store from 'store'; +import wordDb from './WordDatabase'; + +const defaultDictionary = { + name: 'New' +, specification: 'Dictionary' +, description: 'A new dictionary.' +, partsOfSpeech: ['Noun', 'Adjective', 'Verb'] +} + +export class Word { + constructor ({name = '', pronunciation = '', partOfSpeech = '', definition = '', details = ''}) { + this.name = name; + this.pronunciation = pronunciation; + this.partOfSpeech = partOfSpeech; + this.definition = definition; + this.details = details; + } + + create () { + const timestampInSeconds = Math.round(Date.now() / 1000); + this.createdTime = timestampInSeconds; + this.modifiedTime = null; + + return wordDb.words.add(this) + .then(id => { + this.id = id; + console.log('Word added successfully'); + }) + .catch(error => { + console.error(error); + }); + } + + update (wordObject, wordId) { + const timestampInSeconds = Math.round(Date.now() / 1000); + this.modifiedTime = timestampInSeconds; + + wordDb.words.put(wordObject, wordId) + .catch(error => { + + }); + } +} diff --git a/src/managers/WordDatabase.js b/src/managers/WordDatabase.js index 56c369a..b219e38 100644 --- a/src/managers/WordDatabase.js +++ b/src/managers/WordDatabase.js @@ -1,12 +1,16 @@ import Dexie from 'dexie'; +import {Word} from './Word'; + const db = new Dexie('Lexiconga'); db.version(1).stores({ - words: '++id, name, partOfSpeech' + words: '++id, name, partOfSpeech, createdTime, modifiedTime' }); if (['emptydb', 'donotsave'].includes(process.env.NODE_ENV)) { db.words.clear(); } +db.words.mapToClass(Word); + export default db;