TBodyCount.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { Component } from 'react';
  2. import { bindActionCreators } from 'redux';
  3. import { connect } from 'react-redux';
  4. import PropTypes from 'prop-types';
  5. import Router from 'next/router';
  6. import styled from 'styled-components';
  7. import URL from 'url';
  8. import QRCode from 'qrcode.react';
  9. import TBodyButton from './TBodyButton';
  10. import { showPageLoading } from '../../../actions';
  11. import Modal from '../../Modal';
  12. const Wrapper = styled.div`
  13. display: flex;
  14. flex: 1 1 auto;
  15. justify-content: space-between;
  16. align-items: center;
  17. `;
  18. const Actions = styled.div`
  19. display: flex;
  20. justify-content: flex-end;
  21. align-items: center;
  22. button {
  23. margin: 0 2px 0 12px;
  24. }
  25. `;
  26. const Icon = styled.img`
  27. width: 12px;
  28. height: 12px;
  29. `;
  30. class TBodyCount extends Component {
  31. constructor() {
  32. super();
  33. this.state = {
  34. showQrCodeModal: false,
  35. };
  36. this.goTo = this.goTo.bind(this);
  37. this.toggleQrCodeModal = this.toggleQrCodeModal.bind(this);
  38. }
  39. toggleQrCodeModal() {
  40. this.setState(prevState => ({
  41. showQrCodeModal: !prevState.showQrCodeModal,
  42. }));
  43. }
  44. goTo(e) {
  45. e.preventDefault();
  46. this.props.showLoading();
  47. const host = URL.parse(this.props.url.shortUrl).hostname;
  48. Router.push(`/stats?id=${this.props.url.id}${`&domain=${host}`}`);
  49. }
  50. render() {
  51. const { showModal, url } = this.props;
  52. const showQrCode = window.innerWidth > 640;
  53. return (
  54. <Wrapper>
  55. {url.count || 0}
  56. <Actions>
  57. {url.password && <Icon src="/images/lock.svg" lowopacity />}
  58. {url.count > 0 && (
  59. <TBodyButton withText onClick={this.goTo}>
  60. <Icon src="/images/chart.svg" />
  61. Stats
  62. </TBodyButton>
  63. )}
  64. {showQrCode && (
  65. <TBodyButton onClick={this.toggleQrCodeModal}>
  66. <Icon src="/images/qrcode.svg" />
  67. </TBodyButton>
  68. )}
  69. <TBodyButton
  70. data-id={url.id}
  71. data-host={URL.parse(url.shortUrl).hostname}
  72. onClick={showModal}
  73. >
  74. <Icon src="/images/trash.svg" />
  75. </TBodyButton>
  76. </Actions>
  77. <Modal show={this.state.showQrCodeModal} close={this.toggleQrCodeModal}>
  78. <QRCode value={url.shortUrl} size={196} />
  79. </Modal>
  80. </Wrapper>
  81. );
  82. }
  83. }
  84. TBodyCount.propTypes = {
  85. showLoading: PropTypes.func.isRequired,
  86. showModal: PropTypes.func.isRequired,
  87. url: PropTypes.shape({
  88. count: PropTypes.number,
  89. id: PropTypes.string,
  90. password: PropTypes.bool,
  91. shortUrl: PropTypes.string,
  92. }).isRequired,
  93. };
  94. const mapDispatchToProps = dispatch => ({
  95. showLoading: bindActionCreators(showPageLoading, dispatch),
  96. });
  97. export default connect(
  98. null,
  99. mapDispatchToProps
  100. )(TBodyCount);