diff options
| author | adamhrv <adam@ahprojects.com> | 2019-01-14 22:25:25 +0100 |
|---|---|---|
| committer | adamhrv <adam@ahprojects.com> | 2019-01-14 22:25:25 +0100 |
| commit | df9d364e3664f45c65cac5990d3d742b990217fa (patch) | |
| tree | 8842d844a5ea8e6c87599b8683009cba23262713 /client/faceAnalysis/faceAnalysis.actions.js | |
| parent | 2fedd95fcee3f048c5f24333ffdb9bb4e13eafe2 (diff) | |
| parent | 3b2f0dc6d969fa323fe8775b4269e17c60192431 (diff) | |
Merge branch 'master' of github.com:adamhrv/megapixels_dev
Diffstat (limited to 'client/faceAnalysis/faceAnalysis.actions.js')
| -rw-r--r-- | client/faceAnalysis/faceAnalysis.actions.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/client/faceAnalysis/faceAnalysis.actions.js b/client/faceAnalysis/faceAnalysis.actions.js new file mode 100644 index 00000000..4a6fe6ed --- /dev/null +++ b/client/faceAnalysis/faceAnalysis.actions.js @@ -0,0 +1,91 @@ +// import fetchJsonp from 'fetch-jsonp' +import * as types from '../types' +// import { hashPath } from '../util' +import { store } from '../store' +import { get, post } from '../util' +// import querystring from 'query-string' + +// urls + +const url = { + upload: () => process.env.API_HOST + '/task/upload/demo', +} +export const publicUrl = { +} + +// standard loading events + +const loading = (tag, offset) => ({ + ts: Date.now(), + type: types.faceAnalysis.loading, + tag, + offset +}) +const loaded = (tag, data, offset = 0) => ({ + ts: Date.now(), + type: types.faceAnalysis.loaded, + tag, + data, + offset +}) +const polled = (data, offset = 0) => ({ + ts: Date.now(), + type: types.faceAnalysis.poll, + data, + offset +}) +const error = (tag, err) => ({ + type: types.faceAnalysis.error, + tag, + err +}) + +// search UI functions + +export const updateOptions = opt => dispatch => { + dispatch({ type: types.faceAnalysis.update_options, opt }) +} + +// API functions + +// task polling + +const POLL_DELAY = 500 +let pollTimeout = null + +export const poll = (payload, taskURL) => dispatch => { + clearTimeout(pollTimeout) + // console.log('polling...') + get(taskURL) + .then(data => { + // console.log('poll', data) + dispatch(polled(data)) + // console.log(data.state) + if (data.state === 'COMPLETE' || data.state === 'SUCCESS') { + console.log('complete!') + } else if (data.state === 'ERROR' || data.state === 'FAILURE') { + console.log('errorr!') + dispatch(error(data)) + } else { + pollTimeout = setTimeout(() => poll(payload, taskURL)(dispatch), POLL_DELAY) + } + }) + .catch(err => dispatch(error('result', err))) +} + +export const upload = (payload, file) => dispatch => { + const tag = 'task' + const fd = new FormData() + fd.append('query_img', file) + dispatch(loading(tag)) + post(url.upload(), fd) + .then(data => { + // console.log('loaded!', tag, data) + dispatch(loaded(tag, data)) + const { result, taskURL } = data + if (result && taskURL) { + poll(payload, taskURL)(dispatch) + } + }) + .catch(err => dispatch(error(tag, err))) +} |
