| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import React, { Component } from 'react';
- import PropTypes from 'prop-types';
- import { bindActionCreators } from 'redux';
- import { connect } from 'react-redux';
- import styled from 'styled-components';
- import ShortenerResult from './ShortenerResult';
- import ShortenerTitle from './ShortenerTitle';
- import ShortenerInput from './ShortenerInput';
- import { createShortUrl, setShortenerFormError, showShortenerLoading } from '../../actions';
- import { fadeIn } from '../../helpers/animations';
- const Wrapper = styled.div`
- position: relative;
- width: 800px;
- max-width: 98%;
- flex: 0 0 auto;
- display: flex;
- flex-direction: column;
- margin: 16px 0 40px;
- padding-bottom: 125px;
- animation: ${fadeIn} 0.8s ease-out;
- @media only screen and (max-width: 800px) {
- padding: 0 8px 96px;
- }
- `;
- const ResultWrapper = styled.div`
- position: relative;
- height: 96px;
- display: flex;
- justify-content: center;
- align-items: flex-start;
- box-sizing: border-box;
- @media only screen and (max-width: 448px) {
- height: 72px;
- }
- `;
- class Shortener extends Component {
- constructor() {
- super();
- this.state = {
- isCopied: false,
- };
- this.handleSubmit = this.handleSubmit.bind(this);
- this.copyHandler = this.copyHandler.bind(this);
- }
- shouldComponentUpdate(nextProps, nextState) {
- const {
- isAuthenticated,
- domain,
- shortenerError,
- shortenerLoading,
- url: { isShortened },
- } = this.props;
- return (
- isAuthenticated !== nextProps.isAuthenticated ||
- shortenerError !== nextProps.shortenerError ||
- isShortened !== nextProps.url.isShortened ||
- shortenerLoading !== nextProps.shortenerLoading ||
- domain !== nextProps.domain ||
- this.state.isCopied !== nextState.isCopied
- );
- }
- handleSubmit(e) {
- e.preventDefault();
- const { isAuthenticated } = this.props;
- this.props.showShortenerLoading();
- const shortenerForm = document.getElementById('shortenerform');
- const {
- target: originalUrl,
- customurl: customurlInput,
- password: pwd,
- } = shortenerForm.elements;
- const target = originalUrl.value.trim();
- const customurl = customurlInput && customurlInput.value.trim();
- const password = pwd && pwd.value;
- const options = isAuthenticated && { customurl, password };
- shortenerForm.reset();
- if (!isAuthenticated) {
- window.grecaptcha.execute(window.captchaId);
- const getCaptchaToken = () => {
- setTimeout(() => {
- if (window.isCaptchaReady) {
- const reCaptchaToken = window.grecaptcha.getResponse(window.captchaId);
- window.isCaptchaReady = false;
- window.grecaptcha.reset(window.captchaId);
- return this.props.createShortUrl({ target, reCaptchaToken, ...options });
- }
- return getCaptchaToken();
- }, 200);
- };
- return getCaptchaToken();
- }
- return this.props.createShortUrl({ target, ...options });
- }
- copyHandler() {
- this.setState({ isCopied: true });
- setTimeout(() => {
- this.setState({ isCopied: false });
- }, 1500);
- }
- render() {
- const { isCopied } = this.state;
- const { isAuthenticated, shortenerError, shortenerLoading, url } = this.props;
- return (
- <Wrapper>
- <ResultWrapper>
- {!shortenerError && (url.isShortened || shortenerLoading) ? (
- <ShortenerResult
- copyHandler={this.copyHandler}
- loading={shortenerLoading}
- url={url}
- isCopied={isCopied}
- />
- ) : (
- <ShortenerTitle />
- )}
- </ResultWrapper>
- <ShortenerInput
- isAuthenticated={isAuthenticated}
- handleSubmit={this.handleSubmit}
- setShortenerFormError={this.props.setShortenerFormError}
- domain={this.props.domain}
- />
- </Wrapper>
- );
- }
- }
- Shortener.propTypes = {
- isAuthenticated: PropTypes.bool.isRequired,
- domain: PropTypes.string.isRequired,
- createShortUrl: PropTypes.func.isRequired,
- shortenerError: PropTypes.string.isRequired,
- shortenerLoading: PropTypes.bool.isRequired,
- setShortenerFormError: PropTypes.func.isRequired,
- showShortenerLoading: PropTypes.func.isRequired,
- url: PropTypes.shape({
- isShortened: PropTypes.bool.isRequired,
- }).isRequired,
- };
- const mapStateToProps = ({
- auth: { isAuthenticated },
- error: { shortener: shortenerError },
- loading: { shortener: shortenerLoading },
- settings: { customDomain: domain },
- url,
- }) => ({
- isAuthenticated,
- domain,
- shortenerError,
- shortenerLoading,
- url,
- });
- const mapDispatchToProps = dispatch => ({
- createShortUrl: bindActionCreators(createShortUrl, dispatch),
- setShortenerFormError: bindActionCreators(setShortenerFormError, dispatch),
- showShortenerLoading: bindActionCreators(showShortenerLoading, dispatch),
- });
- export default connect(mapStateToProps, mapDispatchToProps)(Shortener);
|