import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { defineMessages, injectIntl } from 'react-intl' import { fetchGifCategories, fetchGifResults, clearGifResults, setSelectedGif, changeGifSearchText } from '../../actions/tenor' import Block from '../block' import Button from '../button' import ColumnIndicator from '../column_indicator' import Image from '../image' import Input from '../input' import Text from '../text' const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, title: { id: 'pick_gif', defaultMessage: 'Select a GIF' }, searchGifs: { id: 'search_gifs', defaultMessage: 'Search for GIFs' }, }) const mapStateToProps = (state) => ({ categories: state.getIn(['tenor', 'categories']), suggestions: state.getIn(['tenor', 'suggestions']), results: state.getIn(['tenor', 'results']), loading: state.getIn(['tenor', 'loading']), error: state.getIn(['tenor', 'error']), searchText: state.getIn(['tenor', 'searchText']), }) const mapDispatchToProps = (dispatch, { onClose }) => ({ handleCloseModal() { dispatch(changeGifSearchText('')) dispatch(clearGifResults()) onClose() }, handleFetchCategories: () => { dispatch(fetchGifCategories()) }, handleOnChange: (value) => { dispatch(changeGifSearchText(value)) if (value.length >= 3) { dispatch(fetchGifResults()) } else if (value.length === 0) { dispatch(clearGifResults()) } }, handleSelectResult: (result) => { dispatch(setSelectedGif(result)) onClose() }, // dispatchSubmit: (e) => { // e.preventDefault(); // dispatch(getGifs()); // }, }) export default @connect(mapStateToProps, mapDispatchToProps) @injectIntl class GifPickerModal extends React.PureComponent { static propTypes = { intl: PropTypes.object.isRequired, handleCloseModal: PropTypes.func.isRequired, handleFetchCategories: PropTypes.func.isRequired, handleOnChange: PropTypes.func.isRequired, handleSelectResult: PropTypes.func.isRequired, categories: PropTypes.array.isRequired, results: PropTypes.array.isRequired, loading: PropTypes.bool, error: PropTypes.bool, searchText: PropTypes.string, } state = { row: 0, } componentDidMount() { this.props.handleFetchCategories() } onChange = (value) => { this.props.handleOnChange(value) } onHandleCloseModal = () => { this.props.handleCloseModal() } handleSelectCategory = (category) => { this.props.handleOnChange(category) } handleSelectGifResult = (resultBlock) => { this.props.handleSelectResult(resultBlock) } render() { const { intl, categories, results, loading, error, searchText, } = this.props return (
{ error && } { (loading && results.length === 0 && categories.length === 0) && } { (results.length > 0 || categories.length > 0) &&
{ results.length === 0 && categories.length > 0 && } { results.length > 0 && }
}
) } } class GifResultsCollectionColumn extends React.PureComponent { static propTypes = { results: PropTypes.array.isRequired, handleSelectGifResult: PropTypes.func.isRequired, } onClick = (resultId) => { this.props.handleSelectGifResult(resultId) } render() { const { results } = this.props return (
{ results.map((result, i) => ( )) }
) } } class GifResultsCollection extends React.PureComponent { static propTypes = { results: PropTypes.array.isRequired, handleSelectGifResult: PropTypes.func.isRequired, } render() { const { results, handleSelectGifResult } = this.props // : todo : const count = results.length const columnIndex = 10 return (
) } } class GifCategoriesCollection extends React.PureComponent { static propTypes = { categories: PropTypes.array.isRequired, handleSelectCategory: PropTypes.func.isRequired, } onClick = (term) => { this.props.handleSelectCategory(term) } render() { const { categories } = this.props return (
{ categories.map((category, i) => ( )) }
) } }