Guest User

Untitled

a guest
Apr 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Link, Redirect } from "react-router-dom";
  3. import SweetAlert from "sweetalert2-react";
  4. import { connect } from "react-redux";
  5. import { Card, Form, Button, Divider } from "semantic-ui-react";
  6.  
  7. import "../../../../css/style.css";
  8. import HomePageHeader from "../Headers/HomePageHeader";
  9. import MainFooter from "../Footers/MainFooter";
  10.  
  11. import { verifyMobileOTP, fetchUserInfo } from "../../actions/index";
  12.  
  13. class MobVerificationOTP extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. OTP: "",
  18. isValidOTP: false,
  19. };
  20.  
  21. this.updateOTP = this.updateOTP.bind(this);
  22. this.verifyOTP = this.verifyOTP.bind(this);
  23. }
  24.  
  25. componentDidMount() {
  26. this.props.fetchUserInfo(this.props.location.state.mobileNumber);
  27. }
  28.  
  29. updateOTP(e) {
  30. this.setState({
  31. OTP: e.target.value
  32. });
  33. }
  34.  
  35. validate(status) {
  36. var otp_status = 0;
  37. if (status == 1 ) {
  38. otp_status = 1;
  39. this.setState({
  40. isValidOTP: false
  41. });
  42. } else {
  43. otp_status = 0;
  44. this.setState({
  45. isValidOTP: true,
  46. OTP: ""
  47. });
  48. }
  49. return otp_status;
  50. }
  51.  
  52. verifyOTP(OTP, mobile_number) {
  53. this.props.verifyMobileOTP(OTP, mobile_number);
  54. }
  55.  
  56. onSubmit(status) {
  57. var otpValidation = this.validate(status);
  58. console.log(otpValidation);
  59. if (otpValidation) {
  60. // this.setState({ redirect: true });
  61. console.log("success");
  62. }
  63. }
  64.  
  65. render() {
  66. const { OTP, isValidOTP } = this.state;
  67. if (this.state.redirect) {
  68. return (
  69. <Redirect
  70. push
  71. to={{
  72. pathname: "/user-info",
  73. state: { mobileNumber: this.props.location.state.mobileNumber }
  74. }}
  75. />
  76. );
  77. }
  78. return (
  79. <div style={{ backgroundColor: "#6a5f9b" }}>
  80. <HomePageHeader />
  81. <Card centered style={{ marginTop: "7%", textAlign: "center" }}>
  82. <Card.Content>
  83. <Form onSubmit={e => this.onSubmit(this.props.status)}>
  84. <Form.Field>
  85. <label>Your Mobile</label>
  86. <input
  87. disabled
  88. value={this.props.location.state.mobileNumber}
  89. />
  90. </Form.Field>
  91.  
  92. <Form.Field>
  93. <label>OTP</label>
  94. <input
  95. type="number"
  96. placeholder="Enter OTP"
  97. value={OTP}
  98. onChange={this.updateOTP}
  99. />
  100. {isValidOTP && (
  101. <SweetAlert
  102. type="error"
  103. show={isValidOTP}
  104. title="Invalid OTP"
  105. text="Please enter a valid OTP"
  106. onConfirm={() =>
  107. this.setState({
  108. show: false,
  109. isValidOTP: false
  110. })
  111. }
  112. />
  113. )}
  114. </Form.Field>
  115.  
  116. <Button
  117. fluid
  118. id="green-button"
  119. type="submit"
  120. style={{ marginTop: "10%" }}
  121. onClick={e =>
  122. this.verifyOTP(OTP, this.props.location.state.mobileNumber)
  123. }
  124. >
  125. SEND
  126. </Button>
  127.  
  128. <Divider horizontal>OR</Divider>
  129. <Link to="/mobile-verification">
  130. <Button fluid type="submit">
  131. BACK
  132. </Button>
  133. </Link>
  134. </Form>
  135. </Card.Content>
  136. </Card>
  137. <MainFooter />
  138. </div>
  139. );
  140. }
  141. }
  142.  
  143. function mapStateToProps(state) {
  144. if (state.verifyOTPReducer.length > 0 && state.userInfoReducer.length > 0) {
  145. let counter = state.verifyOTPReducer.length;
  146. if (state.verifyOTPReducer[counter - 1].hasOwnProperty("message")) {
  147. return {
  148. status: 1,
  149. user_info: state.userInfoReducer[0]
  150. };
  151. } else {
  152. return {
  153. status: 0,
  154. user_info: state.userInfoReducer[0]
  155. };
  156. }
  157. } else {
  158. return {
  159. otp_message: [],
  160. user_info: []
  161. };
  162. }
  163. }
  164.  
  165. function mapDispatchToProps(dispatch) {
  166. return {
  167. verifyMobileOTP(OTP, mobile_number) {
  168. dispatch(verifyMobileOTP(OTP, mobile_number))
  169. },
  170. fetchUserInfo(mobileNumber) {
  171. dispatch(fetchUserInfo(mobileNumber))
  172. }
  173. }
  174.  
  175. export default connect(mapStateToProps, mapDispatchToProps)(
  176. MobVerificationOTP
  177. );
Add Comment
Please, Sign In to add comment