2020-12-07 13:17:19 +03:00
|
|
|
import { FC, ReactNode } from 'react';
|
|
|
|
import { isEmpty } from 'ramda';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { SelectedServer } from '../servers/data';
|
2021-03-05 16:20:49 +03:00
|
|
|
import { titleIsSupported } from '../utils/helpers/features';
|
2020-12-07 13:17:19 +03:00
|
|
|
import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
|
|
|
|
import { ShortUrlsRowProps } from './helpers/ShortUrlsRow';
|
2020-12-20 11:09:22 +03:00
|
|
|
import { OrderableFields } from './reducers/shortUrlsListParams';
|
2020-12-07 13:17:19 +03:00
|
|
|
import './ShortUrlsTable.scss';
|
|
|
|
|
|
|
|
export interface ShortUrlsTableProps {
|
|
|
|
orderByColumn?: (column: OrderableFields) => () => void;
|
|
|
|
renderOrderIcon?: (column: OrderableFields) => ReactNode;
|
|
|
|
shortUrlsList: ShortUrlsListState;
|
|
|
|
selectedServer: SelectedServer;
|
2020-12-20 11:09:22 +03:00
|
|
|
onTagClick?: (tag: string) => void;
|
2020-12-07 13:17:19 +03:00
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ShortUrlsTable = (ShortUrlsRow: FC<ShortUrlsRowProps>) => ({
|
|
|
|
orderByColumn,
|
|
|
|
renderOrderIcon,
|
|
|
|
shortUrlsList,
|
2020-12-20 11:09:22 +03:00
|
|
|
onTagClick,
|
2020-12-07 13:17:19 +03:00
|
|
|
selectedServer,
|
|
|
|
className,
|
|
|
|
}: ShortUrlsTableProps) => {
|
|
|
|
const { error, loading, shortUrls } = shortUrlsList;
|
2021-03-05 16:20:49 +03:00
|
|
|
const actionableFieldClasses = classNames({ 'short-urls-table__header-cell--with-action': !!orderByColumn });
|
|
|
|
const orderableColumnsClasses = classNames('short-urls-table__header-cell', actionableFieldClasses);
|
2020-12-12 12:56:10 +03:00
|
|
|
const tableClasses = classNames('table table-hover', className);
|
2021-03-05 16:20:49 +03:00
|
|
|
const supportsTitle = titleIsSupported(selectedServer);
|
2020-12-07 13:17:19 +03:00
|
|
|
|
|
|
|
const renderShortUrls = () => {
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<tr>
|
|
|
|
<td colSpan={6} className="text-center table-danger">Something went wrong while loading short URLs :(</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <tr><td colSpan={6} className="text-center">Loading...</td></tr>;
|
|
|
|
}
|
|
|
|
|
2020-12-15 01:35:31 +03:00
|
|
|
if (!loading && isEmpty(shortUrls?.data)) {
|
2020-12-07 13:17:19 +03:00
|
|
|
return <tr><td colSpan={6} className="text-center">No results found</td></tr>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shortUrls?.data.map((shortUrl) => (
|
|
|
|
<ShortUrlsRow
|
|
|
|
key={shortUrl.shortUrl}
|
|
|
|
shortUrl={shortUrl}
|
|
|
|
selectedServer={selectedServer}
|
2020-12-20 11:09:22 +03:00
|
|
|
onTagClick={onTagClick}
|
2020-12-07 13:17:19 +03:00
|
|
|
/>
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<table className={tableClasses}>
|
|
|
|
<thead className="short-urls-table__header">
|
|
|
|
<tr>
|
|
|
|
<th className={orderableColumnsClasses} onClick={orderByColumn?.('dateCreated')}>
|
|
|
|
Created at
|
2021-03-05 16:20:49 +03:00
|
|
|
{renderOrderIcon?.('dateCreated')}
|
2020-12-07 13:17:19 +03:00
|
|
|
</th>
|
|
|
|
<th className={orderableColumnsClasses} onClick={orderByColumn?.('shortCode')}>
|
|
|
|
Short URL
|
2021-03-05 16:20:49 +03:00
|
|
|
{renderOrderIcon?.('shortCode')}
|
2020-12-07 13:17:19 +03:00
|
|
|
</th>
|
2021-03-05 16:20:49 +03:00
|
|
|
{!supportsTitle && (
|
|
|
|
<th className={orderableColumnsClasses} onClick={orderByColumn?.('longUrl')}>
|
|
|
|
Long URL
|
|
|
|
{renderOrderIcon?.('longUrl')}
|
|
|
|
</th>
|
|
|
|
) || (
|
|
|
|
<th className="short-urls-table__header-cell">
|
|
|
|
<span className={actionableFieldClasses} onClick={orderByColumn?.('title')}>
|
|
|
|
Title
|
|
|
|
{renderOrderIcon?.('title')}
|
|
|
|
</span>
|
|
|
|
/
|
|
|
|
<span className={actionableFieldClasses} onClick={orderByColumn?.('longUrl')}>
|
|
|
|
<span className="indivisible">Long URL</span>
|
|
|
|
{renderOrderIcon?.('longUrl')}
|
|
|
|
</span>
|
|
|
|
</th>
|
|
|
|
)}
|
2020-12-07 13:17:19 +03:00
|
|
|
<th className="short-urls-table__header-cell">Tags</th>
|
|
|
|
<th className={orderableColumnsClasses} onClick={orderByColumn?.('visits')}>
|
2021-03-05 16:20:49 +03:00
|
|
|
<span className="indivisible">Visits{renderOrderIcon?.('visits')}</span>
|
2020-12-07 13:17:19 +03:00
|
|
|
</th>
|
|
|
|
<th className="short-urls-table__header-cell"> </th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{renderShortUrls()}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
};
|