this.request("PUT", `/recipes/${this.state.id}`, { name: this.state.name }).then(recipe =>{ // loop through and add or update ingredients this.state.ingredients.forEach((item, index) =>{ if(item.isNew) { this.request("POST", "/ingredients", {recipeId: this.state.id, name: item.name}) } else { this.request("PUT", `/ingredients/${item.id}`, { recipeId: this.state.id, name: item.name }); } }); // delete ingredients marked for deletion this.ingredientsToDelete.forEach((item, index) => { this.request("DELETE", `/ingredients/${item.id}`) }); // loop through and add or update directions this.state.directions.forEach((item, index) =>{ if(item.isNew) { this.request("POST", "/directions", {recipeId: this.state.id, name: item.name}) } else { this.request("PUT", `/directions/${item.id}`, { recipeId: this.state.id, name: item.name }); } }); // delete directions marked for deletion this.directionsToDelete.forEach((item, index) => { this.request("DELETE", `/directions/${item.id}`) }); Events.broadcast('notification', 'Recipe updated', 'success') Helper.Redirect('/recipes'); });
this.request("POST", `/recipes/${Authentication.getUserInfo().id}`, { name: this.state.name }).then(recipe => { let id = recipe.id; // loop through and add ingredients this.state.ingredients.forEach((item, index) =>{ this.request("POST", "/ingredients", {recipeId: id, name: item.name}); }); // loop through and add directions this.state.directions.forEach((item, index) =>{ this.request("POST", "/directions", {recipeId: id, name: item.name}); }); Events.broadcast('notification', 'Recipe added', 'success') Helper.Redirect('/recipes'); });
Authentication.login(username, password).then((user) => { Events.broadcast('login'); Helper.Redirect(); });