Shortener.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 } 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. const { isAuthenticated } = this.props;
  57. e.preventDefault();
  58. const shortenerForm = document.getElementById('shortenerform');
  59. const {
  60. target: originalUrl,
  61. customurl: customurlInput,
  62. password: pwd,
  63. } = shortenerForm.elements;
  64. const target = originalUrl.value.trim();
  65. const customurl = customurlInput && customurlInput.value.trim();
  66. const password = pwd && pwd.value;
  67. const options = isAuthenticated && { customurl, password };
  68. shortenerForm.reset();
  69. return this.props.createShortUrl({ target, ...options });
  70. }
  71. copyHandler() {
  72. this.setState({ isCopied: true });
  73. setTimeout(() => {
  74. this.setState({ isCopied: false });
  75. }, 1500);
  76. }
  77. render() {
  78. const { isCopied } = this.state;
  79. const { isAuthenticated, shortenerError, shortenerLoading, url } = this.props;
  80. return (
  81. <Wrapper>
  82. <ResultWrapper>
  83. {!shortenerError && (url.isShortened || shortenerLoading) ? (
  84. <ShortenerResult
  85. copyHandler={this.copyHandler}
  86. loading={shortenerLoading}
  87. url={url}
  88. isCopied={isCopied}
  89. />
  90. ) : (
  91. <ShortenerTitle />
  92. )}
  93. </ResultWrapper>
  94. <ShortenerInput
  95. isAuthenticated={isAuthenticated}
  96. handleSubmit={this.handleSubmit}
  97. setShortenerFormError={this.props.setShortenerFormError}
  98. />
  99. </Wrapper>
  100. );
  101. }
  102. }
  103. Shortener.propTypes = {
  104. isAuthenticated: PropTypes.bool.isRequired,
  105. createShortUrl: PropTypes.func.isRequired,
  106. shortenerError: PropTypes.string.isRequired,
  107. shortenerLoading: PropTypes.bool.isRequired,
  108. setShortenerFormError: PropTypes.func.isRequired,
  109. url: PropTypes.shape({
  110. isShortened: PropTypes.bool.isRequired,
  111. }).isRequired,
  112. };
  113. const mapStateToProps = ({
  114. auth: { isAuthenticated },
  115. error: { shortener: shortenerError },
  116. loading: { shortener: shortenerLoading },
  117. url,
  118. }) => ({
  119. isAuthenticated,
  120. shortenerError,
  121. shortenerLoading,
  122. url,
  123. });
  124. const mapDispatchToProps = dispatch => ({
  125. createShortUrl: bindActionCreators(createShortUrl, dispatch),
  126. setShortenerFormError: bindActionCreators(setShortenerFormError, dispatch),
  127. });
  128. export default connect(mapStateToProps, mapDispatchToProps)(Shortener);