Guest User

Untitled

a guest
Dec 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import axios from 'axios'
  3. import './Love.css'
  4. import Match from './Matches/Match'
  5. import MatchWrapper from './HOC/MatchWrapper';
  6.  
  7. class Love extends Component{
  8.  
  9. state= {
  10. inputName: '',
  11. inputSname: '',
  12. percentage: 0,
  13. result: '',
  14. showResult: false
  15. }
  16.  
  17. componentDidMount(){
  18. console.log('hello')
  19. }
  20.  
  21. findMatchHandler = () =>{
  22. axios.get(`https://love-calculator.p.mashape.com/getPercentage?fname=${this.state.inputName}&sname=${this.state.inputSname}`,{
  23. headers: {
  24. "X-Mashape-Key": "cZA91FBSWlmshegV4IsGJIcGoc3yp1Eq9cCjsnjMGOVB35Z1Ud",
  25. "Accept": "application/json"
  26. }
  27. }).then(res =>
  28. this.setState({
  29. name: res.data.fname,
  30. sName: res.data.sname,
  31. percentage: res.data.percentage,
  32. result: res.data.result,
  33. showResult: true
  34. })
  35. )
  36. }
  37.  
  38. render(){
  39.  
  40. console.log(this.state.percentage)
  41. console.log(this.state.showResult)
  42. return(
  43.  
  44. <div className={"main-div " + (this.state.percentage > 75 && this.state.showResult ? "match " : ' ') + (this.state.percentage > 50 && this.state.percentage < 75 && this.state.showResult === true ? 'semi ' : ' ') + (this.state.percentage < 50 && this.state.showResult ? 'no-match': '')}>
  45.  
  46. <button onClick={this.findMatchHandler}>Find love!</button>
  47. <input type="text" value={this.state.inputName} onChange={(event) => this.setState({inputName: event.target.value, showResult: false})} placeholder="enter your name"/>
  48. <input type="text" value={this.state.inputSname} onChange={(event) => this.setState({inputSname: event.target.value, showResult: false})} placeholder="enter your name"/>
  49.  
  50.  
  51.  
  52. <Match
  53. inputName={this.state.inputName}
  54. inputSname={this.state.inputSname}
  55. percentage={this.state.percentage}
  56. result={this.state.result}
  57. show={this.state.showResult}
  58. />
  59. </div>
  60. )
  61. }
  62. }
  63. export default Love
  64.  
  65. import React from "react";
  66.  
  67. const MatchWrapper = WrappedComponent => (props) => {
  68. const {show, percentage } = props;
  69. console.log(props.percentage)
  70. let type = "";
  71. switch (true) {
  72. case percentage > 75:
  73. type = "success";
  74. break;
  75. case percentage >= 50 && 75 >= percentage:
  76. type = "mediocre";
  77. break;
  78. case percentage <= 50:
  79. type = "failure";
  80. break;
  81. }
  82.  
  83. return show && <WrappedComponent {...props} type={type} />
  84. }
  85.  
  86. export default MatchWrapper;
  87.  
  88. import React, { Component } from "react";
  89.  
  90. const match = (props, type) => {
  91. console.log(type)
  92. let display = ""
  93. if(type="success"){
  94. display = <Success {...props} />
  95. } else if(type="mediocre"){
  96. display=<Mediocre {...props}/>
  97. }else{
  98. display=<Failure {...props} />
  99. }
  100. return (
  101. <div>
  102. {props.show &&
  103. <div className="match">
  104. {display}
  105. {display}
  106. </div>
  107. }
  108. </div>
  109. );
  110. };
  111. export default MatchWrapper(match);
Add Comment
Please, Sign In to add comment