import Inferno from 'inferno'; import Component from 'inferno-component'; import { IPAField } from './IPAField'; import { Word } from '../../managers/Word'; export class WordForm extends Component { constructor (props) { super(props); 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; } submitWord () { 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()) { // 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(); this.props.updateDisplay(); }); } } } clearForm () { this.setState({ wordName: '', wordPronunciation: '', wordPartOfSpeech: '', wordDefinition: '', wordDetails: '', }); } render () { return (
{ this.setState({ wordName: event.target.value }); }} /> {(!this.state.nameIsValid) ? ( You must specify the word. ) : null}
{ this.setState({ wordDefinition: event.target.value }) }} /> {(!this.state.definitionIsValid) ? ( You must at least enter a Definition if excluding Details. ) : null}