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

Brenda Rios - Octos - Madlib #24

Open
wants to merge 6 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"
}
}
}
8 changes: 8 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ body {
color: white;
font: normal 18px/1.5 "Fira Sans", "Helvetica Neue", sans-serif;
}

.false {
display: none;
}

.true {
display:inline;
}
23 changes: 15 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import React, { Component } from 'react';
import './App.css';
import MadLibs from './madlibs/MadLibs.js';
import Story from './components/Story.js';
import StoryForm from './components/StoryForm.js';

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

this.state = {
selectedMadLib: MadLibs[0]
selectedMadLib: MadLibs[Math.floor(Math.random() * MadLibs.length-1)],
displayStory: false
};

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

// Update the value of a word in the selected
// mad lib using setState
updateWord(key, value) {
const updatedMadLib = this.state.selectedMadLib;
const changedWord = updatedMadLib.words.find((word) => {
Expand All @@ -23,18 +25,23 @@ class App extends Component {
this.setState({selectedMadLib: updatedMadLib});
}


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
*/}
<p>Fill in all of the choices to see your final story.</p>
<StoryForm
updateWord={this.updateWord}
words = {this.state.selectedMadLib.words}
/>

<Story
title={ this.state.selectedMadLib.title }
text={ this.state.selectedMadLib.getText() }
/>
/>

</section>
);
}
Expand Down
52 changes: 52 additions & 0 deletions src/components/StoryForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';


class StoryForm extends Component {

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

};

onFieldChange = (key, value) => {
this.props.updateWord(key, value);
};

onSubmit = (event) => {
event.preventDefault();
console.log("Form submission");
this.setState({
displayStory: true
})
};

render() {
let words = this.props.words;
let inputs = words.map((word) => {
return (
<section key={word.key}
className="label-input">
<label>{word.label}</label>
<input
name={word.key}
onChange={(event) => { this.onFieldChange(word.key, event.target.value) }}
/>
</section>
);
});
return (
<div className={'true'}>
<form className="story-form"
onSubmit = {this.onSubmit}
>
{inputs}
<input type ="submit" className="button"/>
</form>
</div>
);
}
}

export default StoryForm;