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

Aruna(ampers) #34

Open
wants to merge 1 commit 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"
}
}
}
34 changes: 27 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import React, { Component } from 'react';
import './App.css';
import MadLibs from './madlibs/MadLibs.js';
import Story from './components/Story.js';
import NewStoryForm from './components/NewStoryForm.js';

class App extends Component {
constructor() {
super();

this.state = {
selectedMadLib: MadLibs[0]
selectedMadLib: MadLibs[0],
isStoryVisible: false,
};

this.updateWord = this.updateWord.bind(this);
}

// Update the value of a word in the selected
Expand All @@ -23,18 +27,34 @@ class App extends Component {
this.setState({selectedMadLib: updatedMadLib});
}

showStory = () => {
if (this.state.isStoryVisible) {
return(
<Story
title={ this.state.selectedMadLib.title }
text={ this.state.selectedMadLib.getText() }
/>
);
}
}

changeStoryVisibility = () => {
this.setState({
isStoryVisible: true,
});
}

render() {
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() }
<NewStoryForm
updateWord={this.updateWord}
words={this.state.selectedMadLib.words}
changeStoryVisibility={this.changeStoryVisibility}
/>
{this.showStory()}
</section>
);
}
Expand Down
47 changes: 47 additions & 0 deletions src/components/NewStoryForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
// import './NewStoryForm.css';

class NewStoryForm extends Component {

static propTypes = {
updateWord: PropTypes.func.isRequired,
words: PropTypes.array.isRequired,
changeStoryVisibility:PropTypes.func.isRequired,
}

onFieldChange = (key, value) => {
this.props.updateWord(key,value)
}
onSubmit=(event) => {
event.preventDefault();
this.props.changeStoryVisibility();
};

render() {
let words = this.props.words;
let inputs = words.map((word) => {
return(
<section key={word.key}>
<label>{word.label}</label>
<input
name={word.key}
onChange={(event) => { this.onFieldChange(word.key, event.target.value) }}
/>
</section>
);
});

return (
<section>
<form className="new-story-form">
onSubmit={this.onSubmit}
{inputs}
<input type="submit" />
</form>
</section>
);
}
}

export default NewStoryForm;