Shortener.js 4.6 KB

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