Table.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React, { Component, FC, useState } from "react";
  2. import { bindActionCreators } from "redux";
  3. import { connect } from "react-redux";
  4. import styled from "styled-components";
  5. import { Flex } from "reflexbox/styled-components";
  6. import THead from "./Table/THead";
  7. import TBody from "./Table/TBody";
  8. import TableOptions from "./TableOptions";
  9. import { deleteShortUrl, getUrlsList } from "../actions";
  10. import Modal from "./Modal";
  11. interface Props {
  12. deleteShortUrl: any; // TODO: types
  13. url: {
  14. list: any[]; // TODO: types
  15. };
  16. }
  17. const Title = styled.h2`
  18. font-size: 24px;
  19. font-weight: 300;
  20. @media only screen and (max-width: 768px) {
  21. font-size: 18px;
  22. }
  23. `;
  24. const TableWrapper = styled.table`
  25. display: flex;
  26. flex: 1 1 auto;
  27. flex-direction: column;
  28. background-color: white;
  29. border-radius: 12px;
  30. box-shadow: 0 6px 30px rgba(50, 50, 50, 0.2);
  31. tr {
  32. display: flex;
  33. flex: 1 1 auto;
  34. padding: 0 24px;
  35. justify-content: space-between;
  36. border-bottom: 1px solid #eaeaea;
  37. }
  38. th,
  39. td {
  40. position: relative;
  41. display: flex;
  42. padding: 16px 0;
  43. align-items: center;
  44. }
  45. @media only screen and (max-width: 768px) {
  46. font-size: 13px;
  47. }
  48. @media only screen and (max-width: 510px) {
  49. tr {
  50. padding: 0 16px;
  51. }
  52. th,
  53. td {
  54. padding: 12px 0;
  55. }
  56. }
  57. `;
  58. const TFoot = styled.tfoot`
  59. background-color: #f1f1f1;
  60. border-bottom-right-radius: 12px;
  61. border-bottom-left-radius: 12px;
  62. `;
  63. const defualtModal = {
  64. id: "",
  65. domain: "",
  66. show: false
  67. };
  68. const Table: FC<Props> = ({ deleteShortUrl, url }) => {
  69. const [copiedIndex, setCopiedIndex] = useState(-1);
  70. const [modal, setModal] = useState(defualtModal);
  71. function handleCopy(index) {
  72. setCopiedIndex(index);
  73. setTimeout(() => {
  74. setCopiedIndex(-1);
  75. }, 1500);
  76. }
  77. function showModal(url) {
  78. return e => {
  79. e.preventDefault();
  80. setModal({
  81. id: url.address,
  82. domain: url.domain,
  83. show: true
  84. });
  85. };
  86. }
  87. const closeModal = () => setModal(defualtModal);
  88. function deleteUrl() {
  89. closeModal();
  90. deleteShortUrl({ id: modal.id, domain: modal.domain });
  91. }
  92. return (
  93. <Flex
  94. width={1200}
  95. maxWidth="95%"
  96. flexDirection="column"
  97. margin="40px 0 120px"
  98. >
  99. <Title>Recent shortened links.</Title>
  100. <TableWrapper>
  101. <THead />
  102. <TBody
  103. copiedIndex={copiedIndex}
  104. handleCopy={handleCopy}
  105. urls={url.list}
  106. showModal={showModal}
  107. />
  108. <TFoot>
  109. <TableOptions nosearch />
  110. </TFoot>
  111. </TableWrapper>
  112. <Modal show={modal.show}>
  113. Are you sure do you want to delete the short URL and its stats?
  114. </Modal>
  115. </Flex>
  116. );
  117. };
  118. const mapStateToProps = ({ url }) => ({ url });
  119. const mapDispatchToProps = dispatch => ({
  120. deleteShortUrl: bindActionCreators(deleteShortUrl, dispatch),
  121. getUrlsList: bindActionCreators(getUrlsList, dispatch)
  122. });
  123. export default connect(mapStateToProps, mapDispatchToProps)(Table);