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"> </th> |
84
|
|
|
</tr> |
85
|
|
|
</thead> |
86
|
|
|
<tbody> |
87
|
|
|
{renderShortUrls()} |
88
|
|
|
</tbody> |
89
|
|
|
</table> |
90
|
|
|
); |
91
|
|
|
}; |
92
|
|
|
|