summaryrefslogtreecommitdiff
path: root/client/faceSearch/faceSearch.actions.js
blob: 03e1a91def9c19211a883b1b37aa91dc18816138 (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
// import fetchJsonp from 'fetch-jsonp'
import * as types from '../types'
// import { hashPath } from '../util'
import { store } from '../store'
import { post, preloadImage } from '../util'
// import querystring from 'query-string'

// urls

const url = {
  upload: (dataset) => process.env.API_HOST + '/api/dataset/' + dataset + '/face',
}
export const publicUrl = {
}

// standard loading events

const loading = (tag, offset) => ({
  type: types.faceSearch.loading,
  tag,
  offset
})
const loaded = (tag, data, offset = 0) => ({
  type: types.faceSearch.loaded,
  tag,
  data,
  offset
})
const error = (tag, err) => ({
  type: types.faceSearch.error,
  tag,
  err
})

// search UI functions

export const updateOptions = opt => dispatch => {
  dispatch({ type: types.faceSearch.update_options, opt })
}

// API functions

export const upload = (payload, file) => dispatch => {
  // const { options } = store.getState().faceSearch
  const tag = 'result'
  const fd = new FormData()
  fd.append('query_img', file)
  // fd.append('limit', options.perPage)
  // if (!query) {
  dispatch(loading(tag))
  // }
  post(url.upload(payload.dataset), fd)
    .then(data => {
      dispatch(loaded(tag, data))
    })
    .catch(err => dispatch(error(tag, err)))
}