import React from 'react'; import ReactDOM from 'react-dom'; import Api from '../api'; import Item from './item'; class Modal extends React.Component { constructor(props) { super(props); this.state = { items: [], searching: false, searched: false, loadClass: 'loading', activeKey: null }; } componentDidMount() { // Avoid 'jerk' if overflow from body is hidden. setTimeout(() => { $('body').addClass('modal-active'); }, 200); ReactDOM.findDOMNode(this.refs.input).focus(); setTimeout(() => { this.setState({ loadClass: '' }); }, 20); } componentWillUnmount() { $('body').removeClass('modal-active'); } render() { let content = ''; let items = this.state.items.map((value) => { return }); if( ! this.state.items.length && this.state.searched) { content =
Nothing found :(
} else if(this.state.searching) { content = } else if(this.state.items.length && ! this.state.searching) { content = items; } return (
{content}
); } searchKeyEvents(event) { if(event.key === 'Enter') { this.setState({ searching: true, searched: false }); Api.search(this.props.type, event.target.value).done((value) => { setTimeout(() => { this.setState({ searching: false, searched: true, items: value }); }, 200); }).fail((value) => { if(value.status === 401) { alert('Unauthorized'); } else { alert('Server Error'); } }); } else if(event.key === 'Escape') { this.props.closeModal(); } } changeActiveKey(key) { this.setState({ activeKey: key }); } } export default Modal;