Shortener.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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: 98%;
  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 {
  47. isAuthenticated,
  48. domain,
  49. shortenerError,
  50. shortenerLoading,
  51. url: { isShortened },
  52. } = this.props;
  53. return (
  54. isAuthenticated !== nextProps.isAuthenticated ||
  55. shortenerError !== nextProps.shortenerError ||
  56. isShortened !== nextProps.url.isShortened ||
  57. shortenerLoading !== nextProps.shortenerLoading ||
  58. domain !== nextProps.domain ||
  59. this.state.isCopied !== nextState.isCopied
  60. );
  61. }
  62. handleSubmit(e) {
  63. e.preventDefault();
  64. const { isAuthenticated } = this.props;
  65. this.props.showShortenerLoading();
  66. const shortenerForm = document.getElementById('shortenerform');
  67. const {
  68. target: originalUrl,
  69. customurl: customurlInput,
  70. password: pwd,
  71. } = shortenerForm.elements;
  72. const target = originalUrl.value.trim();
  73. const customurl = customurlInput && customurlInput.value.trim();
  74. const password = pwd && pwd.value;
  75. const options = isAuthenticated && { customurl, password };
  76. shortenerForm.reset();
  77. if (!isAuthenticated) {
  78. window.grecaptcha.execute(window.captchaId);
  79. const getCaptchaToken = () => {
  80. setTimeout(() => {
  81. if (window.isCaptchaReady) {
  82. const reCaptchaToken = window.grecaptcha.getResponse(window.captchaId);
  83. window.isCaptchaReady = false;
  84. window.grecaptcha.reset(window.captchaId);
  85. return this.props.createShortUrl({ target, reCaptchaToken, ...options });
  86. }
  87. return getCaptchaToken();
  88. }, 200);
  89. };
  90. return getCaptchaToken();
  91. }
  92. return this.props.createShortUrl({ target, ...options });
  93. }
  94. copyHandler() {
  95. this.setState({ isCopied: true });
  96. setTimeout(() => {
  97. this.setState({ isCopied: false });
  98. }, 1500);
  99. }
  100. render() {
  101. const { isCopied } = this.state;
  102. const { isAuthenticated, shortenerError, shortenerLoading, url } = this.props;
  103. return (
  104. <Wrapper>
  105. <ResultWrapper>
  106. {!shortenerError && (url.isShortened || shortenerLoading) ? (
  107. <ShortenerResult
  108. copyHandler={this.copyHandler}
  109. loading={shortenerLoading}
  110. url={url}
  111. isCopied={isCopied}
  112. />
  113. ) : (
  114. <ShortenerTitle />
  115. )}
  116. </ResultWrapper>
  117. <ShortenerInput
  118. isAuthenticated={isAuthenticated}
  119. handleSubmit={this.handleSubmit}
  120. setShortenerFormError={this.props.setShortenerFormError}
  121. domain={this.props.domain}
  122. />
  123. </Wrapper>
  124. );
  125. }
  126. }
  127. Shortener.propTypes = {
  128. isAuthenticated: PropTypes.bool.isRequired,
  129. domain: PropTypes.string.isRequired,
  130. createShortUrl: PropTypes.func.isRequired,
  131. shortenerError: PropTypes.string.isRequired,
  132. shortenerLoading: PropTypes.bool.isRequired,
  133. setShortenerFormError: PropTypes.func.isRequired,
  134. showShortenerLoading: PropTypes.func.isRequired,
  135. url: PropTypes.shape({
  136. isShortened: PropTypes.bool.isRequired,
  137. }).isRequired,
  138. };
  139. const mapStateToProps = ({
  140. auth: { isAuthenticated },
  141. error: { shortener: shortenerError },
  142. loading: { shortener: shortenerLoading },
  143. settings: { customDomain: domain },
  144. url,
  145. }) => ({
  146. isAuthenticated,
  147. domain,
  148. shortenerError,
  149. shortenerLoading,
  150. url,
  151. });
  152. const mapDispatchToProps = dispatch => ({
  153. createShortUrl: bindActionCreators(createShortUrl, dispatch),
  154. setShortenerFormError: bindActionCreators(setShortenerFormError, dispatch),
  155. showShortenerLoading: bindActionCreators(showShortenerLoading, dispatch),
  156. });
  157. export default connect(mapStateToProps, mapDispatchToProps)(Shortener);