ShortenerResult.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import { CopyToClipboard } from 'react-copy-to-clipboard';
  5. import QRCode from 'qrcode.react';
  6. import Button from '../Button';
  7. import Loading from '../PageLoading';
  8. import { fadeIn } from '../../helpers/animations';
  9. import TBodyButton from '../Table/TBody/TBodyButton';
  10. import Modal from '../Modal';
  11. const Wrapper = styled.div`
  12. position: relative;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. button {
  17. margin-left: 24px;
  18. }
  19. `;
  20. const Url = styled.h2`
  21. margin: 8px 0;
  22. font-size: 32px;
  23. font-weight: 300;
  24. border-bottom: 2px dotted #aaa;
  25. cursor: pointer;
  26. transition: all 0.2s ease;
  27. :hover {
  28. opacity: 0.5;
  29. }
  30. @media only screen and (max-width: 448px) {
  31. font-size: 24px;
  32. }
  33. `;
  34. const CopyMessage = styled.p`
  35. position: absolute;
  36. top: -32px;
  37. left: 0;
  38. font-size: 14px;
  39. color: #689f38;
  40. animation: ${fadeIn} 0.3s ease-out;
  41. `;
  42. const QRButton = styled(TBodyButton)`
  43. width: 36px;
  44. height: 36px;
  45. margin-left: 12px !important;
  46. box-shadow: 0 4px 10px rgba(100, 100, 100, 0.2);
  47. :hover {
  48. box-shadow: 0 4px 10px rgba(100, 100, 100, 0.3);
  49. }
  50. @media only screen and (max-width: 768px) {
  51. height: 32px;
  52. width: 32px;
  53. img {
  54. width: 14px;
  55. height: 14px;
  56. }
  57. }
  58. `;
  59. const Icon = styled.img`
  60. width: 16px;
  61. height: 16px;
  62. `;
  63. class ShortenerResult extends Component {
  64. constructor() {
  65. super();
  66. this.state = {
  67. showQrCodeModal: false,
  68. };
  69. this.toggleQrCodeModal = this.toggleQrCodeModal.bind(this);
  70. }
  71. toggleQrCodeModal() {
  72. this.setState(prevState => ({
  73. showQrCodeModal: !prevState.showQrCodeModal,
  74. }));
  75. }
  76. render() {
  77. const { copyHandler, isCopied, loading, url } = this.props;
  78. const showQrCode = window.innerWidth > 420;
  79. if (loading) return <Loading />;
  80. return (
  81. <Wrapper>
  82. {isCopied && <CopyMessage>Copied to clipboard.</CopyMessage>}
  83. <CopyToClipboard text={url.list[0].shortLink} onCopy={copyHandler}>
  84. <Url>{url.list[0].shortLink.replace(/^https?:\/\//, '')}</Url>
  85. </CopyToClipboard>
  86. <CopyToClipboard text={url.list[0].shortLink} onCopy={copyHandler}>
  87. <Button icon="copy">Copy</Button>
  88. </CopyToClipboard>
  89. {showQrCode && (
  90. <QRButton onClick={this.toggleQrCodeModal}>
  91. <Icon src="/images/qrcode.svg" />
  92. </QRButton>
  93. )}
  94. <Modal show={this.state.showQrCodeModal} close={this.toggleQrCodeModal}>
  95. <QRCode value={url.list[0].shortLink} size={196} />
  96. </Modal>
  97. </Wrapper>
  98. );
  99. }
  100. }
  101. ShortenerResult.propTypes = {
  102. copyHandler: PropTypes.func.isRequired,
  103. isCopied: PropTypes.bool.isRequired,
  104. loading: PropTypes.bool.isRequired,
  105. url: PropTypes.shape({
  106. list: PropTypes.array.isRequired,
  107. }).isRequired,
  108. };
  109. export default ShortenerResult;