Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import * as React from "react";
- import * as $ from "jquery";
- import Axios from "axios";
- import { createStore } from 'redux';
- import {Redirect} from "react-router";
- var initialState = "";
- function reducerID(state = initialState, action) {
- switch (action.type) {
- case 'ADD':
- return action.id;
- default:
- return state
- }
- }
- var store = createStore(reducerID);
- console.log("Aktuální stav STORE: " + store.getState())
- interface State
- {
- cardResult : string,
- redirect : boolean
- }
- interface Props
- {
- }
- export class Home extends React.Component<Props, State> {
- constructor(props : any)
- {
- super(props);
- this.state = {
- cardResult : "Proveďte validaci",
- redirect : false
- };
- }
- checkCard = (e: any) =>
- {
- e.preventDefault();
- var kartaId = $("#card_id").val();
- store.dispatch({ type: 'ADD', id: kartaId });
- console.log("Karta ve STORE: " + store.getState());
- Axios.post('/checkcard', {
- kartaId: store.getState()
- })
- .then(response =>
- {
- console.log(response.data);
- this.setState({
- cardResult : response.data
- });
- if(response.data == "Karta je platná")
- {
- this.setState({
- redirect : true
- });
- }
- })
- .catch(error =>
- {
- this.setState({
- cardResult: "Chyba při zjištění platnosti! Zkuste to prosím znovu." + error
- });
- })
- .then(() => {
- $('#card_id').val("");
- });
- };
- saveRecord = (e:any) => {
- console.log(store.getState());
- console.log(e.target.name);
- Axios.post('/savecardtolocaldb', {
- kartaID: store.getState(),
- typJidla: e.target.name,
- })
- .then(response =>
- {
- console.log(response.data);
- })
- .catch(error =>
- {
- console.log("Někde se stala chyba");
- console.log(error);
- })
- };
- render()
- {
- if(this.state.redirect)
- {
- return (
- <div className={"container"}>
- <div className={"row text-center"}>
- <h1>
- Vyberte prosím typ jídla
- </h1>
- </div>
- <div className={"row"}>
- <div className={"offset-lg-2 col-lg-3"}>
- <button onClick={this.saveRecord} name="teple" typeof={"button"} className={"foodBtn hotFood"}>TEPLÉ</button>
- </div>
- <div className={"offset-lg-2 col-lg-3"}>
- <button onClick={this.saveRecord} name="studene" typeof={"button"} className={"foodBtn coldFood"}>STUDENÉ</button>
- </div>
- </div>
- </div>
- );
- }
- return (
- <div>
- <h1 className="text-center">Stravovací systém Ostravské univerzity</h1>
- <form onSubmit={this.checkCard}>
- <div className="row justify-content-lg-center">
- <div className="col-lg-8">
- <label className="sr-only" htmlFor="card_id">Naskenujte kartu</label>
- <div className="input-group mb-2 mb-sm-0">
- <div className="input-group-addon">
- <i className="fa fa-address-card-o" aria-hidden="true" />
- </div>
- <input type="text" className="form-control" id="card_id" placeholder="ID KARTY" autoFocus />
- </div>
- </div>
- </div>
- </form>
- <span className="validation row justify-content-lg-center">
- {this.state.cardResult}
- </span>
- </div>
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement