diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-03-20 01:59:47 +0100 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-03-20 01:59:47 +0100 |
| commit | d9d81925299aa787cbdb815cb4b06e17a412b40c (patch) | |
| tree | 601ea37cc6f7e5c6aa3ac1e886d056c2df107779 /client/components/MealFilter.jsx | |
| parent | bf7ac6af587f68553b83a54fcb724dfc9d684644 (diff) | |
filter by date
Diffstat (limited to 'client/components/MealFilter.jsx')
| -rw-r--r-- | client/components/MealFilter.jsx | 72 |
1 files changed, 67 insertions, 5 deletions
diff --git a/client/components/MealFilter.jsx b/client/components/MealFilter.jsx index cd78f89..e9dfbd8 100644 --- a/client/components/MealFilter.jsx +++ b/client/components/MealFilter.jsx @@ -9,25 +9,87 @@ import { DateRange } from 'react-date-range'; export default class MealFilter extends React.Component { constructor(){ super() + const start = new Date () + start.setHours(0) + start.setMinutes(0) + start.setSeconds(0) + start.setDate(start.getDate()-6) + const end = new Date () - end.setDate(end.getDate()-6) + end.setHours(23) + end.setMinutes(59) + end.setSeconds(59) + this.state = { startDate: moment(start), endDate: moment(end), startTime: 0, endTime: 23, + meals: [], } this.handleSelect = this.handleSelect.bind(this) + this.fetch = this.fetch.bind(this) + this.filter = this.filter.bind(this) + } + fetch (state){ + state = state || this.state + let start = state.startDate + let end = state.endDate + console.log(start.toDate(), end.toDate()) + + client.service('meals').find({ + query: { + userid: this.props.user.id, + date: { $gte: start.toDate(), $lte: end.toDate() }, + $sort: { 'date': '-1' }, + token: client.get('token'), + }, + }).then((data) => { + console.log(data) + this.setState({meals: data.data}) + this.filter(data.data) + }).catch((error) => { + console.error(error) + }) + } + filter(meals) { + meals = meals || this.state.meals + const start = this.state.startTime + const end = this.state.endTime + const filteredMeals = this.state.meals.filter((meal) => { + const hours = new Date(meal.date).getHours() + return (start <= hours && hours <= end) + }) + this.props.onChange(filteredMeals) } handleSelect(range){ - console.log(range) - // range.startDate; // Momentjs object - // range.endDate - this.setState(range) + const start = range.startDate + start.hours(0) + start.minutes(0) + start.seconds(0) + + let end = range.endDate + if (start.isSame(end)) { + end = start.clone() + } + + end.hours(23) + end.minutes(59) + end.seconds(59) + + this.setState({ + startDate: start, + endDate: end, + }) + this.fetch({ + startDate: start, + endDate: end, + }) } pickTimeRange(event,start,end){ this.setState({startTime: start, endTime: end}) + this.filter() } render(){ |
