import Inferno from 'inferno'; import Component from 'inferno-component'; import PropTypes from 'prop-types'; import marked from 'marked'; import swal from 'sweetalert2'; import './WordDisplay.scss'; import idManager from '../../managers/IDManager'; import { Word } from '../../managers/Word'; import { WordForm } from '../management/WordForm'; export class WordDisplay extends Component { constructor (props) { super(props); PropTypes.checkPropTypes({ word: PropTypes.object.isRequired, updateDisplay: PropTypes.func.isRequired, }, props, 'prop', 'WordDisplay'); this.state = { menuIsOpen: false, isEditing: false, } this.wordDetailsHTML = marked(props.word.details); } componentWillUpdate (nextProps, nextState) { if (this.props.word.details !== nextProps.word.details) { this.wordDetailsHTML = marked(nextProps.word.details); } } edit () { this.setState({ menuIsOpen: false, isEditing: true, }); } delete () { const { word } = this.props; swal({ title: `Delete "${ word.name }"?`, text: `It will be gone forever and cannot be restored!`, type: 'warning', showCancelButton: true, confirmButtonText: 'Yes, delete it!', cancelButtonText: 'No, cancel!', confirmButtonClass: 'button is-danger', cancelButtonClass: 'button', buttonsStyling: false }).then(() => { word.delete(word.id) .then(() => { this.props.updateDisplay(); }).then(() => { this.setState({ menuIsOpen: false }, () => { swal( 'Deleted!', `"${ word.name }" has been deleted.`, 'success' ); }); }); }, dismiss => { console.log('Word not deleted'); }); } render () { const { menuIsOpen, isEditing } = this.state; const { word, updateDisplay } = this.props; if (isEditing) { return ( { this.setState({ menuIsOpen: false, isEditing: false, }) }} /> ); } return (
{ word.name } { (word.pronunciation || word.partOfSpeech) && ( { (word.pronunciation) && ( { word.pronunciation } ) } { (word.partOfSpeech) && ( { word.partOfSpeech } ) } ) }
this.setState({ menuIsOpen: !menuIsOpen }) } >
{ (word.definition) && (

{ word.definition }

) } { (word.details) && (

) }

); } }