Skip to content

Commit

Permalink
loans: date range search
Browse files Browse the repository at this point in the history
closes #168
  • Loading branch information
topless committed Mar 31, 2020
1 parent 6da7d71 commit 2aeb9e0
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 8 deletions.
6 changes: 4 additions & 2 deletions invenio_app_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
from .documents.api import DOCUMENT_PID_FETCHER, DOCUMENT_PID_MINTER, \
DOCUMENT_PID_TYPE, Document
from .documents.search import DocumentSearch
from .facets import default_value_when_missing_filter, keyed_range_filter, \
not_empty_object_or_list_filter, overdue_agg, overdue_loans_filter
from .facets import date_range_filter, default_value_when_missing_filter, \
keyed_range_filter, not_empty_object_or_list_filter, overdue_agg, \
overdue_loans_filter
from .records.views import UserInfoResource

from .api import ( # isort:skip
Expand Down Expand Up @@ -1191,6 +1192,7 @@ def _(x):
),
filters={
"returns.end_date": overdue_loans_filter("end_date"),
"loan.start_date": date_range_filter("start_date"),
},
post_filters=dict(
state=terms_filter("state"),
Expand Down
20 changes: 19 additions & 1 deletion invenio_app_ils/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# it under the terms of the MIT License; see LICENSE file for more details.

"""Facets and factories for result filtering and aggregation."""
from datetime import timedelta

import arrow
from elasticsearch_dsl.query import Bool, Q, Range
Expand Down Expand Up @@ -113,3 +112,22 @@ def overdue_agg():
)
)
)


# loan.start_date:2020-02-02,2020-12-30
def date_range_filter(field):
"""Create a range filter.
:param field: Field name.
:param values[0]: string with comma separated fromDate and toDate.
"""
def inner(values):
fromDate, toDate = values[0].split(',')
params = {}
if fromDate:
params["gte"] = str(arrow.get(fromDate).date())
if toDate:
params["lte"] = str(arrow.get(toDate).date())
return Range(**{field: params})

return inner
6 changes: 3 additions & 3 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.0",
"react-scroll": "^1.7.16",
"react-searchkit": "^0.16.0",
"react-searchkit": "^0.17.0",
"react-show-more": "^2.0.0",
"react-tagcloud": "^2.0.0",
"redux": "^4.0.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { Component } from 'react';
import { Card } from 'semantic-ui-react';
import { DatePicker } from '@components';
import { withState, onQueryChanged } from 'react-searchkit';
import _fromPairs from 'lodash/fromPairs';
import _toPairs from 'lodash/toPairs';
import _cloneDeep from 'lodash/cloneDeep';

class _SearchDateRange extends Component {
constructor(props) {
super(props);
const [fromDate, toDate] = this.parseUrlDates();
this.state = { fromDate, toDate };
}

parseUrlDates = () => {
const { filters } = this.props.currentQueryState;
const filtersObj = _fromPairs(filters);
const dateFilter = filtersObj['loan.start_date'];
if (dateFilter) return dateFilter.split(',');
return ['', ''];
};

updateFilters = () => {
const newQuery = _cloneDeep(this.props.currentQueryState);
const filtersObj = _fromPairs(newQuery.filters);
filtersObj[
'loan.start_date'
] = `${this.state.fromDate},${this.state.toDate}`;
newQuery.filters = _toPairs(filtersObj);

// NOTE: since its wrapped with `withState` when the pr is merged
// (https://github.com/inveniosoftware/react-searchkit/pull/100) we could
// use this.props.updateStateQuery(newQuery) instead of the events.
onQueryChanged({ searchQuery: newQuery });
};

render() {
return (
<Card>
<Card.Content>
<Card.Header>Date</Card.Header>
<Card.Meta>
<span>*Loan start date</span>
</Card.Meta>
</Card.Content>
<Card.Content>
<DatePicker
maxDate={this.state.toDate}
defaultValue={this.state.fromDate}
placeholder="From"
handleDateChange={value =>
this.setState({ fromDate: value }, this.updateFilters)
}
/>
</Card.Content>
<Card.Content>
<DatePicker
minDate={this.state.fromDate}
defaultValue={this.state.toDate}
placeholder="To"
handleDateChange={value =>
this.setState({ toDate: value }, this.updateFilters)
}
/>
</Card.Content>
</Card>
);
}
}

export const SearchDateRange = withState(_SearchDateRange);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SearchDateRange } from './SearchDateRange';
1 change: 1 addition & 0 deletions ui/src/components/SearchControls/components/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { SearchAggregationsCards } from './SearchAggregations';
export { SearchAggregationsMenu } from './SearchAggregations';
export { SearchDateRange } from './SearchDateRange';
export { SearchFooter } from './SearchFooter';
export { SearchPagination } from './SearchPagination';
export { SearchEmptyResults } from './SearchEmptyResults';
9 changes: 8 additions & 1 deletion ui/src/pages/backoffice/Loan/LoanSearch/LoanSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
SearchAggregationsCards,
} from '@components/SearchControls';

import { SearchDateRange } from '@components/SearchControls/components';

export class LoanSearch extends Component {
searchApi = new InvenioSearchApi({
url: loanApi.searchBaseURL,
Expand Down Expand Up @@ -77,7 +79,11 @@ export class LoanSearch extends Component {
<>
<Header as="h2">Loans and requests</Header>

<ReactSearchKit searchApi={this.searchApi} history={history}>
<ReactSearchKit
searchApi={this.searchApi}
history={history}
eventListenerEnabled={true}
>
<Container fluid className="spaced">
<SearchBar renderElement={this.renderSearchBar} />
</Container>
Expand All @@ -87,6 +93,7 @@ export class LoanSearch extends Component {
<Grid.Column width={3} className={'search-aggregations'}>
<Header content={'Filter by'} />
<SearchAggregationsCards modelName={'loans'} />
<SearchDateRange />
</Grid.Column>
<Grid.Column width={13}>
<SearchEmptyResults extras={this.renderEmptyResultsExtra} />
Expand Down

0 comments on commit 2aeb9e0

Please sign in to comment.