Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import * as React from "react";
  4. import * as $ from "jquery";
  5. import Axios from "axios";
  6. import { createStore } from 'redux';
  7.  
  8. import {Redirect} from "react-router";
  9.  
  10. var initialState = "";
  11.  
  12. function reducerID(state = initialState, action) {
  13. switch (action.type) {
  14. case 'ADD':
  15. return action.id;
  16.  
  17. default:
  18. return state
  19. }
  20. }
  21.  
  22. var store = createStore(reducerID);
  23.  
  24. console.log("Aktuální stav STORE: " + store.getState())
  25.  
  26. interface State
  27. {
  28. cardResult : string,
  29. redirect : boolean
  30. }
  31.  
  32. interface Props
  33. {
  34.  
  35. }
  36.  
  37. export class Home extends React.Component<Props, State> {
  38.  
  39. constructor(props : any)
  40. {
  41. super(props);
  42. this.state = {
  43. cardResult : "Proveďte validaci",
  44. redirect : false
  45. };
  46. }
  47.  
  48. checkCard = (e: any) =>
  49. {
  50. e.preventDefault();
  51.  
  52. var kartaId = $("#card_id").val();
  53.  
  54. store.dispatch({ type: 'ADD', id: kartaId });
  55. console.log("Karta ve STORE: " + store.getState());
  56.  
  57.  
  58. Axios.post('/checkcard', {
  59. kartaId: store.getState()
  60. })
  61. .then(response =>
  62. {
  63. console.log(response.data);
  64. this.setState({
  65. cardResult : response.data
  66. });
  67. if(response.data == "Karta je platná")
  68. {
  69. this.setState({
  70. redirect : true
  71. });
  72. }
  73. })
  74. .catch(error =>
  75. {
  76. this.setState({
  77. cardResult: "Chyba při zjištění platnosti! Zkuste to prosím znovu." + error
  78. });
  79. })
  80. .then(() => {
  81. $('#card_id').val("");
  82. });
  83.  
  84. };
  85.  
  86. saveRecord = (e:any) => {
  87.  
  88. console.log(store.getState());
  89. console.log(e.target.name);
  90.  
  91. Axios.post('/savecardtolocaldb', {
  92. kartaID: store.getState(),
  93. typJidla: e.target.name,
  94. })
  95. .then(response =>
  96. {
  97. console.log(response.data);
  98. })
  99. .catch(error =>
  100. {
  101. console.log("Někde se stala chyba");
  102. console.log(error);
  103. })
  104. };
  105.  
  106. render()
  107. {
  108. if(this.state.redirect)
  109. {
  110. return (
  111. <div className={"container"}>
  112. <div className={"row text-center"}>
  113. <h1>
  114. Vyberte prosím typ jídla
  115. </h1>
  116. </div>
  117. <div className={"row"}>
  118. <div className={"offset-lg-2 col-lg-3"}>
  119. <button onClick={this.saveRecord} name="teple" typeof={"button"} className={"foodBtn hotFood"}>TEPLÉ</button>
  120. </div>
  121. <div className={"offset-lg-2 col-lg-3"}>
  122. <button onClick={this.saveRecord} name="studene" typeof={"button"} className={"foodBtn coldFood"}>STUDENÉ</button>
  123. </div>
  124. </div>
  125. </div>
  126. );
  127. }
  128.  
  129. return (
  130. <div>
  131. <h1 className="text-center">Stravovací systém Ostravské univerzity</h1>
  132. <form onSubmit={this.checkCard}>
  133. <div className="row justify-content-lg-center">
  134. <div className="col-lg-8">
  135. <label className="sr-only" htmlFor="card_id">Naskenujte kartu</label>
  136. <div className="input-group mb-2 mb-sm-0">
  137. <div className="input-group-addon">
  138. <i className="fa fa-address-card-o" aria-hidden="true" />
  139. </div>
  140. <input type="text" className="form-control" id="card_id" placeholder="ID KARTY" autoFocus />
  141. </div>
  142. </div>
  143. </div>
  144. </form>
  145.  
  146. <span className="validation row justify-content-lg-center">
  147. {this.state.cardResult}
  148. </span>
  149.  
  150. </div>
  151. );
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement