From a19e562b3ca8bc7a2c18002dc535b798916bc82c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 20 Mar 2017 05:07:10 +0100 Subject: block managers from editing user meals --- client/components/MealList.jsx | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'client/components/MealList.jsx') 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 { ) @@ -81,6 +83,7 @@ export default class MealList extends React.Component { return (
{ 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 ( -
this.props.onClick(meal)}> +
{meal.name}
{meal.calories} cal
{date}
@@ -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 (
) + } 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) -- cgit v1.2.3-70-g09d2