From 69240c3e724e411ef320d25c04f0aca6e6796d52 Mon Sep 17 00:00:00 2001 From: Brendan Sudol Date: Wed, 31 May 2017 13:34:47 -0400 Subject: [PATCH] start --- src/components/AgencySearch.js | 108 ++++++++++++++++------------- src/components/Explorer.js | 4 +- src/components/LocationFilter.js | 43 +++++------- src/components/SidebarContainer.js | 107 ++++++++++++++++------------ 4 files changed, 141 insertions(+), 121 deletions(-) diff --git a/src/components/AgencySearch.js b/src/components/AgencySearch.js index cd1666b0..efcc92d1 100644 --- a/src/components/AgencySearch.js +++ b/src/components/AgencySearch.js @@ -1,34 +1,42 @@ +import PropTypes from 'prop-types' import React, { Component } from 'react' import AgencySearchResults from './AgencySearchResults' class AgencySearch extends Component { - state = { search: '', showSelection: true } + constructor(props) { + super(props) + + const search = props.agency + const hasSelection = !!search + this.state = { search, hasSelection, showResults: false } + } handleChange = e => { - this.setState({ search: e.target.value }) + this.setState({ + search: e.target.value, + hasSelection: false, + showResults: true, + }) } handleClick = d => e => { e.preventDefault() - this.setState({ search: '', showSelection: true }) + this.setState({ search: d.agency_name, hasSelection: true }) this.props.onChange({ place: d.ori, placeType: 'agency' }) } - removeSelection = () => { - this.setState({ showSelection: false, search: '' }) + clearInput = () => { + this.setState({ search: '', hasSelection: false }) } - render() { - const { agencies, ori, state } = this.props - const { showSelection, search } = this.state - - const data = Object.keys(agencies[state]).map(id => ({ - ori: id, - ...agencies[state][id], - })) + toggleResults = () => { + this.setState(prevState => ({ showResults: !prevState.showResults })) + } - const selected = data.find(d => d.ori === ori) + render() { + const { data } = this.props + const { search, hasSelection, showResults } = this.state // get unique set of counties (for result grouping) const counties = {} @@ -42,48 +50,48 @@ class AgencySearch extends Component { return words.includes(searchUpper) }) - const showOris = !!search.length && dataFiltered.length > 0 - return (
- {selected && showSelection - ?
- +
+ + -
- :
-
- -
- {showOris && - a.agency_name > b.agency_name, - )} - groupKey="primary_county" - groupValues={Object.keys(counties).sort()} - onClick={this.handleClick} - />} -
} + +
+ {!hasSelection && + showResults && + dataFiltered.length > 0 && + a.agency_name > b.agency_name)} + groupKey="primary_county" + groupValues={Object.keys(counties).sort()} + onClick={this.handleClick} + />} +
) } } +AgencySearch.propTypes = { + agency: PropTypes.string.isRequired, + data: PropTypes.arrayOf(PropTypes.object).isRequired, +} + export default AgencySearch diff --git a/src/components/Explorer.js b/src/components/Explorer.js index b2f4069d..4948f90e 100644 --- a/src/components/Explorer.js +++ b/src/components/Explorer.js @@ -57,7 +57,7 @@ class Explorer extends React.Component { } render() { - const { appState, dispatch, params, router } = this.props + const { appState, dispatch, params } = this.props const { crime } = params const { place, placeType } = getPlaceInfo(params) @@ -91,7 +91,7 @@ class Explorer extends React.Component { - +
{ - const isNational = place === nationalKey - const isAgency = placeType === 'agency' - const selected = isAgency ? oriToState(place) : place - - return ( -
-
Location
- - {showSearch && - selected && - !isNational && - } -
- ) -} + usState, +}) => ( +
+
Location
+ + {showSearch && + usState !== nationalKey && + } +
+) LocationFilter.defaultProps = { - selected: '', + usState: '', showSearch: false, } diff --git a/src/components/SidebarContainer.js b/src/components/SidebarContainer.js index 916ff56f..cb3b8f3f 100644 --- a/src/components/SidebarContainer.js +++ b/src/components/SidebarContainer.js @@ -6,59 +6,80 @@ import CrimeTypeFilter from './CrimeTypeFilter' import LocationFilter from './LocationFilter' import TimePeriodFilter from './TimePeriodFilter' import { hideSidebar } from '../actions/sidebar' +import { getAgency, oriToState } from '../util/ori' const SidebarContainer = ({ - agencies, - dispatch, + agency, + agencyData, + crime, + hide, filters, isOpen, onChange, -}) => { - const { crime, place, placeType } = filters - const hide = () => dispatch(hideSidebar()) - - return ( - - ) -} + showSearch, + usState, +}) => ( + +) SidebarContainer.propTypes = { onChange: PropTypes.func, } -const mapStateToProps = ({ agencies, filters, sidebar }) => ({ - agencies, - filters, - isOpen: sidebar.isOpen, +const formatAgencyData = (agencies, state) => + Object.keys(agencies[state]).map(id => ({ + ori: id, + ...agencies[state][id], + })) + +const mapStateToProps = ({ agencies, filters, sidebar }) => { + const { agencySearch, crime, place, placeType } = filters + + const isAgency = placeType === 'agency' + const usState = isAgency ? oriToState(place) : place + const agency = isAgency && getAgency(agencies, place) + const agencyData = usState && formatAgencyData(agencies.data, usState) + + return { + agency, + agencyData, + crime, + filters, + isOpen: sidebar.isOpen, + showSearch: agencySearch === 'true', + usState, + } +} +const mapDispatchToProps = dispatch => ({ + hide: () => dispatch(hideSidebar()), }) -const mapDispatchToProps = dispatch => ({ dispatch }) export default connect(mapStateToProps, mapDispatchToProps)(SidebarContainer)