summaryrefslogtreecommitdiff
path: root/client/nameSearch
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-12-18 01:03:10 +0100
committerJules Laplace <julescarbon@gmail.com>2018-12-18 01:03:10 +0100
commit7b8e6f9a7d3eb36b72b53d5e754b9c7916b98ed7 (patch)
treee0445a2709ecf07e770a9b0ddecb8eeb3d168931 /client/nameSearch
parentd3be915bc5725a36dee867b07404725177783460 (diff)
namesearchg
Diffstat (limited to 'client/nameSearch')
-rw-r--r--client/nameSearch/index.js5
-rw-r--r--client/nameSearch/nameSearch.actions.js52
-rw-r--r--client/nameSearch/nameSearch.container.js24
-rw-r--r--client/nameSearch/nameSearch.query.js48
-rw-r--r--client/nameSearch/nameSearch.reducer.js32
-rw-r--r--client/nameSearch/nameSearch.result.js88
6 files changed, 249 insertions, 0 deletions
diff --git a/client/nameSearch/index.js b/client/nameSearch/index.js
new file mode 100644
index 00000000..8c6475e4
--- /dev/null
+++ b/client/nameSearch/index.js
@@ -0,0 +1,5 @@
+import Container from './nameSearch.container'
+
+export {
+ Container,
+}
diff --git a/client/nameSearch/nameSearch.actions.js b/client/nameSearch/nameSearch.actions.js
new file mode 100644
index 00000000..290ee38d
--- /dev/null
+++ b/client/nameSearch/nameSearch.actions.js
@@ -0,0 +1,52 @@
+// import fetchJsonp from 'fetch-jsonp'
+import * as types from '../types'
+// import { hashPath } from '../util'
+import { post } from '../util'
+// import querystring from 'query-string'
+
+// urls
+
+const url = {
+ search: (dataset, q) => process.env.API_HOST + '/api/dataset/' + dataset + '/name?q=' + encodeURIComponent(q),
+}
+export const publicUrl = {
+}
+
+// standard loading events
+
+const loading = (tag, offset) => ({
+ type: types.nameSearch.loading,
+ tag,
+ offset
+})
+const loaded = (tag, data, offset = 0) => ({
+ type: types.nameSearch.loaded,
+ tag,
+ data,
+ offset
+})
+const error = (tag, err) => ({
+ type: types.nameSearch.error,
+ tag,
+ err
+})
+
+// search UI functions
+
+export const updateOptions = opt => dispatch => {
+ dispatch({ type: types.nameSearch.update_options, opt })
+}
+
+// API functions
+
+export const search = (payload, q) => dispatch => {
+ const tag = 'result'
+ const fd = new FormData()
+ fd.append('q', q)
+ dispatch(loading(tag))
+ post(url.search(payload.dataset, q), fd)
+ .then(data => {
+ dispatch(loaded(tag, data))
+ })
+ .catch(err => dispatch(error(tag, err)))
+}
diff --git a/client/nameSearch/nameSearch.container.js b/client/nameSearch/nameSearch.container.js
new file mode 100644
index 00000000..b0de0c3a
--- /dev/null
+++ b/client/nameSearch/nameSearch.container.js
@@ -0,0 +1,24 @@
+import React, { Component } from 'react'
+import { bindActionCreators } from 'redux'
+import { connect } from 'react-redux'
+
+import * as actions from './nameSearch.actions'
+
+import NameSearchQuery from './nameSearch.query'
+import NameSearchResult from './nameSearch.result'
+
+class NameSearchContainer extends Component {
+ render() {
+ const { payload } = this.props
+ // console.log(payload)
+ return (
+ <div className='searchContainer'>
+ <NameSearchQuery payload={payload} />
+ <NameSearchResult payload={payload} />
+ </div>
+ )
+ }
+}
+
+
+export default NameSearchContainer
diff --git a/client/nameSearch/nameSearch.query.js b/client/nameSearch/nameSearch.query.js
new file mode 100644
index 00000000..b82e324b
--- /dev/null
+++ b/client/nameSearch/nameSearch.query.js
@@ -0,0 +1,48 @@
+import React, { Component } from 'react'
+import { bindActionCreators } from 'redux'
+import { connect } from 'react-redux'
+
+import * as actions from './nameSearch.actions'
+
+class NameSearchQuery extends Component {
+ state = {
+ value: null
+ }
+
+ handleInput(value) {
+ this.setState({ q: value })
+ if (value.length > 2) {
+ this.props.actions.search(this.props.payload, value)
+ }
+ }
+
+ render() {
+ return (
+ <div className='query'>
+ <h2>Find Your Name</h2>
+ <h3>Searching {13456} identities</h3>
+ <p>
+ {'Enter your name to see if you were included in this dataset..'}
+ </p>
+ <input
+ type="text"
+ class="q"
+ placeholder="Enter your name"
+ value={this.state.q}
+ onInput={e => this.handleInput(e.target.value)}
+ />
+ </div>
+ )
+ }
+}
+
+const mapStateToProps = state => ({
+ result: state.nameSearch.result,
+ options: state.nameSearch.options,
+})
+
+const mapDispatchToProps = dispatch => ({
+ actions: bindActionCreators({ ...actions }, dispatch),
+})
+
+export default connect(mapStateToProps, mapDispatchToProps)(NameSearchQuery)
diff --git a/client/nameSearch/nameSearch.reducer.js b/client/nameSearch/nameSearch.reducer.js
new file mode 100644
index 00000000..101c93ea
--- /dev/null
+++ b/client/nameSearch/nameSearch.reducer.js
@@ -0,0 +1,32 @@
+import * as types from '../types'
+
+const initialState = () => ({
+ query: {},
+ result: {},
+ loading: false,
+})
+
+export default function nameSearchReducer(state = initialState(), action) {
+ switch (action.type) {
+ case types.nameSearch.loading:
+ return {
+ ...state,
+ [action.tag]: { loading: true },
+ }
+
+ case types.nameSearch.loaded:
+ return {
+ ...state,
+ [action.tag]: action.data,
+ }
+
+ case types.nameSearch.error:
+ return {
+ ...state,
+ [action.tag]: { error: action.err },
+ }
+
+ default:
+ return state
+ }
+}
diff --git a/client/nameSearch/nameSearch.result.js b/client/nameSearch/nameSearch.result.js
new file mode 100644
index 00000000..9e20228c
--- /dev/null
+++ b/client/nameSearch/nameSearch.result.js
@@ -0,0 +1,88 @@
+import React, { Component } from 'react'
+import { bindActionCreators } from 'redux'
+import { connect } from 'react-redux'
+import { courtesyS } from '../util'
+
+import * as actions from './nameSearch.actions'
+import { Loader } from '../common'
+
+const errors = {
+ nomatch: (
+ <div>
+ <h3>Name not found</h3>
+ {"No names matched your query."}
+ </div>
+ ),
+ error: (
+ <div>
+ <h3>{"No matches found"}</h3>
+ </div>
+ ),
+}
+
+class NameSearchResult extends Component {
+ render() {
+ const { dataset } = this.props.payload
+ const { query, results, loading, error } = this.props.result
+ console.log(this.props.result)
+ if (loading) {
+ return (
+ <div className='result'>
+ <div>
+ <Loader />
+ </div>
+ </div>
+ )
+ }
+ if (error) {
+ console.log(error)
+ let errorMessage = errors[error] || errors.error
+ return (
+ <div className='result'>{errorMessage}</div>
+ )
+ }
+ if (!results) {
+ return <div className='result'></div>
+ }
+ if (!results.length) {
+ return (
+ <div className='result'>{errors.nomatch}</div>
+ )
+ }
+ const els = results.map((result, i) => {
+ const { uuid } = result.uuid
+ const { fullname, gender, description, images } = result.identity
+ return (
+ <div key={i}>
+ <img src={'https://megapixels.nyc3.digitaloceanspaces.com/v1/media/' + dataset + '/' + uuid + '.jpg'} />
+ {fullname} {'('}{gender}{')'}<br/>
+ {description}<br/>
+ {courtesyS(images, 'image')}{' in dataset'}<br />
+ </div>
+ )
+ })
+
+ return (
+ <div className='result'>
+ <div className="timing">
+ {'Search took '}{Math.round(query.timing * 1000) + ' ms'}
+ </div>
+ <div className='results'>
+ {els}
+ </div>
+ </div>
+ )
+ }
+}
+
+const mapStateToProps = state => ({
+ query: state.nameSearch.query,
+ result: state.nameSearch.result,
+ options: state.nameSearch.options,
+})
+
+const mapDispatchToProps = dispatch => ({
+ actions: bindActionCreators({ ...actions }, dispatch),
+})
+
+export default connect(mapStateToProps, mapDispatchToProps)(NameSearchResult)