All files / recent-reports/components/ReportsTable/components/ReportItem index.jsx

86.67% Statements 13/15
100% Branches 4/4
75% Functions 3/4
86.67% Lines 13/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                        1x                                               1x                                     1x                       2x 1x     1x       1x     1x           3x           3x       3x                                           1x             1x            
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { Text, Link } from '@bufferapp/components';
import moment from 'moment';
 
import {
  geyser,
  outerSpace,
} from '@bufferapp/components/style/color';
import { blue } from '@bufferapp/ui/style/colors';
 
const ReportRow = styled.tr`
  margin: 0;
  padding: 0;
  border-color: ${geyser};
 
  &:first-child > td {
    border-top: 1px dotted ${geyser};
  }
 
  &:last-child > td {
    border-bottom: none;
  }
 
  a {
    pointer-events: none !important;
    transition: opacity 0.25s cubic-bezier(0.455, 0.03, 0.515, 0.955) !important;
  }
 
  &:hover a {
    opacity: 1 !important;
    pointer-events: all !important;
  }
`;
 
const ContentCell = styled.td`
  color: ${outerSpace};
  padding: 0.75rem 1rem 0.8rem;
  vertical-align: middle;
  border-bottom: 1px dotted ${geyser};
  border-right: 1px dotted ${geyser};
  align-items: center;
  width: 18%;
 
  &:first-child {
    width: 46%;
    padding-left: 1.5rem;
  }
 
  &:last-child {
    border-right: none;
  }
`;
 
const ReportCell = styled.div`
  display: flex;
  align-items: center;
  margin-top: 2px;
 
  &:hover a > span {
    color: ${blue} !important;
    cursor: pointer;
  }
`;
 
function daysString(value) {
  if (value === 1) {
    return 'day'
  }
 
  return 'days';
}
 
function customDateString(start, end) {
  return `${moment(new Date(start)).format('MMMM Do')} - ${moment(new Date(end)).format('MMMM Do')}`;;
}
 
const ReportItem = ({
  title,
  dateRange: {range, start, end},
  reportId,
  openReport,
}) => {
  const handleReportClick = e => {
    e.preventDefault();
 
    openReport(reportId);
  };
 
  const dateRangeString = range
    ? `Last ${range} ${daysString(range)}`
    : customDateString(start, end);
 
  return (
    <ReportRow>
        <ContentCell>
          <ReportCell>
            <Link
              onClick={handleReportClick}
              href={''}
              unstyled
            >
              <Text size="mini">{title}</Text>
            </Link>
          </ReportCell>
        </ContentCell>
        <ContentCell>
          <ReportCell>
            <Text size="mini">{dateRangeString}</Text>
          </ReportCell>
        </ContentCell>
    </ReportRow>
  );
}
 
ReportItem.propTypes = {
  title: PropTypes.string,
  dateRange: PropTypes.object,
  reportId: PropTypes.string,
  openReport: PropTypes.func,
};
 
ReportItem.defaultProps = {
  loading: true,
  dateRange: {}
};
 
export default ReportItem;