import React from 'react'
import client from '../client'
import MealFilter from './MealFilter.jsx'
export default class MealList extends React.Component {
constructor(props) {
super()
this.state = {
total: 0,
limit: 0,
skip: 0,
data: []
}
this.handleCreate = this.handleCreate.bind(this)
this.handleUpdate = this.handleUpdate.bind(this)
this.handleDelete = this.handleDelete.bind(this)
this.pickMeal = this.pickMeal.bind(this)
this.loadMeals = this.loadMeals.bind(this)
}
handleCreate(meal) {
const meals = this.state.data.slice()
meals.unshift( meal )
this.setState({
data: meals.sort(sortByDate)
})
}
handleUpdate(meal) {
const meals = this.state.data.map((data) => {
return (data.id === meal.id) ? meal : data
}).sort(sortByDate)
this.setState({
data: meals
})
}
handleDelete(mealid) {
const meals = this.state.data.filter((data) => {
return data.id !== mealid
}).sort(sortByDate)
this.setState({
data: meals
})
}
pickMeal(meal) {
this.mealForm.pick(meal)
}
loadMeals(meals) {
this.setState({ data: meals })
}
render() {
const canEdit = canEditUserMeals(this.props.currentUser, this.props.user)
var groups = groupByDate(this.state.data)
const items = Object.keys(groups).sort().reverse().map((date) => {
const group = groups[date]
const mealitems = group.meals.map((meal) => {
return (
)
})
const isOverLimit = group.calories > this.props.user.goal ? 'isOverLimit' : 'isUnderLimit'
return (
{group.date}
{group.calories} cal
{mealitems}
)
})
if (! items.length) {
items.push(
No meals found
)
}
return (
{ this.mealForm = mealForm }}
onCreate={(meal) => { this.handleCreate(meal) }}
onUpdate={(meal) => { this.handleUpdate(meal) }}
/>
{ this.mealFilter = mealFilter }}
onChange={ this.loadMeals }
/>
{items}
)
}
}
class MealItem extends React.Component {
constructor() {
super()
this.handleClick = this.handleClick.bind(this)
this.remove = this.remove.bind(this)
}
handleClick() {
if (this.props.canEdit) {
this.props.onClick(this.props.meal)
}
}
remove(e) {
e.stopPropagation()
const mealid = this.props.meal.id
const mealsService = client.service('meals')
const params = { query: { token: client.get('token') } }
mealsService.remove(mealid, params).then(result => {
this.props.onDelete(mealid)
}).catch(error => {
console.error(error)
})
}
render() {
const meal = this.props.meal
// const canEdit = this.props.meal.userid === this.props.currentUser.id ? 'canEdit' : ''
const canEdit = this.props.canEdit ? 'canEdit' : ''
const date = parseDate(meal.date)
const time = parseTime(meal.date)
return (
{meal.name}
{meal.calories} cal
{date}
{time}
x
)
}
}
class MealForm extends React.Component {
constructor(props) {
super()
this.state = {
id: '',
userid: props.user.id,
name: '',
calories: '',
date: new Date (),
}
this.updateState = this.updateState.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
reset() {
this.setState({
id: '',
name: '',
calories: '',
date: new Date (),
})
}
pick(meal){
this.setState(meal)
}
updateState(event){
const name = event.target.name
let value = event.target.value
if (name === 'date') {
value = new Date(value + 'T' + this.state.date.split("T")[1] ).toString()
} else if (name === 'time') {
value = new Date(this.state.date.split("T")[0] + value).toString()
} else if (name === 'calories') {
value = parseInt(value)
}
this.setState({
[name]: value,
error: null,
})
}
handleSubmit(event) {
event.preventDefault()
const id = this.state.id
if (! id) {
this.create()
}
else {
this.update()
}
}
create() {
const mealsService = client.service('meals')
const params = { query: { token: client.get('token') } }
mealsService.create(this.state, params).then(result => {
this.props.onCreate(result)
this.reset()
}).catch(error => {
console.error(error)
this.setState({
error: error.toString()
})
})
}
update() {
const mealsService = client.service('meals')
const params = { query: { token: client.get('token') } }
mealsService.update(this.state.id, this.state, params).then(result => {
this.props.onUpdate(result)
this.reset()
}).catch(error => {
console.error(error)
this.setState({
error: error.toString()
})
})
}
render() {
const id = this.state.id
const action = id ? 'update' : 'create'
const canEdit = canEditUserMeals(this.props.currentUser, this.props.user)
if (! canEdit) {
return ()
}
const date = parseDate(this.state.date)
const time = parseTime(this.state.date)
return (
)
}
}
function canEditUserMeals (currentUser, user) {
const isValidRole = (currentUser.role === 'admin')
return (user.id == currentUser.id) || isValidRole
}
function groupByDate(a) {
return a.reduce(function(rv, x) {
var date = parseDate(x.date)
var ab = rv[date] = rv[date] || { date: date, calories: 0, meals: [] }
ab.meals.push(x)
ab.calories += x.calories
return rv
}, {})
}
function sortByDate(a,b){
return new Date(b.date) - new Date(a.date)
}
function parseDate(d){
return new Date(d).toISOString().substr(0, 10)
}
function parseTime(d){
return new Date(d).toISOString().substr(11, 5)
}
function capitalize(s){
s = s || '';
return (s[0] || '').toUpperCase() + s.substr(1)
}