diff options
Diffstat (limited to 'client/components/MealList.jsx')
| -rw-r--r-- | client/components/MealList.jsx | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/client/components/MealList.jsx b/client/components/MealList.jsx index 7efbf4d..ef13ac9 100644 --- a/client/components/MealList.jsx +++ b/client/components/MealList.jsx @@ -51,6 +51,7 @@ export default class MealList extends React.Component { 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] @@ -59,6 +60,7 @@ export default class MealList extends React.Component { <MealItem key={meal.id} meal={meal} + canEdit={canEdit} onClick={this.pickMeal} onDelete={this.handleDelete} /> ) @@ -81,6 +83,7 @@ export default class MealList extends React.Component { return ( <div> <MealForm user={this.props.user} + currentUser={this.props.currentUser} ref={(mealForm) => { this.mealForm = mealForm }} onCreate={(meal) => { this.handleCreate(meal) }} onUpdate={(meal) => { this.handleUpdate(meal) }} @@ -100,8 +103,14 @@ export default class MealList extends React.Component { 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 @@ -116,11 +125,11 @@ class MealItem extends React.Component { render() { const meal = this.props.meal // const canEdit = this.props.meal.userid === this.props.currentUser.id ? 'canEdit' : '' - const canEdit = 'canEdit' + const canEdit = this.props.canEdit ? 'canEdit' : '' const date = parseDate(meal.date) const time = parseTime(meal.date) return ( - <div className={'meal row ' + canEdit} onClick={() => this.props.onClick(meal)}> + <div className={'meal row ' + canEdit} onClick={this.handleClick}> <div className='name'>{meal.name}</div> <div className='calories'>{meal.calories} cal</div> <div className='date'>{date}</div> @@ -213,6 +222,10 @@ class MealForm extends React.Component { render() { const id = this.state.id const action = id ? 'update' : 'create' + const canEdit = canEditUserMeals(this.props.currentUser, this.props.user) + if (! canEdit) { + return (<div></div>) + } const date = parseDate(this.state.date) const time = parseTime(this.state.date) @@ -232,6 +245,11 @@ class MealForm extends React.Component { } } +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) |
