From d9d81925299aa787cbdb815cb4b06e17a412b40c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 20 Mar 2017 01:59:47 +0100 Subject: filter by date --- client/components/MealFilter.jsx | 72 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) (limited to 'client/components/MealFilter.jsx') 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(){ -- cgit v1.2.3-70-g09d2