Updated Search to include checks for isLoading, isError and if no results found

• Updated:
- Search to include checks for isLoading, isError and if no results found
This commit is contained in:
mgabdev 2020-07-16 13:27:36 -05:00
parent ad29b3aca6
commit b3e76b5a58
2 changed files with 69 additions and 7 deletions

View File

@ -2,15 +2,20 @@ import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component' import ImmutablePureComponent from 'react-immutable-pure-component'
import { withRouter } from 'react-router-dom' import { withRouter } from 'react-router-dom'
import { me } from '../initial_state' import { me } from '../initial_state'
import ResponsiveClassesComponent from '../features/ui/util/responsive_classes_component'
import HashtagItem from '../components/hashtag_item' import HashtagItem from '../components/hashtag_item'
import GroupListItem from '../components/group_list_item' import GroupListItem from '../components/group_list_item'
import Text from '../components/text' import Text from '../components/text'
import Account from '../components/account' import Account from '../components/account'
import PanelLayout from '../components/panel/panel_layout' import PanelLayout from '../components/panel/panel_layout'
import ColumnIndicator from '../components/column_indicator'
import Block from '../components/block'
const mapStateToProps = (state) => ({ const mapStateToProps = (state) => ({
isError: state.getIn(['search', 'isError']),
isLoading: state.getIn(['search', 'isLoading']),
results: state.getIn(['search', 'results']), results: state.getIn(['search', 'results']),
suggestions: state.getIn(['suggestions', 'items']), submitted: state.getIn(['search', 'submitted']),
}); });
export default export default
@ -19,8 +24,11 @@ export default
class Search extends ImmutablePureComponent { class Search extends ImmutablePureComponent {
static propTypes = { static propTypes = {
results: ImmutablePropTypes.map.isRequired, isError: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired,
location: PropTypes.object, location: PropTypes.object,
results: ImmutablePropTypes.map.isRequired,
submitted: PropTypes.bool.isRequired,
} }
state = { state = {
@ -28,11 +36,39 @@ class Search extends ImmutablePureComponent {
} }
render() { render() {
const { results, location } = this.props const {
isError,
isLoading,
location,
results,
submitted,
} = this.props
const { isSmallScreen } = this.state const { isSmallScreen } = this.state
if (results.isEmpty() && isSmallScreen) { if (isLoading) {
return null return <ColumnIndicator type='loading' />
}
if (isError) {
return (
<ResponsiveClassesComponent classNamesXS={_s.px10}>
<Block>
<ColumnIndicator type='error' message='Error fetching search results.' />
</Block>
</ResponsiveClassesComponent>
)
}
if ((results.isEmpty() && isSmallScreen) || (!submitted && results.isEmpty())) {
return (
<ResponsiveClassesComponent classNamesXS={_s.px10}>
<Block>
<div className={[_s.default, _s.py15, _s.px15].join(' ')}>
<Text>Type in the box above and submit to perform a search.</Text>
</div>
</Block>
</ResponsiveClassesComponent>
)
} }
const pathname = location.pathname || '' const pathname = location.pathname || ''
@ -133,6 +169,16 @@ class Search extends ImmutablePureComponent {
) )
} }
if (!accounts && !statuses && !hashtags && !groups) {
return (
<ResponsiveClassesComponent classNamesXS={_s.px10}>
<Block>
<ColumnIndicator type='missing' message='No search results.' />
</Block>
</ResponsiveClassesComponent>
)
}
return ( return (
<div> <div>
{accounts} {accounts}

View File

@ -1,6 +1,8 @@
import { import {
SEARCH_CHANGE, SEARCH_CHANGE,
SEARCH_CLEAR, SEARCH_CLEAR,
SEARCH_FETCH_REQUEST,
SEARCH_FETCH_FAIL,
SEARCH_FETCH_SUCCESS, SEARCH_FETCH_SUCCESS,
SEARCH_SHOW, SEARCH_SHOW,
} from '../actions/search'; } from '../actions/search';
@ -14,11 +16,23 @@ const initialState = ImmutableMap({
value: '', value: '',
submitted: false, submitted: false,
hidden: false, hidden: false,
isLoading: false,
isError: false,
results: ImmutableMap(), results: ImmutableMap(),
}); });
export default function search(state = initialState, action) { export default function search(state = initialState, action) {
switch(action.type) { switch(action.type) {
case SEARCH_FETCH_REQUEST:
return state.withMutations(map => {
map.set('isError', false);
map.set('isLoading', true);
});
case SEARCH_FETCH_FAIL:
return state.withMutations(map => {
map.set('isError', true);
map.set('isLoading', false);
});
case SEARCH_CHANGE: case SEARCH_CHANGE:
return state.withMutations(map => { return state.withMutations(map => {
map.set('value', action.value); map.set('value', action.value);
@ -30,6 +44,8 @@ export default function search(state = initialState, action) {
map.set('results', ImmutableMap()); map.set('results', ImmutableMap());
map.set('submitted', false); map.set('submitted', false);
map.set('hidden', false); map.set('hidden', false);
map.set('isLoading', false);
map.set('isError', false);
}); });
case SEARCH_SHOW: case SEARCH_SHOW:
return state.set('hidden', false); return state.set('hidden', false);
@ -42,7 +58,7 @@ export default function search(state = initialState, action) {
statuses: ImmutableList(action.results.statuses.map(item => item.id)), statuses: ImmutableList(action.results.statuses.map(item => item.id)),
hashtags: fromJS(action.results.hashtags), hashtags: fromJS(action.results.hashtags),
groups: fromJS(action.results.groups), groups: fromJS(action.results.groups),
})).set('submitted', true); })).set('submitted', true).set('isLoading', false).set('isError', false);
default: default:
return state; return state;
} }