Shortener.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { bindActionCreators } from 'redux';
  4. import { connect } from 'react-redux';
  5. import styled from 'styled-components';
  6. import ShortenerResult from './ShortenerResult';
  7. import ShortenerTitle from './ShortenerTitle';
  8. import ShortenerInput from './ShortenerInput';
  9. import ShortenerCaptcha from './ShortenerCaptcha';
  10. import { createShortUrl, setShortenerFormError } from '../../actions';
  11. import showRecaptcha from '../../helpers/recaptcha';
  12. import { fadeIn } from '../../helpers/animations';
  13. const Wrapper = styled.div`
  14. position: relative;
  15. width: 800px;
  16. max-width: 100%;
  17. flex: 0 0 auto;
  18. display: flex;
  19. flex-direction: column;
  20. margin: 16px 0 40px;
  21. padding-bottom: 125px;
  22. animation: ${fadeIn} 0.8s ease-out;
  23. @media only screen and (max-width: 800px) {
  24. padding: 0 8px 96px;
  25. }
  26. `;
  27. const ResultWrapper = styled.div`
  28. position: relative;
  29. height: 96px;
  30. display: flex;
  31. justify-content: center;
  32. align-items: flex-start;
  33. box-sizing: border-box;
  34. @media only screen and (max-width: 448px) {
  35. height: 72px;
  36. }
  37. `;
  38. class Shortener extends Component {
  39. constructor() {
  40. super();
  41. this.state = {
  42. isCopied: false,
  43. };
  44. this.handleSubmit = this.handleSubmit.bind(this);
  45. this.copyHandler = this.copyHandler.bind(this);
  46. }
  47. componentDidMount() {
  48. showRecaptcha();
  49. }
  50. shouldComponentUpdate(nextProps, nextState) {
  51. const { isAuthenticated, shortenerError, shortenerLoading, url: { isShortened } } = this.props;
  52. return (
  53. isAuthenticated !== nextProps.isAuthenticated ||
  54. shortenerError !== nextProps.shortenerError ||
  55. isShortened !== nextProps.url.isShortened ||
  56. shortenerLoading !== nextProps.shortenerLoading ||
  57. this.state.isCopied !== nextState.isCopied
  58. );
  59. }
  60. handleSubmit(e) {
  61. const { isAuthenticated } = this.props;
  62. e.preventDefault();
  63. const shortenerForm = document.getElementById('shortenerform');
  64. const {
  65. target: originalUrl,
  66. customurl: customurlInput,
  67. password: pwd,
  68. 'g-recaptcha-input': recaptcha,
  69. } = shortenerForm.elements;
  70. const target = originalUrl.value.trim();
  71. const customurl = customurlInput && customurlInput.value.trim();
  72. const password = pwd && pwd.value;
  73. const reCaptchaToken = !isAuthenticated && recaptcha && recaptcha.value;
  74. if (!isAuthenticated && !reCaptchaToken) {
  75. window.grecaptcha.reset();
  76. return this.props.setShortenerFormError('reCAPTCHA is not valid. Try again.');
  77. }
  78. const options = isAuthenticated && { customurl, password };
  79. shortenerForm.reset();
  80. if (!isAuthenticated && recaptcha) window.grecaptcha.reset();
  81. return this.props.createShortUrl({ target, reCaptchaToken, ...options });
  82. }
  83. copyHandler() {
  84. this.setState({ isCopied: true });
  85. setTimeout(() => {
  86. this.setState({ isCopied: false });
  87. }, 1500);
  88. }
  89. render() {
  90. const { isCopied } = this.state;
  91. const { isAuthenticated, shortenerError, shortenerLoading, url } = this.props;
  92. return (
  93. <Wrapper>
  94. <ResultWrapper>
  95. {!shortenerError && (url.isShortened || shortenerLoading) ? (
  96. <ShortenerResult
  97. copyHandler={this.copyHandler}
  98. loading={shortenerLoading}
  99. url={url}
  100. isCopied={isCopied}
  101. />
  102. ) : (
  103. <ShortenerTitle />
  104. )}
  105. </ResultWrapper>
  106. <ShortenerInput
  107. isAuthenticated={isAuthenticated}
  108. handleSubmit={this.handleSubmit}
  109. setShortenerFormError={this.props.setShortenerFormError}
  110. />
  111. {!isAuthenticated && <ShortenerCaptcha />}
  112. </Wrapper>
  113. );
  114. }
  115. }
  116. Shortener.propTypes = {
  117. isAuthenticated: PropTypes.bool.isRequired,
  118. createShortUrl: PropTypes.func.isRequired,
  119. shortenerError: PropTypes.string.isRequired,
  120. shortenerLoading: PropTypes.bool.isRequired,
  121. setShortenerFormError: PropTypes.func.isRequired,
  122. url: PropTypes.shape({
  123. isShortened: PropTypes.bool.isRequired,
  124. }).isRequired,
  125. };
  126. const mapStateToProps = ({
  127. auth: { isAuthenticated },
  128. error: { shortener: shortenerError },
  129. loading: { shortener: shortenerLoading },
  130. url,
  131. }) => ({
  132. isAuthenticated,
  133. shortenerError,
  134. shortenerLoading,
  135. url,
  136. });
  137. const mapDispatchToProps = dispatch => ({
  138. createShortUrl: bindActionCreators(createShortUrl, dispatch),
  139. setShortenerFormError: bindActionCreators(setShortenerFormError, dispatch),
  140. });
  141. export default connect(mapStateToProps, mapDispatchToProps)(Shortener);