TBodyCount.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 TBodyButton from './TBodyButton';
  9. import { showPageLoading } from '../../../actions';
  10. const Wrapper = styled.div`
  11. display: flex;
  12. flex: 1 1 auto;
  13. justify-content: space-between;
  14. align-items: center;
  15. `;
  16. const Actions = styled.div`
  17. display: flex;
  18. justify-content: flex-end;
  19. align-items: center;
  20. button {
  21. margin: 0 2px 0 12px;
  22. }
  23. `;
  24. const Icon = styled.img`
  25. width: 12px;
  26. height: 12px;
  27. `;
  28. class TBodyCount extends Component {
  29. constructor() {
  30. super();
  31. this.goTo = this.goTo.bind(this);
  32. }
  33. goTo(e) {
  34. e.preventDefault();
  35. this.props.showLoading();
  36. const host = URL.parse(this.props.url.shortUrl).hostname;
  37. Router.push(`/stats?id=${this.props.url.id}${`&domain=${host}`}`);
  38. }
  39. render() {
  40. const { showModal, url } = this.props;
  41. return (
  42. <Wrapper>
  43. {url.count || 0}
  44. <Actions>
  45. {url.password && <Icon src="/images/lock.svg" lowopacity />}
  46. {url.count > 0 && (
  47. <TBodyButton withText onClick={this.goTo}>
  48. <Icon src="/images/chart.svg" />
  49. Stats
  50. </TBodyButton>
  51. )}
  52. <TBodyButton
  53. data-id={url.id}
  54. data-host={URL.parse(url.shortUrl).hostname}
  55. onClick={showModal}
  56. >
  57. <Icon src="/images/trash.svg" />
  58. </TBodyButton>
  59. </Actions>
  60. </Wrapper>
  61. );
  62. }
  63. }
  64. TBodyCount.propTypes = {
  65. showLoading: PropTypes.func.isRequired,
  66. showModal: PropTypes.func.isRequired,
  67. url: PropTypes.shape({
  68. count: PropTypes.number,
  69. id: PropTypes.string,
  70. password: PropTypes.bool,
  71. shortUrl: PropTypes.string,
  72. }).isRequired,
  73. };
  74. const mapDispatchToProps = dispatch => ({
  75. showLoading: bindActionCreators(showPageLoading, dispatch),
  76. });
  77. export default connect(null, mapDispatchToProps)(TBodyCount);