2020-12-07 13:17:19 +03:00
|
|
|
import classNames from 'classnames';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { isEmpty } from 'ramda';
|
|
|
|
import type { ReactNode } from 'react';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrlsOrderableFields } from './data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrlsRowType } from './helpers/ShortUrlsRow';
|
|
|
|
import type { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
|
2020-12-07 13:17:19 +03:00
|
|
|
import './ShortUrlsTable.scss';
|
|
|
|
|
2022-12-18 12:12:34 +03:00
|
|
|
interface ShortUrlsTableProps {
|
2021-12-25 12:24:37 +03:00
|
|
|
orderByColumn?: (column: ShortUrlsOrderableFields) => () => void;
|
|
|
|
renderOrderIcon?: (column: ShortUrlsOrderableFields) => ReactNode;
|
2020-12-07 13:17:19 +03:00
|
|
|
shortUrlsList: ShortUrlsListState;
|
2020-12-20 11:09:22 +03:00
|
|
|
onTagClick?: (tag: string) => void;
|
2020-12-07 13:17:19 +03:00
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
2022-12-18 12:12:34 +03:00
|
|
|
export const ShortUrlsTable = (ShortUrlsRow: ShortUrlsRowType) => ({
|
2020-12-07 13:17:19 +03:00
|
|
|
orderByColumn,
|
|
|
|
renderOrderIcon,
|
|
|
|
shortUrlsList,
|
2020-12-20 11:09:22 +03:00
|
|
|
onTagClick,
|
2020-12-07 13:17:19 +03:00
|
|
|
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);
|
2022-12-18 12:12:34 +03:00
|
|
|
const tableClasses = classNames('table table-hover responsive-table short-urls-table', className);
|
2020-12-07 13:17:19 +03:00
|
|
|
|
|
|
|
const renderShortUrls = () => {
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<tr>
|
2021-11-09 01:41:17 +03:00
|
|
|
<td colSpan={6} className="text-center table-danger text-dark">
|
|
|
|
Something went wrong while loading short URLs :(
|
|
|
|
</td>
|
2020-12-07 13:17:19 +03:00
|
|
|
</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}
|
2020-12-20 11:09:22 +03:00
|
|
|
onTagClick={onTagClick}
|
2020-12-07 13:17:19 +03:00
|
|
|
/>
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<table className={tableClasses}>
|
2021-08-22 11:46:47 +03:00
|
|
|
<thead className="responsive-table__header short-urls-table__header">
|
2020-12-07 13:17:19 +03:00
|
|
|
<tr>
|
|
|
|
<th className={orderableColumnsClasses} onClick={orderByColumn?.('dateCreated')}>
|
2021-11-01 15:41:16 +03:00
|
|
|
Created at {renderOrderIcon?.('dateCreated')}
|
2020-12-07 13:17:19 +03:00
|
|
|
</th>
|
|
|
|
<th className={orderableColumnsClasses} onClick={orderByColumn?.('shortCode')}>
|
2021-11-01 15:41:16 +03:00
|
|
|
Short URL {renderOrderIcon?.('shortCode')}
|
2020-12-07 13:17:19 +03:00
|
|
|
</th>
|
2022-05-01 12:00:46 +03:00
|
|
|
<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-11-01 15:41:16 +03:00
|
|
|
<span className="indivisible">Visits {renderOrderIcon?.('visits')}</span>
|
2020-12-07 13:17:19 +03:00
|
|
|
</th>
|
2022-12-19 21:40:04 +03:00
|
|
|
<th className="short-urls-table__header-cell" colSpan={2} />
|
2020-12-07 13:17:19 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{renderShortUrls()}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
};
|
2022-12-18 12:12:34 +03:00
|
|
|
|
|
|
|
export type ShortUrlsTableType = ReturnType<typeof ShortUrlsTable>;
|