-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #765 from 18F/bjs-start-ori-search
First pass at agency search functionality
- Loading branch information
Showing
36 changed files
with
416 additions
and
132 deletions.
There are no files selected for viewing
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const jsonFile = require('./agencies.json') | ||
|
||
const agencies = jsonFile.results | ||
const usStates = {} | ||
|
||
agencies.forEach(agency => { | ||
const stateAbbr = agency.state_abbr | ||
|
||
const smallAgency = { | ||
agency_name: agency.agency_name, | ||
} | ||
|
||
if (usStates[stateAbbr]) { | ||
usStates[stateAbbr][agency.ori] = smallAgency | ||
} else { | ||
usStates[stateAbbr] = { [agency.ori]: smallAgency } | ||
} | ||
}) | ||
|
||
const onWriteDone = err => { | ||
if (err) throw err | ||
|
||
console.log('done!') | ||
|
||
Object.keys(usStates).sort((a, b) => a - b).forEach(state => { | ||
const agenciesCount = Object.keys(usStates[state]).length | ||
console.log(`${state} has ${agenciesCount} agencies`) | ||
}) | ||
} | ||
|
||
const file = path.join(__dirname, '../../data/agencies-by-state.json') | ||
|
||
fs.writeFile(file, JSON.stringify(usStates), onWriteDone) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, { Component } from 'react' | ||
|
||
import AgencySearchResults from './AgencySearchResults' | ||
|
||
class AgencySearch extends Component { | ||
state = { search: '', displaySelection: true } | ||
|
||
handleChange = e => { | ||
this.setState({ search: e.target.value }) | ||
} | ||
|
||
handleClick = datum => e => { | ||
e.preventDefault() | ||
this.setState({ displaySelection: true }) | ||
this.props.onChange({ | ||
place: datum.ori, | ||
placeType: 'agency', | ||
}) | ||
} | ||
|
||
removeSelection = () => { | ||
this.setState({ displaySelection: false, search: '' }) | ||
} | ||
|
||
render() { | ||
const { agencies, ori, state } = this.props | ||
const { displaySelection, search } = this.state | ||
|
||
const data = Object.keys(agencies[state]).map(agencyOri => { | ||
const agency = agencies[state][agencyOri] | ||
return { ori: agencyOri, ...agency } | ||
}) | ||
const selected = data.find(d => d.ori === ori) | ||
|
||
const searchUpper = search.toUpperCase() | ||
const dataFiltered = searchUpper === '' | ||
? data | ||
: data.filter(d => { | ||
const words = `${d.ori} ${d.agency_name}`.toUpperCase() | ||
return words.includes(searchUpper) | ||
}) | ||
|
||
const showOris = !!search.length && dataFiltered.length > 0 | ||
|
||
return ( | ||
<div className="mt2"> | ||
{selected && displaySelection | ||
? <div className="mb2 relative"> | ||
<input | ||
type="text" | ||
className="col-12 field field-sm bg-white border-blue pr5 truncate" | ||
defaultValue={selected.agency_name} | ||
/> | ||
<button | ||
className="absolute btn p0 line-height-1" | ||
style={{ top: '.5rem', right: '1rem' }} | ||
onClick={this.removeSelection} | ||
> | ||
<img src="/img/x-navy.svg" alt="close" width="12" height="12" /> | ||
</button> | ||
</div> | ||
: <div className="relative"> | ||
<div className="relative"> | ||
<input | ||
type="text" | ||
className="col-12 field field-sm bg-white border-blue rounded-none" | ||
placeholder="Search for an agency..." | ||
value={search} | ||
onChange={this.handleChange} | ||
/> | ||
</div> | ||
{showOris && | ||
<AgencySearchResults | ||
data={dataFiltered.sort( | ||
(a, b) => a.agency_name > b.agency_name, | ||
)} | ||
onClick={this.handleClick} | ||
/>} | ||
</div>} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default AgencySearch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react' | ||
|
||
const agencyTypes = [ | ||
'City', | ||
'County', | ||
'Federal', | ||
'State Police', | ||
'University or college', | ||
'Tribal', | ||
'Other', | ||
] | ||
|
||
const AgencySearchRefine = ({ keyword, onChange, onSubmit }) => ( | ||
<div | ||
className="p2 absolute col-12 border-box bg-white border" | ||
style={{ marginTop: -1, minHeight: 280 }} | ||
> | ||
<div> | ||
<label className="mb-tiny fs-18 bold block">Keyword</label> | ||
<input | ||
className="mb2 col-12 field field-sm bg-white border-blue" | ||
type="text" | ||
name="keyword" | ||
value={keyword} | ||
onChange={onChange} | ||
/> | ||
</div> | ||
<div> | ||
<label className="mb-tiny fs-18 bold block">Agency type</label> | ||
{agencyTypes.map((d, i) => ( | ||
<label key={i} className="block control checkbox"> | ||
<input type="checkbox" value={d} /> | ||
<span className="indicator" /> | ||
<span className="fs-12">{d}</span> | ||
</label> | ||
))} | ||
</div> | ||
<div className="mt1"> | ||
<button className="btn btn-sm btn-primary bg-navy" onClick={onSubmit}> | ||
View results | ||
</button> | ||
</div> | ||
</div> | ||
) | ||
|
||
export default AgencySearchRefine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
|
||
const AgencySearchResults = ({ data, onClick }) => ( | ||
<div | ||
className="mb2 p2 absolute col-12 border-box bg-white border overflow-auto" | ||
style={{ marginTop: -1, maxHeight: 240 }} | ||
> | ||
<div className="mb1 fs-20 bold line-height-2">Results</div> | ||
<ul className="m0 list-reset fs-12"> | ||
{data.slice(0, 100).map((d, i) => ( | ||
<li key={i} className=""> | ||
<a | ||
className="block black truncate" | ||
style={{ lineHeight: '1.75' }} | ||
href="#!" | ||
onClick={onClick(d)} | ||
> | ||
{d.agency_name} | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
|
||
export default AgencySearchResults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.