2017-04-04 14:39:35 -06:00
|
|
|
import Inferno from 'inferno';
|
|
|
|
import Component from 'inferno-component';
|
|
|
|
|
2017-04-22 17:05:36 -06:00
|
|
|
import {IPAField} from './IPAField';
|
2017-04-20 13:02:12 -06:00
|
|
|
import {Word} from '../../managers/Word';
|
|
|
|
|
2017-04-04 14:39:35 -06:00
|
|
|
export class WordForm extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2017-04-20 13:02:12 -06:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
wordName: this.props.name || ''
|
|
|
|
, wordPronunciation: this.props.pronunciation || ''
|
|
|
|
, wordPartOfSpeech: this.props.partOfSpeech || ''
|
|
|
|
, wordDefinition: this.props.definition || ''
|
|
|
|
, wordDetails: this.props.details || ''
|
|
|
|
|
|
|
|
, nameIsValid: true
|
|
|
|
, pronunciationIsValid: true
|
|
|
|
, partOfSpeechIsValid: true
|
|
|
|
, definitionIsValid: true
|
|
|
|
, detailsIsValid: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidWord () {
|
|
|
|
let nameIsValid = true
|
|
|
|
, definitionIsValid = true
|
|
|
|
, detailsIsValid = true;
|
|
|
|
|
|
|
|
if (this.state.wordName === '') {
|
|
|
|
nameIsValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.wordPartOfSpeech === '') {
|
|
|
|
// popup(?) confirming no part of speech.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.wordDefinition === ''
|
|
|
|
&& this.state.wordDetails === '') {
|
|
|
|
definitionIsValid = false;
|
|
|
|
detailsIsValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
nameIsValid
|
|
|
|
, definitionIsValid
|
|
|
|
, detailsIsValid
|
|
|
|
});
|
|
|
|
|
|
|
|
return nameIsValid == true && definitionIsValid == true && detailsIsValid == true;
|
|
|
|
}
|
|
|
|
|
2017-05-18 23:43:29 -06:00
|
|
|
submitWord () {
|
2017-04-20 13:02:12 -06:00
|
|
|
const word = new Word({
|
|
|
|
name: this.state.wordName
|
|
|
|
, pronunciation: this.state.wordPronunciation
|
|
|
|
, partOfSpeech: this.state.wordPartOfSpeech
|
|
|
|
, definition: this.state.wordDefinition
|
|
|
|
, details: this.state.wordDetails
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (this.isValidWord()) {
|
2017-05-18 23:43:29 -06:00
|
|
|
// Need to trigger a WordsList re-render after success.
|
|
|
|
if (this.props.wordId) {
|
|
|
|
word.update(this.props.wordId)
|
|
|
|
.then(() => {
|
|
|
|
this.props.callback();
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
word.create()
|
|
|
|
.then(() => {
|
|
|
|
this.clearForm();
|
|
|
|
});
|
|
|
|
}
|
2017-04-20 13:02:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearForm () {
|
|
|
|
this.setState({
|
|
|
|
wordName: ''
|
|
|
|
, wordPronunciation: ''
|
|
|
|
, wordPartOfSpeech: ''
|
|
|
|
, wordDefinition: ''
|
|
|
|
, wordDetails: ''
|
|
|
|
});
|
2017-04-04 14:39:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div className='box'>
|
|
|
|
<div className='field'>
|
|
|
|
<label className='label'>Word</label>
|
|
|
|
<p className='control'>
|
2017-04-20 13:02:12 -06:00
|
|
|
<input className={`input${(!this.state.nameIsValid) ? ' is-danger' : ''}`}
|
|
|
|
type='text' placeholder='Required'
|
|
|
|
value={this.state.wordName}
|
|
|
|
onChange={(event) => {
|
|
|
|
this.setState({ wordName: event.target.value });
|
|
|
|
}} />
|
|
|
|
{(!this.state.nameIsValid)
|
|
|
|
? (
|
|
|
|
<span className='help is-danger'>You must specify the word.</span>
|
|
|
|
) : null}
|
2017-04-04 14:39:35 -06:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
2017-05-18 22:27:16 -06:00
|
|
|
<IPAField value={this.state.wordPronunciation}
|
2017-04-22 17:05:36 -06:00
|
|
|
onChange={newValue => this.setState({ wordPronunciation: newValue })} />
|
2017-04-04 14:39:35 -06:00
|
|
|
|
|
|
|
<div className='field'>
|
|
|
|
<label className='label'>Part of Speech</label>
|
|
|
|
<p className='control'>
|
|
|
|
<span className='select'>
|
2017-04-20 13:02:12 -06:00
|
|
|
<select value={this.state.wordPartOfSpeech}
|
|
|
|
onChange={event => {
|
|
|
|
this.setState({ wordPartOfSpeech: event.target.value });
|
|
|
|
}}>
|
|
|
|
<option value=''></option>
|
2017-04-04 14:39:35 -06:00
|
|
|
{this.props.partsOfSpeech.map((partOfSpeech) => {
|
|
|
|
return (
|
|
|
|
<option value={partOfSpeech}>{partOfSpeech}</option>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</select>
|
|
|
|
</span>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='field'>
|
|
|
|
<label className='label'>Definition</label>
|
|
|
|
<p className='control'>
|
2017-04-20 13:02:12 -06:00
|
|
|
<input className={`input${(!this.state.definitionIsValid) ? ' is-danger' : ''}`}
|
|
|
|
type='text' placeholder='Equivalent word(s)'
|
|
|
|
value={this.state.wordDefinition}
|
|
|
|
onChange={(event) => {
|
|
|
|
this.setState({ wordDefinition: event.target.value })
|
|
|
|
}} />
|
|
|
|
{(!this.state.definitionIsValid)
|
|
|
|
? (
|
|
|
|
<span className='help is-danger'>
|
|
|
|
You must at least enter a Definition if excluding Details.
|
|
|
|
</span>
|
|
|
|
) : null}
|
2017-04-04 14:39:35 -06:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='field'>
|
|
|
|
<label className='label'>Details</label>
|
|
|
|
<p className='control'>
|
2017-04-20 13:02:12 -06:00
|
|
|
<textarea className={`textarea${(!this.state.detailsIsValid) ? ' is-danger' : ''}`}
|
|
|
|
placeholder='Explanation of word (Markdown enabled)'
|
2017-05-18 22:27:16 -06:00
|
|
|
value={this.state.wordDetails}
|
2017-04-20 13:02:12 -06:00
|
|
|
onChange={(event) => {
|
|
|
|
this.setState({ wordDetails: event.target.value });
|
2017-05-18 22:27:16 -06:00
|
|
|
}} />
|
2017-04-20 13:02:12 -06:00
|
|
|
{(!this.state.detailsIsValid)
|
|
|
|
? (
|
|
|
|
<span className='help is-danger'>
|
|
|
|
You must at least enter Details if excluding a Definition.
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='field'>
|
|
|
|
<p className='control'>
|
|
|
|
<a className='button is-primary'
|
|
|
|
onClick={() => {
|
2017-05-18 23:43:29 -06:00
|
|
|
this.submitWord();
|
2017-04-20 13:02:12 -06:00
|
|
|
}}>
|
|
|
|
Create
|
|
|
|
</a>
|
2017-04-04 14:39:35 -06:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|