diff --git a/package.json b/package.json index 9d7a88e..be01922 100644 --- a/package.json +++ b/package.json @@ -13,4 +13,4 @@ "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" } -} \ No newline at end of file +} diff --git a/src/App.js b/src/App.js index dd2692a..c5dd5d1 100644 --- a/src/App.js +++ b/src/App.js @@ -2,19 +2,22 @@ import React, { Component } from 'react'; import './App.css'; import MadLibs from './madlibs/MadLibs.js'; import Story from './components/Story.js'; +import MadlibForm from './components/MadlibForm.js'; + class App extends Component { constructor() { super(); this.state = { - selectedMadLib: MadLibs[0] + selectedMadLib: MadLibs[getRandomInt(MadLibs.length)], + showStory: false, }; } // Update the value of a word in the selected // mad lib using setState - updateWord(key, value) { + updateWord = (key, value) => { const updatedMadLib = this.state.selectedMadLib; const changedWord = updatedMadLib.words.find((word) => { return word.key === key @@ -23,21 +26,41 @@ class App extends Component { this.setState({selectedMadLib: updatedMadLib}); } + renderStory = () => { + this.setState( { + showStory: true, + }) + } + render() { + const showStory = this.state.showStory; + + const story = showStory ? ( + + ) : ( + '' + ); + return (

Welcome to MadLibs!

Fill in all of the choices to see your final story.

- {/* - Render your form with input values - */} - + + { story }
); } } +function getRandomInt(max) { + return Math.floor(Math.random() * Math.floor(max)); +} + export default App; diff --git a/src/components/MadlibForm.css b/src/components/MadlibForm.css new file mode 100644 index 0000000..e69de29 diff --git a/src/components/MadlibForm.js b/src/components/MadlibForm.js new file mode 100644 index 0000000..d9bc5ff --- /dev/null +++ b/src/components/MadlibForm.js @@ -0,0 +1,61 @@ +import React, { Component } from 'react'; +import './MadlibForm.css'; +import PropTypes from 'prop-types' + +class MadlibForm extends Component { + + constructor() { + super(); + } + + static propTypes = { + // addStudent: PropTypes.func.isRequired, + } + + wordValid = () => { + return this.state.email.match(/\S+@\S+/) + } + + // clearForm = () => { + // + // } + + onSubmit = (event) => { + event.preventDefault(); + this.props.renderStory() + // this.clearForm(); + } + + render() { + + const inputs = this.props.words.map (item => { + return
+ + + { this.props.updateWord(item.key, event.target.value) }} + /> +
+ }); + + return ( +
+
+ {inputs} + +
+
+ ); + } +} + +export default MadlibForm;