forked from AdaGold/madlib
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathApp.js
66 lines (56 loc) · 1.49 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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[getRandomInt(MadLibs.length)],
showStory: false,
};
}
// 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) => {
return word.key === key
});
changedWord.value = value;
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>
<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;