Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. // Dis is salad laborazion
  2.  
  3. // salladklassen
  4.  
  5. import inventory from "./inventory.ES6.js";
  6. import React from "react";
  7.  
  8. class Salad extends React.Component {
  9. constructor(props){
  10. super(props);
  11.  
  12.  
  13. this.foundation = undefined;
  14. this.extras = [];
  15. this.proteins = [];
  16. this.dressings = undefined;
  17. }
  18.  
  19. add(ingredient) {
  20. console.log("i theSalad, ingrediens-string: " + ingredient + " tomt?");
  21. if(inventory[ingredient].foundation === true){
  22. this.foundation = inventory[ingredient];
  23. }
  24. else if( [ingredient].extra === true){
  25. this.extras.push(inventory[ingredient]);
  26. }
  27. else if(inventory[ingredient].protein === true){
  28. this.proteins.push(inventory[ingredient]);
  29. }
  30. else if(inventory[ingredient].dressing === true){
  31. this.dressings = inventory[ingredient];
  32. }
  33. }
  34. // Använd Splice för att ta bort element i vektor. ex. this.extras.indexOf(...)
  35.  
  36. remove(ingredient){
  37. if(inventory[ingredient].foundation === true){
  38. this.foundation = undefined;
  39.  
  40. }
  41. else if(inventory[ingredient].extra === true){
  42. this.extras.splice(this.extras.indexOf(ingredient), 1);
  43.  
  44. }
  45.  
  46. else if(inventory[ingredient].protein === true){
  47. this.proteins.splice(this.proteins.indexOf(ingredient), 1);
  48. }
  49.  
  50. else if(inventory[ingredient].dressing === true){
  51. this.dressings = undefined;
  52. }
  53. }
  54.  
  55.  
  56. price(){
  57. let extraPrice = this.extras.reduce(function (total, num){
  58. return total + num.price;
  59. }, 0)
  60.  
  61. let proteinsPrice = this.proteins.reduce(function (total, num) {
  62. return total + num.price
  63. }, 0)
  64.  
  65.  
  66. let sum = extraPrice + proteinsPrice + this.foundation.price
  67. + this.dressings.price;
  68.  
  69. return sum;
  70.  
  71.  
  72.  
  73.  
  74. }
  75.  
  76. toString() {
  77. return ('Foundation: ' + this.foundation + ' \n Extras:' + this.extras +
  78. '\n Proteins: ' + this.proteins +
  79. '\n Dressings: ' + this.dressings);
  80. }
  81. }
  82.  
  83. export default Salad;
  84.  
  85.  
  86. //ComposeSalad
  87.  
  88. import React from 'react';
  89. import Salad from './theSalad.js';
  90. class ComposeSalad extends React.Component {
  91. constructor(props){
  92. super(props);
  93.  
  94. this.state = {
  95. Foundation: " ",
  96. Proteins: [],
  97. Extras: [],
  98. Dressing: " ",
  99. Price: 0,
  100.  
  101. };
  102.  
  103.  
  104. this.handleFoundationChange = this.handleFoundationChange.bind(this);
  105. this.handleProteinChange = this.handleProteinChange.bind(this);
  106. this.handleExtraChange = this.handleExtraChange.bind(this);
  107. this.handleDressingChange = this.handleDressingChange.bind(this);
  108. this.handleSubmit = this.handleSubmit.bind(this);
  109. }
  110.  
  111.  
  112.  
  113. handleFoundationChange(event) {
  114. let index = event.nativeEvent.target.selectedIndex;
  115. let a = event.nativeEvent.target[index].value;
  116. console.log("foundation before setState" + this.state.Foundation)
  117. this.setState({Foundation: a});
  118. console.log("foundation after setState" + this.state.Foundation);
  119. //console.log(this.state);
  120. }
  121.  
  122. handleProteinChange(event){
  123.  
  124. console.log("Start of handleProteinChange " + event.target.id)
  125. let a = this.state.Proteins.slice();
  126. if(!this.state.Proteins.includes(event.target.id)){
  127. a.push(event.target.name);
  128. console.log("array a after pushing protein: " + a);
  129. this.setState({Proteins : a});
  130. console.log("this.Proteins after setting this.setState(a)" + this.state.Proteins);
  131.  
  132. } else {
  133. a.splice(a.indexOf(event.target.id), 1);
  134. this.setState({Proteins: a});
  135.  
  136.  
  137. }
  138. console.log("Protein state after state has been changed in protein change method" + this.state.Proteins[0]);
  139.  
  140.  
  141.  
  142.  
  143.  
  144. }
  145.  
  146. handleExtraChange(event){
  147.  
  148. let a = this.state.Extras.slice();
  149. if(!this.state.Extras.includes(event.target.id)){
  150. a.push(event.target.name);
  151. this.setState({Extras: a});
  152.  
  153. } else {
  154.  
  155. a.splice(a.indexOf(event.target.id), 1);
  156. this.setState({Extras: a});
  157. }
  158.  
  159.  
  160.  
  161.  
  162. }
  163.  
  164. handleDressingChange(event){
  165. let index = event.nativeEvent.target.selectedIndex;
  166. let a = event.nativeEvent.target[index].value;
  167.  
  168. this.setState({Dressing: a});
  169. //console.log(this.state);
  170. }
  171.  
  172. handleSubmit(event) {
  173. event.preventDefault();
  174. console.log("handlesubmit: " + this.state.Foundation + this.state.Proteins + this.state.Extras + this.state.Dressing);
  175. alert('A name was submitted: ' + this.state.value);
  176.  
  177. let s = new Salad();
  178.  
  179. console.log("Current foundation, from state: " + this.state.Foundation);
  180.  
  181. s.add(this.state.Foundation);
  182.  
  183. console.log("Current Dressing, from state: " + this.state.Dressing);
  184.  
  185. s.add(this.state.Dressing);
  186.  
  187. for(let i of this.state.Proteins){
  188. s.add(i);
  189. }
  190. for(let i of this.state.Extras){
  191. s.add(i);
  192. }
  193. // s.add(this.state.Proteins[0]);
  194. // console.log("extra-state before add: " +this.state.Extras[0]);
  195. // s.add(this.state.Extras[0]);
  196.  
  197. console.log(s.toString());
  198.  
  199.  
  200.  
  201.  
  202. //this.props.handleNewSalad(s);
  203.  
  204. }
  205.  
  206.  
  207.  
  208.  
  209. render() {
  210. console.log("tjenna");
  211. const inventory = this.props.inventory;
  212. // test for correct ussage, the parent must send this datastructure
  213. if (!inventory)
  214. alert("inventory is undefined in ComposeSalad");
  215.  
  216. let foundations = Object.keys(inventory).filter(
  217. name => inventory[name].foundation
  218. );
  219. let proteins = Object.keys(inventory).filter(
  220. name => inventory[name].protein
  221. );
  222. let extras = Object.keys(inventory).filter(
  223. name => inventory[name].extra
  224. );
  225. let dressings = Object.keys(inventory).filter(
  226. name => inventory[name].dressing
  227. );
  228.  
  229. return (
  230.  
  231. <form onSubmit={this.handleSubmit}>
  232. <div className="container">
  233. <h4>Välj bas</h4>
  234. <ul>
  235. <select name='foundation' onChange={e => this.handleFoundationChange(e)}>
  236. <option value="" selected disabled hidden>Välj här</option>
  237. {foundations.map(name => <option value={name}>{name} {inventory[name].price} kr </option>)}
  238.  
  239.  
  240. </select>
  241. </ul>
  242.  
  243. <h4>Välj Protein</h4>
  244. <ul>
  245. <div className="form-check" id="proteins">
  246. {proteins.map(name =>
  247. <ul>
  248. <input type="checkbox" className="form-check-input" id={name} name={name} onChange={e => this.handleProteinChange(e)}/>
  249. <label className="form-check-label" htmlFor={name}>{name} (+{inventory[name].price}kr)</label>
  250. </ul>
  251. )}
  252. </div>
  253. </ul>
  254.  
  255. <h4>Välj Extras</h4>
  256. <ul>
  257. <div className="form-check" id="extras">
  258. {extras.map(name =>
  259. <ul>
  260. <input type="checkbox" className="form-check-input" id={name} name={name} onChange={e => this.handleExtraChange(e)}/>
  261. <label className="form-check-label" htmlFor={name}>{name} (+{inventory[name].price}kr)</label>
  262. </ul>
  263. )}
  264. </div>
  265.  
  266. </ul>
  267.  
  268. <h4>Välj Dressing</h4>
  269. <ul>
  270. <select name='Dressing' onChange={e => this.handleDressingChange(e)}>
  271. <option value="" selected disabled hidden>Välj här</option>
  272. {dressings.map(name => <option value={name}>{name} {inventory[name].price} kr </option>)}
  273.  
  274.  
  275. </select>
  276. </ul>
  277.  
  278. </div>
  279. <button onSubmit={e => this.handleSubmit(e)}>Skapa Sallad
  280.  
  281. </button>
  282. </form>
  283.  
  284. );
  285. }
  286. }
  287.  
  288. export default ComposeSalad;
  289.  
  290.  
  291. //App
  292.  
  293. import React, { Component } from 'react';
  294. import logo from './logo.svg';
  295. import './App.css';
  296. import inventory from './inventory.ES6.js';
  297. import ComposeSalad from './ComposeSalad';
  298. //import ComposeSaladModal from './ComposeSaladModal.js';
  299. class App extends Component {
  300. constructor(...args) {
  301. super(...args);
  302. this.state = {
  303. data: []
  304. };
  305. this.handleNewSalad = this.handleNewSalad(this);
  306. }
  307.  
  308. handleNewSalad(s){
  309. console.log(this.state.data[0]);
  310. }
  311.  
  312.  
  313.  
  314. render() {
  315. return (
  316. <div>
  317. <div className="jumbotron text-center">
  318. <h1>My Own Salad Bar</h1>
  319. <p>Here you can order custom made salads!</p>
  320. </div>
  321. <div>
  322. <ComposeSalad handleNewSalad={this.handleNewSalad}
  323. inventory={inventory} />;
  324. </div>
  325.  
  326. </div>
  327.  
  328. );
  329. }
  330. }
  331.  
  332. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement