Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ampers: Nora Peters #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
}
41 changes: 32 additions & 9 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ? (
<Story
title={ this.state.selectedMadLib.title }
text={ this.state.selectedMadLib.getText() }
/>
) : (
''
);

return (
<section className="App">
<h1>Welcome to MadLibs!</h1>
<p>Fill in all of the choices to see your final story.</p>
{/*
Render your form with input values
*/}
<Story
title={ this.state.selectedMadLib.title }
text={ this.state.selectedMadLib.getText() }
/>
<MadlibForm
renderStory = { this.renderStory }
words={this.state.selectedMadLib.words}
updateWord={ this.updateWord }
/>
{ story }
</section>
);
}
}

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

export default App;
Empty file added src/components/MadlibForm.css
Empty file.
61 changes: 61 additions & 0 deletions src/components/MadlibForm.js
Original file line number Diff line number Diff line change
@@ -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 <div key={ item.key }>
<label htmlFor={item.key}>
{item.label}:
</label>
<input
name={item.label}
// placeholder={item.label}
onChange={(event) =>
{ this.props.updateWord(item.key, event.target.value) }}
/>
</div>
});

return (
<div>
<form onSubmit={this.onSubmit}
className="new-madlib-form">
{inputs}
<input
className="button success"
type="submit"
value="Submit"
/>
</form>
</div>
);
}
}

export default MadlibForm;