summaryrefslogtreecommitdiff
path: root/client/faceSearch/faceSearch.result.js
blob: 1882def06e9c8b1fa628efb99c2eec46081c8060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { courtesyS } from '../util'

import * as actions from './faceSearch.actions'

const errors = {
  'bbox': "Sorry, we couldn't find a face in that image.  Please choose an image where the face is large and clear.",
  'nomatch': "We didn't find a match.",
  'default': "There was an error!",
}

class FaceSearchResult extends Component {
  render() {
    const { dataset } = this.props.payload
    const { distances, results, error } = this.props.result
    if (error) {
      let errorMessage = errors[error.message] || errors.default
      return (
        <div className='result'>{errorMessage}</div>
      )
    }
    if (!results) {
      return (
        <div className='result'></div>
      )
    }
    if (!this.props.result.results.length) {
      return (
        <div className='result'>No results</div>
      )
    }
    const els = results.map((result, i) => {
      const distance = distances[i]
      const { uuid } = result.uuid
      const { fullname, gender, description, images } = result.identity
      return (
        <div>
          <img src={'https://megapixels.nyc3.digitaloceanspaces.com/v1/media/' + dataset + '/' + uuid + '.jpg'} />
          {fullname} {'('}{gender}{')'}<br/>
          {description}<br/>
          {courtesyS(images, 'image')}<br />
          {distance}
        </div>
      )
    })

    return (
      <div className='result'>
        <div class='results'>
          {els}
        </div>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  query: state.faceSearch.query,
  result: state.faceSearch.result,
  options: state.faceSearch.options,
})

const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators({ ...actions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(FaceSearchResult)