From 62b0c3476f17a505581866e105bb79cf0f73edb1 Mon Sep 17 00:00:00 2001 From: KhaledaNYC Date: Thu, 6 Oct 2016 19:03:07 -0400 Subject: [PATCH] added edit form not quite working yet --- src/actions/index.js | 16 +++ src/components/menus/MenuEdit.js | 69 ++++++++++++ src/components/menus/MenuShow.js | 173 ++++++++++++++++++++++++------- src/reducers/menus_reducer.js | 3 + src/routes.js | 3 + 5 files changed, 225 insertions(+), 39 deletions(-) create mode 100644 src/components/menus/MenuEdit.js diff --git a/src/actions/index.js b/src/actions/index.js index dfe7e93..c51433b 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -134,3 +134,19 @@ export function addUser(newUserFromForm) { return {type: 'ADD_USER', payload: newUserFromApi} } + + +export function updateMenu(menu) { + const request = fetch(`http://localhost:5000/api/v1/menus/${menu.id}`, { + method: 'PUT', + headers: new Headers({ + 'Content-Type': 'application/json' + }), + body: JSON.stringify({menu: menu}) + }).then(responseMenu => { + return responseMenu.json() + }).then(updateMenuPayload => { + return updateMenuPayload + }) + return {type: 'UPDATE_CAT',request} +} diff --git a/src/components/menus/MenuEdit.js b/src/components/menus/MenuEdit.js new file mode 100644 index 0000000..3e857f8 --- /dev/null +++ b/src/components/menus/MenuEdit.js @@ -0,0 +1,69 @@ +import React from 'react' +import * as actions from '../../actions' +import { bindActionCreators } from 'redux' +import { connect } from 'react-redux' + +class MenuEdit extends React.Component { + + constructor(props){ + super(props) + this.newMenuHandler = this.newMenuHandler.bind(this) + } + + newMenuHandler(event){ + event.preventDefault() + let recipes = this.props.recipes + let checkedRecipes = recipes.filter((recipe) => this.refs[recipe.id].checked ) + let idRecipes = checkedRecipes.map((recipe) => (recipe.id)) + + const newMenu = { + name: this.refs.name.value, + occasion: this.refs.occasion.value, + description: this.refs.description.value, + recipe_ids: idRecipes + } + this.props.actions.addMenu(newMenu) + } + + makeRecipes() { + let recipes = this.props.recipes + return recipes.map((recipe) =>
) +} + + render(){ + return ( +
+
+ +
+ +
+ +
+ {this.makeRecipes()} + +
+
+ ) + } +} + + +function mapDispatchToProps(dispatch){ + return {actions: bindActionCreators(actions, dispatch)} +} + +function mapStateToProps(state, ownProps) { + if (state.recipes.length > 0) { + return {recipes: state.recipes} + } + else { + return {recipes: [{name: ''}]} + } +} + +const componentCreator = connect(mapStateToProps, mapDispatchToProps) +export default componentCreator(MenuEdit) diff --git a/src/components/menus/MenuShow.js b/src/components/menus/MenuShow.js index 4b629c6..50ed8ca 100644 --- a/src/components/menus/MenuShow.js +++ b/src/components/menus/MenuShow.js @@ -1,6 +1,6 @@ import React from 'react'; import {connect} from 'react-redux'; -import MenuCreate from './MenuCreate' +import MenuEdit from './MenuEdit' import ReduxPromise from 'redux-promise'; import ReduxThunk from 'redux-thunk' import * as actions from '../../actions' @@ -8,69 +8,164 @@ import { bindActionCreators } from 'redux' class MenuShow extends React.Component { - constructor(){ - super(); + constructor(props,context){ + super(props,context); + this.state ={ + isEditing: false, + menu: this.props.cat, + menuRecipes: this.props.menuRecipes, + checkBoxRecipes: this.props.checkBoxRecipes + }; this.appetizers = this.appetizers.bind(this) this.mains = this.mains.bind(this) this.dessert = this.dessert.bind(this) + this.toggleEdit = this.toggleEdit.bind(this) + this.updateMenuState = this.updateMenuState.bind(this) + this.updateMenuRecipes = this.updateMenuRecipes.bind(this); + this.saveMenu = this.saveMenu.bind(this) + } + updateMenuState(event) { + const field = event.target.name; + const menu = this.state.cat; + menu[field] = event.target.value; + return this.setState({menu:menu}) } + updateMenuRecipes(event) { + const menu = this.state.menu; + const recipeId = event.target.value; + const recipe = this.state.checkBoxHobbies.filter(recipe => recipe.id == recipeId)[0]; + const checked = !recipe.checked; + recipe['checked'] = !recipe.checked; + if (checked) { + menu.recipe_ids.push(recipe.id); + } else { + menu.recipe_ids.splice(menu.recipe_ids.indexOf(recipe.id)); + } + this.setState({menu: menu}); -appetizers(){ + } + componentWillReceiveProps(nextProps) { + if (this.props.menu.id != nextProps.menu.id) { + this.setState({menu: nextProps.menu}); + } + if (this.props.checkBoxRecipes.length < nextProps.checkBoxRecipes.length) { + this.setState({menuRecipes: nextProps.menuRecipes, checkBoxRecipes: nextProps.checkBoxRecipes}); + } +} -return this.props.menu.recipes.filter(recipe => recipe.course == 'appetizer').map(recipe =>
  • {recipe.name}
  • ) + toggleEdit(){ + this.setState({ + isEditing: !this.state.isEditing + }) + } + saveMenu(event) { + event.preventDefault(); + this.props.actions.updateMenu(this.state.menu); + } +appetizers(){ + return this.props.menu.recipes.filter(recipe => recipe.course == 'appetizer').map(recipe =>
  • {recipe.name}
  • ) } - mains(){ +mains(){ return this.props.menu.recipes.filter(recipe => recipe.course == 'main').map(recipe =>
  • {recipe.name}
  • ) } - dessert(){ - return this.props.menu.recipes.filter(recipe => recipe.course == 'dessert').map(recipe =>
  • {recipe.name}
  • ) +dessert(){ + return this.props.menu.recipes.filter(recipe => recipe.course == 'dessert').map(recipe =>
  • {recipe.name}
  • ) + } +render(){ + if (this.state.isEditing) { + return( +
    +

    Edit Menu

    + +
    + ) } - render(){ - return( -
    -
    -

    {this.props.menu.name}

    -
    {this.props.menu.occasion}
    -
    {this.props.menu.description}
    -

    Appetizers

    -
      -
    • Deviled Eggs
    • - {this.appetizers()} -
    -

    Mains

    -
      -
    • Jambalaya
    • - {this.mains()} + return( + + + +
      +
      +

      {this.props.menu.name}

      +
      {this.props.menu.occasion}
      +
      {this.props.menu.description}
      +

      Appetizers

      +
        + {this.appetizers()} +
      +

      Mains

      +
        + {this.mains()} +
      +

      Desserts

      +
        + {this.dessert()}
      -

      Desserts

      -
        -
      • Beignets
      • - {this.dessert()} -
      +
      +
      -
    - ) + ) + } +} +function recipesForMenus(recipes,menu=null) { + return recipes.map( recipe => { + if (menu && menu.recipes.filter(recipeId => recipeId.id == recipe.id).length > 0) { + recipe['checked'] = true; + } else { + recipe['checked'] = false; } + return recipe; +}); + +} +function collectMenuRecipes(recipes, menu) { + let selected = recipes.map(recipe => { + if (menu.recipes.filter(recipeId => recipeId.id == recipe.id).length > 0) { + return recipe; + } + }) + return selected.filter(r => r != undefined) } function mapDispatchToProps(dispatch){ return {actions: bindActionCreators(actions, dispatch)} } function mapStateToProps(state, ownProps){ -console.log('mapStateToProps',state, ownProps); - if (state.menus.length > 0){ - const menu = state.menus.find((menu) => { + console.log(state); + const stateRecipes = Object.assign([],state.recipes) + let checkBoxRecipes =[]; + let menuRecipes = []; + let menu = {menu: [{name: '', occasion: '', description: '', recipes: [{name: ''}]}]} + if (ownProps.params.id && state.menus.length > 0 && state.recipes.length > 0){ + menu = state.menus.find((menu) => { return menu.id == ownProps.params.id - }) - return {menu: menu} - } else { - return {menu: [{name: '', occasion: '', description: '', recipes: [{name: ''}]}]} - } + }); + if (menu.recipes.length > 0) { + checkBoxRecipes = recipesForMenus(stateRecipes,menu); + menuRecipes = collectMenuRecipes(stateRecipes,menu); + } else { + checkBoxRecipes = recipesForMenus(stateRecipes) + } + } + return { + menu: menu, + checkBoxRecipes: checkBoxRecipes, + menuRecipes:menuRecipes + }; } + + + + const componentCreator = connect(mapStateToProps,mapDispatchToProps) export default componentCreator(MenuShow); diff --git a/src/reducers/menus_reducer.js b/src/reducers/menus_reducer.js index a4a549c..a463bce 100644 --- a/src/reducers/menus_reducer.js +++ b/src/reducers/menus_reducer.js @@ -5,6 +5,9 @@ export default function menusReducer(state=[], action) { return action.payload; case 'ADD_MENU': return [...state, action.payload] + case 'UPDATE_MENU': + return [...state.filter(cat => cat.id !== action.cat.id), + Object.assign({}),action.cat] default: return state; } diff --git a/src/routes.js b/src/routes.js index 7af9c9b..a4cf684 100644 --- a/src/routes.js +++ b/src/routes.js @@ -5,6 +5,8 @@ import App from './components/App' import MenusIndex from './components/menus/MenusIndex' import MenuCreate from './components/menus/MenuCreate' import MenuShow from './components/menus/MenuShow' +import MenuEdit from './components/menus/MenuEdit' + import RecipeIndex from './components/recipes/RecipeIndex' import RecipeCreate from './components/recipes/RecipeCreate' @@ -34,6 +36,7 @@ export default( +