Skip to content

Instantly share code, notes, and snippets.

@Jeyloh
Created June 12, 2018 21:18
Show Gist options
  • Save Jeyloh/8c5aff51b2a19b9df34dfae8700712f2 to your computer and use it in GitHub Desktop.
Save Jeyloh/8c5aff51b2a19b9df34dfae8700712f2 to your computer and use it in GitHub Desktop.
class App extends Component {
state = {
name: "",
ingredient: "",
direction: "",
ingredients: [],
directions: []
}
componentDidMount() {
this.props.subscribeToDeletedRecipes()
this.props.subscribeToNewRecipes()
}
onChange = (key, value) => {
this.setState({[key]: value})
}
addIngredient = () => {
if (this.state.ingredient === '') return
const ingredients = this.state.ingredients
ingredients.push(this.state.ingredient)
this.setState({
ingredient: ''
})
}
addDirection = () => {
if (this.state.direction === '') return
const directions = this.state.directions
directions.push(this.state.direction)
this.setState({
direction: ''
})
}
addRecipe = () => {
const { name, ingredients, directions } = this.state
this.props.onAdd({
id: uuidv4(), name, ingredients, directions
})
this.setState({name: '', directions: [], ingredients: []})
}
render() {
console.log(`props: `, this.props)
return (
<div className="App">
<Header />
<CookbookWrapper name={this.state.name}
ingredient={this.state.ingredient}
direction={this.state.direction}
ingredients={this.state.ingredients}
directions={this.state.directions}
onChange={this.onChange}
addIngredient={this.addIngredient}
addDirection={this.addDirection}
addRecipe={this.addRecipe}
/>
<RecipeOverview recipes={this.props.recipes} deleteRecipeById={this.props.deleteRecipeById} />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment