Passed
Pull Request — main (#343)
by Alejandro
03:59
created

src/short-urls/ShortUrlsTable.tsx   A

Complexity

Total Complexity 12
Complexity/F 0

Size

Lines of Code 92
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 12
eloc 73
mnd 12
bc 12
fnc 0
dl 0
loc 92
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
ccs 10
cts 14
cp 0.7143
rs 10
1
import { FC, ReactNode } from 'react';
2
import { isEmpty } from 'ramda';
3
import classNames from 'classnames';
4
import { SelectedServer } from '../servers/data';
5
import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
6
import { ShortUrlsRowProps } from './helpers/ShortUrlsRow';
7
import { OrderableFields, ShortUrlsListParams } from './reducers/shortUrlsListParams';
8
import './ShortUrlsTable.scss';
9
10
export interface ShortUrlsTableProps {
11
  orderByColumn?: (column: OrderableFields) => () => void;
12
  renderOrderIcon?: (column: OrderableFields) => ReactNode;
13
  shortUrlsList: ShortUrlsListState;
14
  selectedServer: SelectedServer;
15
  refreshList?: Function;
16
  shortUrlsListParams?: ShortUrlsListParams;
17
  className?: string;
18
}
19
20 1
export const ShortUrlsTable = (ShortUrlsRow: FC<ShortUrlsRowProps>) => ({
21
  orderByColumn,
22
  renderOrderIcon,
23
  shortUrlsList,
24
  refreshList,
25
  shortUrlsListParams,
26
  selectedServer,
27
  className,
28
}: ShortUrlsTableProps) => {
29 5
  const { error, loading, shortUrls } = shortUrlsList;
30 5
  const orderableColumnsClasses = classNames('short-urls-table__header-cell', {
31
    'short-urls-table__header-cell--with-action': !!orderByColumn,
32
  });
33 5
  const tableClasses = classNames('table table-striped table-hover', className);
34
35 5
  const renderShortUrls = () => {
36 5
    if (error) {
37
      return (
38
        <tr>
39
          <td colSpan={6} className="text-center table-danger">Something went wrong while loading short URLs :(</td>
40
        </tr>
41
      );
42
    }
43
44 5
    if (loading) {
45
      return <tr><td colSpan={6} className="text-center">Loading...</td></tr>;
46
    }
47
48 5
    if (!loading && isEmpty(shortUrlsList)) {
49
      return <tr><td colSpan={6} className="text-center">No results found</td></tr>;
50
    }
51
52 5
    return shortUrls?.data.map((shortUrl) => (
53
      <ShortUrlsRow
54
        key={shortUrl.shortUrl}
55
        shortUrl={shortUrl}
56
        selectedServer={selectedServer}
57
        refreshList={refreshList}
58
        shortUrlsListParams={shortUrlsListParams}
59
      />
60
    ));
61
  };
62
63 5
  return (
64
    <table className={tableClasses}>
65
      <thead className="short-urls-table__header">
66
        <tr>
67
          <th className={orderableColumnsClasses} onClick={orderByColumn?.('dateCreated')}>
68
            {renderOrderIcon?.('dateCreated')}
69
            Created at
70
          </th>
71
          <th className={orderableColumnsClasses} onClick={orderByColumn?.('shortCode')}>
72
            {renderOrderIcon?.('shortCode')}
73
            Short URL
74
          </th>
75
          <th className={orderableColumnsClasses} onClick={orderByColumn?.('longUrl')}>
76
            {renderOrderIcon?.('longUrl')}
77
            Long URL
78
          </th>
79
          <th className="short-urls-table__header-cell">Tags</th>
80
          <th className={orderableColumnsClasses} onClick={orderByColumn?.('visits')}>
81
            <span className="indivisible">{renderOrderIcon?.('visits')} Visits</span>
82
          </th>
83
          <th className="short-urls-table__header-cell">&nbsp;</th>
84
        </tr>
85
      </thead>
86
      <tbody>
87
        {renderShortUrls()}
88
      </tbody>
89
    </table>
90
  );
91
};
92