Skip to content

Commit 1e56648

Browse files
committed
Customer filters
1 parent 9c5b0f4 commit 1e56648

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

app/models/customer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Customer < ApplicationRecord
2+
scope :company_name_contains, -> (value) { where('company_name ILIKE ?', "%#{value.join}%") }
23
end

app/resources/customer_resource.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class CustomerResource < JSONAPI::Resource
2+
extend ModelFilter
23
attributes :company_name,
34
:contact_name,
45
:contact_title,
@@ -12,4 +13,5 @@ class CustomerResource < JSONAPI::Resource
1213
:created_at
1314

1415
paginator :paged
16+
model_filters :company_name_contains
1517
end

client/src/components/Customers/CustomerList.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React, { Component, PropTypes } from 'react';
22
import { Link } from 'react-router';
33
import { find, keyBy } from 'lodash';
4+
import { Button } from 'reactstrap';
45

56
import { ListTable } from '../UI';
67
import { withResourceList } from '../../hocs';
8+
import CustomerListFilter from './CustomerListFilter';
79

810
const formatDate = date => (new Date(date)).toLocaleString();
911

@@ -14,6 +16,7 @@ export class CustomerList extends Component {
1416
}
1517

1618
render() {
19+
const { onFilter } = this.props;
1720
const columns = [
1821
{
1922
attribute: 'companyName',
@@ -36,9 +39,21 @@ export class CustomerList extends Component {
3639
];
3740

3841
return (
39-
<ListTable {...this.props} columns={columns} />
42+
<div>
43+
<Button tag={Link} to={'/customers/new'}>New Customer</Button>
44+
45+
<CustomerListFilter
46+
onSubmit={onFilter}>
47+
</CustomerListFilter>
48+
49+
<ListTable {...this.props} columns={columns} />
50+
</div>
4051
);
4152
}
4253
}
4354

55+
export const mapStateToProps = state => ({
56+
filter: get(state, 'form.customerListFilter.values') || {}
57+
});
58+
4459
export default withResourceList('customers')(CustomerList);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { Component, PropTypes } from 'react';
2+
import { isEmpty } from 'lodash';
3+
import { Field, reduxForm } from 'redux-form';
4+
import { Form, Row, Col } from 'reactstrap';
5+
6+
import { InputField, SelectField } from '../../forms';
7+
8+
class CustomerListFilter extends Component {
9+
render() {
10+
const { handleSubmit, onSubmit } = this.props;
11+
12+
const submitOnChange = () => setTimeout(() => handleSubmit(onSubmit)(), 0);
13+
14+
15+
return (
16+
<Form onSubmit={handleSubmit}>
17+
<Row>
18+
<Col md={8}>
19+
<Field
20+
name="company_name_contains"
21+
label="Company Name Contains"
22+
component={InputField}
23+
onChange={submitOnChange}
24+
/>
25+
</Col>
26+
</Row>
27+
</Form>
28+
);
29+
}
30+
}
31+
32+
export default reduxForm({
33+
form: 'customerListFilter',
34+
destroyOnUnmount: false,
35+
})(CustomerListFilter);

client/src/components/Routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class Routes extends PureComponent {
3636
<Route path="/users" component={UserIsAdmin(UserList)}/>
3737
<Route path="/users/:id" component={UserIsAdmin(UserEdit)}/>
3838
<Route path="/customers" component={UserIsAdmin(CustomerList)}/>
39+
<Route path="/customers/new" component={UserIsAdmin(CustomerEdit)}/>
3940
<Route path="/customers/:id" component={UserIsAdmin(CustomerEdit)}/>
4041
</Route>
4142
<Route path="/login" component={Login}/>

0 commit comments

Comments
 (0)