Table.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 THead from './THead';
  7. import TBody from './TBody';
  8. import TableOptions from './TableOptions';
  9. import { deleteShortUrl, getUrlsList } from '../../actions';
  10. import Modal from '../Modal';
  11. const Wrapper = styled.div`
  12. width: 1200px;
  13. max-width: 95%;
  14. display: flex;
  15. flex-direction: column;
  16. margin: 40px 0 120px;
  17. `;
  18. const Title = styled.h2`
  19. font-size: 24px;
  20. font-weight: 300;
  21. @media only screen and (max-width: 768px) {
  22. font-size: 18px;
  23. }
  24. `;
  25. const TableWrapper = styled.table`
  26. display: flex;
  27. flex: 1 1 auto;
  28. flex-direction: column;
  29. background-color: white;
  30. border-radius: 12px;
  31. box-shadow: 0 6px 30px rgba(50, 50, 50, 0.2);
  32. tr {
  33. display: flex;
  34. flex: 1 1 auto;
  35. padding: 0 24px;
  36. justify-content: space-between;
  37. border-bottom: 1px solid #eaeaea;
  38. }
  39. th,
  40. td {
  41. position: relative;
  42. display: flex;
  43. padding: 16px 0;
  44. align-items: center;
  45. }
  46. @media only screen and (max-width: 768px) {
  47. font-size: 13px;
  48. }
  49. @media only screen and (max-width: 510px) {
  50. tr {
  51. padding: 0 16px;
  52. }
  53. th,
  54. td {
  55. padding: 12px 0;
  56. }
  57. }
  58. `;
  59. const TFoot = styled.tfoot`
  60. background-color: #f1f1f1;
  61. border-bottom-right-radius: 12px;
  62. border-bottom-left-radius: 12px;
  63. `;
  64. class Table extends Component {
  65. constructor() {
  66. super();
  67. this.state = {
  68. copiedIndex: -1,
  69. modalUrlId: '',
  70. modalUrlDomain: '',
  71. showModal: false,
  72. };
  73. this.handleCopy = this.handleCopy.bind(this);
  74. this.showModal = this.showModal.bind(this);
  75. this.closeModal = this.closeModal.bind(this);
  76. this.deleteUrl = this.deleteUrl.bind(this);
  77. }
  78. handleCopy(index) {
  79. this.setState({ copiedIndex: index });
  80. setTimeout(() => {
  81. this.setState({ copiedIndex: -1 });
  82. }, 1500);
  83. }
  84. showModal(e) {
  85. e.preventDefault();
  86. const modalUrlId = e.currentTarget.dataset.id;
  87. const modalUrlDomain = e.currentTarget.dataset.host;
  88. this.setState({
  89. modalUrlId,
  90. modalUrlDomain,
  91. showModal: true,
  92. });
  93. }
  94. closeModal() {
  95. this.setState({
  96. modalUrlId: '',
  97. modalUrlDomain: '',
  98. showModal: false,
  99. });
  100. }
  101. deleteUrl() {
  102. this.closeModal();
  103. const { modalUrlId, modalUrlDomain } = this.state;
  104. this.props.deleteShortUrl({ id: modalUrlId, domain: modalUrlDomain });
  105. }
  106. render() {
  107. const { copiedIndex } = this.state;
  108. const { url } = this.props;
  109. return (
  110. <Wrapper>
  111. <Title>Recent shortened links.</Title>
  112. <TableWrapper>
  113. <THead />
  114. <TBody
  115. copiedIndex={copiedIndex}
  116. handleCopy={this.handleCopy}
  117. urls={url.list}
  118. showModal={this.showModal}
  119. />
  120. <TFoot>
  121. <TableOptions nosearch />
  122. </TFoot>
  123. </TableWrapper>
  124. <Modal show={this.state.showModal} handler={this.deleteUrl} close={this.closeModal}>
  125. Are you sure do you want to delete the short URL and its stats?
  126. </Modal>
  127. </Wrapper>
  128. );
  129. }
  130. }
  131. Table.propTypes = {
  132. deleteShortUrl: PropTypes.func.isRequired,
  133. url: PropTypes.shape({
  134. list: PropTypes.array.isRequired,
  135. }).isRequired,
  136. };
  137. const mapStateToProps = ({ url }) => ({ url });
  138. const mapDispatchToProps = dispatch => ({
  139. deleteShortUrl: bindActionCreators(deleteShortUrl, dispatch),
  140. getUrlsList: bindActionCreators(getUrlsList, dispatch),
  141. });
  142. export default connect(
  143. mapStateToProps,
  144. mapDispatchToProps
  145. )(Table);