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/LoggedInView.jsx | 64 +-------------------------------- client/components/MealFilter.jsx | 72 +++++++++++++++++++++++++++++++++++--- client/components/MealList.jsx | 17 +++------ client/components/Menu.jsx | 57 ++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 81 deletions(-) create mode 100644 client/components/Menu.jsx (limited to 'client/components') diff --git a/client/components/LoggedInView.jsx b/client/components/LoggedInView.jsx index 5c7a690..3fa3e8b 100644 --- a/client/components/LoggedInView.jsx +++ b/client/components/LoggedInView.jsx @@ -1,5 +1,6 @@ import React from 'react' import ModalDialog from './ModalDialog.jsx' +import Menu from './Menu.jsx' import UserList from './UserList.jsx' import MealList from './MealList.jsx' @@ -45,66 +46,3 @@ export default class LoggedInView extends React.Component { } -class Menu extends React.Component { - constructor() { - super() - this.setGoal = this.setGoal.bind(this) - this.logout = this.logout.bind(this) - } - setGoal() { - const goal = Math.abs(parseInt(prompt('Please enter your calorie goal', this.props.user.goal))) - if (goal) { - client.service('users').patch(this.props.user.id, { - goal: goal, - token: client.get('token'), - }).then((user) => { - this.props.updateUser(user) - }) - } - } - logout() { - client.logout().then(() => { - window.location.reload() - }) - } - render() { - const user = this.props.user - const items = [] - items.push(
  • Hello {user.email}
  • ) - items.push(
  • Goal: {user.goal}
  • ) - switch (user.role) { - case 'admin': - if (this.props.user.id !== this.props.currentUser.id) { - items.push(
  • Reset User
  • ) - } - items.push(
  • Users
  • ) - items.push(
  • Meals
  • ) - break - case 'manager': - items.push(
  • Users
  • ) - items.push(
  • Meals
  • ) - case 'user': - break - } - items.push(
  • Logout
  • ) - - return ( -
    - -
    - ) - // {}} /> - } -} - - -// class ProfileModal extends React.Component { -// render() { -// return ( -// -// -// ) -// } -// } 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(){ diff --git a/client/components/MealList.jsx b/client/components/MealList.jsx index 53c8289..5b0bc1b 100644 --- a/client/components/MealList.jsx +++ b/client/components/MealList.jsx @@ -19,18 +19,6 @@ export default class MealList extends React.Component { this.handleUpdate = this.handleUpdate.bind(this) this.handleDelete = this.handleDelete.bind(this) this.pickMeal = this.pickMeal.bind(this) - - client.service('meals').find({ - query: { - userid: props.user.id, - '$sort': { 'date': '-1' }, - token: client.get('token'), - }, - }).then((data) => { - this.setState(data) - }).catch((error) => { - console.error(error) - }) } handleCreate(meal) { const meals = this.state.data.slice() @@ -79,7 +67,10 @@ export default class MealList extends React.Component { onCreate={(meal) => { this.handleCreate(meal) }} onUpdate={(meal) => { this.handleUpdate(meal) }} /> - + { this.mealFilter = mealFilter }} + onChange={(meals) => { this.setState({ data: meals }) }} + />
    {items}
    diff --git a/client/components/Menu.jsx b/client/components/Menu.jsx new file mode 100644 index 0000000..d53803d --- /dev/null +++ b/client/components/Menu.jsx @@ -0,0 +1,57 @@ + +import React from 'react' + +import client from '../client' + +export default class Menu extends React.Component { + constructor() { + super() + this.setGoal = this.setGoal.bind(this) + this.logout = this.logout.bind(this) + } + setGoal() { + const goal = Math.abs(parseInt(prompt('Please enter your calorie goal', this.props.user.goal))) + if (goal) { + client.service('users').patch(this.props.user.id, { + goal: goal, + token: client.get('token'), + }).then((user) => { + this.props.updateUser(user) + }) + } + } + logout() { + client.logout().then(() => { + window.location.reload() + }) + } + render() { + const user = this.props.user + const items = [] + items.push(
  • Hello {user.email}
  • ) + items.push(
  • Goal: {user.goal}
  • ) + switch (user.role) { + case 'admin': + if (this.props.user.id !== this.props.currentUser.id) { + items.push(
  • Reset User
  • ) + } + items.push(
  • Users
  • ) + items.push(
  • Meals
  • ) + break + case 'manager': + items.push(
  • Users
  • ) + items.push(
  • Meals
  • ) + case 'user': + break + } + items.push(
  • Logout
  • ) + + return ( +
    +
      + {items} +
    +
    + ) + } +} -- cgit v1.2.3-70-g09d2