import React, { Component } from 'react' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import * as actions from '../actions' import UploadImage from './uploadImage.component' class Query extends Component { state = { image: null, blob: null, url: "", threshold: 20, searchType: 'file', thresholdChanged: false, } handleUpload(blob) { const { image, threshold } = this.state if (image) { URL.revokeObjectURL(image) } const url = URL.createObjectURL(blob) this.setState({ image: url, blob, thresholdChanged: false }) this.props.actions.upload(blob, threshold) } handleURL() { const { url, threshold } = this.state if (!url || url.indexOf('http') !== 0) return this.setState({ image: url, blob: null, thresholdChanged: false }) this.props.actions.submit(url, threshold) } resubmit() { const { image, blob } = this.state if (blob) { this.handleUpload(blob) } else { this.handleURL() } } render() { const { url, image, threshold, thresholdChanged, searchType } = this.state const style = {} if (image) { style.backgroundImage = 'url(' + image + ')' } return (
Search by
{searchType === 'file' ? : } {image &&
Query image
}
) } } const mapStateToProps = state => ({}) const mapDispatchToProps = dispatch => ({ actions: bindActionCreators({ ...actions }, dispatch), }) export default connect(mapStateToProps, mapDispatchToProps)(Query)