Skip to content

Commit

Permalink
New filter functionality for 'status'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Feb 14, 2017
1 parent bc71a6e commit d9bf5bb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/app/actions/SearchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ export default {
criteria: _criteria,
});
},
changeStatus(_status) {
AppDispatcher.dispatch({
actionType: SearchConstants.CHANGE_STATUS,
status: _status,
});
},
};
33 changes: 32 additions & 1 deletion src/app/components/SearchComponent.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
import React, { Component } from 'react';
import _ from 'underscore';
import SearchActions from '../actions/SearchActions';
import ItemsStore from '../stores/ItemsStore';

export default class SearchComponent extends Component {

constructor(props) {
super(props);
this.state = { status: ItemsStore.statusList() };
}

componentDidMount() {
ItemsStore.addChangeResultListener(this.onChangeResultHandler.bind(this));
}

componentWillUnmount() {
ItemsStore.removeChangeResultListener(this.onChangeResultHandler.bind(this));
}

onChangeResultHandler() {
this.setState({ status: ItemsStore.statusList() });
}

status() {
return _.map(this.state.status, (status, key) => (
<option key={key} value={status}>{status}</option>
));
}

render() {
return (
<input type="search" placeholder="Search" className="uk-input uk-width-1-4@s" onChange={(event) => { SearchActions.changeCriteria(event.target.value) }} />
<div>
<input type="search" placeholder="Search" className="uk-input uk-width-1-4@s" onChange={(event) => { SearchActions.changeCriteria(event.target.value) }} />
<select className="uk-select uk-width-1-4@s" onChange={(event) => { SearchActions.changeStatus(event.target.value) }} >
<option value="">Please select...</option>
{ this.status() }
</select>
</div>
);
}

Expand Down
1 change: 1 addition & 0 deletions src/app/constants/SearchConstants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
CHANGE_CRITERIA: 'CHANGE_CRITERIA',
CHANGE_STATUS: 'CHANGE_STATUS',
};
37 changes: 29 additions & 8 deletions src/app/stores/ItemsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ const state = {
// url: 'items.json',
},
criteria: '',
status: '',
};

let results = [];
const results = {
items: [],
status: [],
};

const ItemsStore = Object.assign({}, EventEmitter.prototype, {
result() {
return results;
return results.items;
},
statusList() {
return results.status;
},
emitChangeResult() {
this.emit(EVENTS.CHANGE_RESULT);
Expand All @@ -43,15 +50,25 @@ const groupByCategories = (items) => {
});
};

const search = (criteria) => {
const updateResultStatus = (items) => {
const status = _.groupBy(items, (item) => {
return item.status;
});

results.status = _.keys(status);
};

const search = () => {
itemsLoader.load(state.endpoint, (items) => {
const rx = new RegExp(criteria, 'i');
updateResultStatus(items);

results = _.filter(items, (item) => {
return rx.test(item.name) || rx.test(item.description);
const criteriaRegExp = new RegExp(state.criteria, 'i');

results.items = _.filter(items, (item) => {
return (criteriaRegExp.test(item.name) || criteriaRegExp.test(item.description)) && (state.status !== '' ? item.status === state.status : true);
});

results = groupByCategories(results);
results.items = groupByCategories(results.items);

ItemsStore.emitChangeResult();
});
Expand All @@ -61,7 +78,11 @@ AppDispatcher.register((action) => {
switch (action.actionType) {
case SearchConstants.CHANGE_CRITERIA:
state.criteria = action.criteria;
search(action.criteria);
search();
break;
case SearchConstants.CHANGE_STATUS:
state.status = action.status;
search();
break;
default:
// no op
Expand Down

0 comments on commit d9bf5bb

Please sign in to comment.