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