Advertisement
Guest User

cyoa.js

a guest
Feb 26th, 2020
3,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. import React, { Component } from "react";
  2.  
  3. import "./styles.css";
  4.  
  5. var pages = {
  6. start: {
  7. text: "Welcome, traveler! How would you like to get to your destination?",
  8. buttons: [
  9. {
  10. label: "Train",
  11. page:"thetrainstation"
  12. },
  13. {
  14. label: "Ship",
  15. page:"theharbor"
  16. }
  17. ]
  18. },
  19. thetrainstation: {
  20. image: "img/station.jpg",
  21. text:
  22. "Welcome to the train station. Are you commuting or going on vacation?",
  23. leftLabel: "Commuting",
  24. rightLabel: "Traveling",
  25. leftPage: "caltrain",
  26. rightPage: "amtrak"
  27. },
  28. theharbor: {
  29. text:
  30. "A massive tsunami has destroyed all the ships. Looks like you'll have to take the train.",
  31. leftLabel: "Take the train.",
  32. leftPage: "thetrainstation"
  33. },
  34. caltrain: {
  35. text: "You're taking Caltrain today. Where are you going?",
  36. leftLabel: "Work",
  37. rightLabel: "School",
  38. leftPage: "atwork",
  39. rightPage: "atschool"
  40. },
  41. atwork: {
  42. text: "Wow. No, don't do this to yourself. Go have some fun!",
  43. leftLabel: "Have fun",
  44. rightLabel: "Go to school",
  45. leftPage: "amtrak",
  46. rightPage: "atschool"
  47. },
  48. atschool: {
  49. text: "What are you doing? Seriously, go have some fun. Live your life!",
  50. leftLabel: "Have fun",
  51. rightLabel: "Don't have fun",
  52. leftPage: "amtrak",
  53. rightPage: "atwork"
  54. },
  55. amtrak: {
  56. text:
  57. "Woo-woo! You're taking Amtrak to your vacation destination! Where do you wanna go?",
  58. leftLabel: "New York",
  59. middleLabel: "Oregon",
  60. rightLabel: "Florida",
  61. leftPage: "nyc",
  62. middlePage: "oregon",
  63. rightPage: "florida"
  64. },
  65. nyc: {
  66. text:
  67. "Welcome to the city that never sleeps. Whatcha wanna do here, traveler?",
  68. leftLabel: "See Hamilton.",
  69. middleLabel: "Eat pizza.",
  70. rightLabel: "Look for Spider-Man.",
  71. leftPage: "hamilton",
  72. middlePage: "pizza",
  73. rightPage: "spiderman"
  74. },
  75. hamilton: {
  76. text:
  77. "Nice choice. But Hamilton tickets are crazy expensive. How are you getting in?",
  78. leftLabel: "Pay up.",
  79. rightLabel: "Sneak in.",
  80. leftPage: "pay",
  81. rightPage: "sneak"
  82. },
  83. pay: {
  84. text: "Pleb. Enjoy the show!"
  85. },
  86. sneak: {
  87. text: "Sneaky. Just kidding, you got caught. Enjoy prison!"
  88. },
  89. pizza: {
  90. text: "PIZZA TIME!"
  91. },
  92. spiderman: {
  93. text: "LMAO Spider-Man isn't real."
  94. },
  95. oregon: {
  96. text: "Welcome to Oregon. There's lot's to do here. Do you wanna...",
  97. leftLabel: "Visit Crater Lake?",
  98. rightLabel: "Go to Portland?",
  99. leftPage: "craterlake",
  100. rightPage: "portland"
  101. },
  102. craterlake: {
  103. text: "Beautiful, isn't it? Good thing you caught it on a cloudless day!"
  104. },
  105. portland: {
  106. text: "Man, this place is WEIRD. "
  107. },
  108. florida: {
  109. text: "Welcome to the Sunshine State! What do you wanna do for fun?",
  110. leftLabel: "Go on a cruise!",
  111. rightLabel: "Go to Disney World!",
  112. leftPage: "cruise",
  113. rightPage: "disney"
  114. },
  115. cruise: {
  116. text:
  117. "Cruises are bad for the environment, not to mention that all the ships have been knocked out by another tsunami."
  118. },
  119. disney: {
  120. text:
  121. "'The happiest place on Earth' is plagued by lines. Wanna go somewhere else?",
  122. leftLabel: "Yes",
  123. rightLabel: "No",
  124. leftPage: "amtrak",
  125. rightPage: "lines"
  126. },
  127. lines: {
  128. text:
  129. "Well, you know what they say. If you can't beat 'em, join 'em. Have fun being a loser waiting like everybody else!"
  130. }
  131. };
  132.  
  133. class App extends Component {
  134. constructor(props) {
  135. super(props);
  136.  
  137. this.state = {
  138. page: "start"
  139. };
  140. }
  141.  
  142. goToPage(pageName) {
  143. this.setState({
  144. page: pageName
  145. });
  146. }
  147.  
  148. render() {
  149. var pageData = pages[this.state.page];
  150.  
  151. var imageElement = "";
  152. if (pageData.image) {
  153. imageElement = <img src={pageData.image} width="200" />;
  154. }
  155. // buttons: [
  156. // {
  157. // label: "Train",
  158. // page:"thetrainstation"
  159. // },
  160. // {
  161. // label: "Ship",
  162. // page:"theharbor"
  163. // }
  164. // ]
  165. // var buttons = [];
  166. // for (let i = 0; i < pageData.buttons.length; i = i + 1) {
  167. // let buttonInfo = pageData.buttons[i];
  168. // let buttonElement = <button onClick={() => this.goToPage(buttonInfo.page)}>
  169. // {buttonInfo.label}
  170. // </button>
  171. // buttons.push(buttonElement);
  172. // }
  173.  
  174. return (
  175. <div className="App">
  176. <p>
  177. {pageData.image}
  178. <br />
  179. {imageElement}
  180. </p>
  181. <p>{pageData.text}</p>
  182. {pageData.buttons.map((buttonInfo) =>
  183. <button onClick={() => this.goToPage(buttonInfo.page)}>
  184. {buttonInfo.label}
  185. </button>
  186. )}
  187. </div>
  188. );
  189. }
  190. }
  191.  
  192. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement