2020-08-30 20:45:17 +03:00
|
|
|
import { faCaretDown as caretDownIcon, faCaretUp as caretUpIcon } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-12-07 13:17:19 +03:00
|
|
|
import { head, keys, values } from 'ramda';
|
2020-11-14 00:44:26 +03:00
|
|
|
import { FC, useEffect, useState } from 'react';
|
2020-09-04 20:05:41 +03:00
|
|
|
import { RouteComponentProps } from 'react-router';
|
2020-12-25 13:15:49 +03:00
|
|
|
import { Card } from 'reactstrap';
|
2020-08-30 20:45:17 +03:00
|
|
|
import SortingDropdown from '../utils/SortingDropdown';
|
2021-11-01 15:41:16 +03:00
|
|
|
import { determineOrderDir, Order, OrderDir } from '../utils/helpers/ordering';
|
2021-10-17 14:43:37 +03:00
|
|
|
import { getServerId, SelectedServer } from '../servers/data';
|
2020-09-06 20:41:15 +03:00
|
|
|
import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
|
2020-12-20 21:28:09 +03:00
|
|
|
import { parseQuery } from '../utils/helpers/query';
|
2021-02-28 12:12:30 +03:00
|
|
|
import { Topics } from '../mercure/helpers/Topics';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
|
2020-09-12 18:59:58 +03:00
|
|
|
import { OrderableFields, ShortUrlsListParams, SORTABLE_FIELDS } from './reducers/shortUrlsListParams';
|
2020-12-07 13:17:19 +03:00
|
|
|
import { ShortUrlsTableProps } from './ShortUrlsTable';
|
2020-12-25 13:15:49 +03:00
|
|
|
import Paginator from './Paginator';
|
2020-08-30 20:45:17 +03:00
|
|
|
|
|
|
|
interface RouteParams {
|
|
|
|
page: string;
|
|
|
|
serverId: string;
|
|
|
|
}
|
|
|
|
|
2020-12-07 13:17:19 +03:00
|
|
|
export interface ShortUrlsListProps extends RouteComponentProps<RouteParams> {
|
2020-08-30 20:45:17 +03:00
|
|
|
selectedServer: SelectedServer;
|
2020-12-07 13:17:19 +03:00
|
|
|
shortUrlsList: ShortUrlsListState;
|
2020-08-30 20:45:17 +03:00
|
|
|
listShortUrls: (params: ShortUrlsListParams) => void;
|
|
|
|
shortUrlsListParams: ShortUrlsListParams;
|
|
|
|
resetShortUrlParams: () => void;
|
|
|
|
}
|
|
|
|
|
2021-11-01 15:41:16 +03:00
|
|
|
type ShortUrlsOrder = Order<OrderableFields>;
|
|
|
|
|
2020-12-07 13:17:19 +03:00
|
|
|
const ShortUrlsList = (ShortUrlsTable: FC<ShortUrlsTableProps>) => boundToMercureHub(({
|
2020-08-30 20:45:17 +03:00
|
|
|
listShortUrls,
|
|
|
|
resetShortUrlParams,
|
|
|
|
shortUrlsListParams,
|
|
|
|
match,
|
|
|
|
location,
|
|
|
|
shortUrlsList,
|
|
|
|
selectedServer,
|
2020-12-07 13:17:19 +03:00
|
|
|
}: ShortUrlsListProps) => {
|
2020-08-30 20:45:17 +03:00
|
|
|
const { orderBy } = shortUrlsListParams;
|
2021-11-01 15:41:16 +03:00
|
|
|
const [ order, setOrder ] = useState<ShortUrlsOrder>({
|
|
|
|
field: orderBy && (head(keys(orderBy)) as OrderableFields),
|
|
|
|
dir: orderBy && head(values(orderBy)),
|
2020-08-30 20:45:17 +03:00
|
|
|
});
|
2020-12-25 13:15:49 +03:00
|
|
|
const { pagination } = shortUrlsList?.shortUrls ?? {};
|
2020-08-30 20:45:17 +03:00
|
|
|
const refreshList = (extraParams: ShortUrlsListParams) => listShortUrls({ ...shortUrlsListParams, ...extraParams });
|
2021-11-01 15:41:16 +03:00
|
|
|
const handleOrderBy = (field?: OrderableFields, dir?: OrderDir) => {
|
|
|
|
setOrder({ field, dir });
|
|
|
|
refreshList({ orderBy: field ? { [field]: dir } : undefined });
|
2020-08-30 20:45:17 +03:00
|
|
|
};
|
|
|
|
const orderByColumn = (field: OrderableFields) => () =>
|
2021-11-01 15:41:16 +03:00
|
|
|
handleOrderBy(field, determineOrderDir(field, order.field, order.dir));
|
|
|
|
const renderOrderIcon = (field: OrderableFields) => order.dir && order.field === field &&
|
|
|
|
<FontAwesomeIcon icon={order.dir === 'ASC' ? caretUpIcon : caretDownIcon} className="ml-1" />;
|
2020-08-30 20:45:17 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-12-20 21:28:09 +03:00
|
|
|
const { tag } = parseQuery<{ tag?: string }>(location.search);
|
2021-09-01 11:53:45 +03:00
|
|
|
const tags = tag ? [ decodeURIComponent(tag) ] : shortUrlsListParams.tags;
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2020-12-07 13:17:19 +03:00
|
|
|
refreshList({ page: match.params.page, tags, itemsPerPage: undefined });
|
2020-08-30 20:45:17 +03:00
|
|
|
|
|
|
|
return resetShortUrlParams;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2020-12-30 22:05:04 +03:00
|
|
|
<div className="d-block d-lg-none mb-3">
|
2020-08-30 20:45:17 +03:00
|
|
|
<SortingDropdown
|
|
|
|
items={SORTABLE_FIELDS}
|
2021-11-01 15:41:16 +03:00
|
|
|
orderField={order.field}
|
|
|
|
orderDir={order.dir}
|
2020-08-30 20:45:17 +03:00
|
|
|
onChange={handleOrderBy}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-12-25 13:15:49 +03:00
|
|
|
<Card body className="pb-1">
|
|
|
|
<ShortUrlsTable
|
|
|
|
orderByColumn={orderByColumn}
|
|
|
|
renderOrderIcon={renderOrderIcon}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
shortUrlsList={shortUrlsList}
|
|
|
|
onTagClick={(tag) => refreshList({ tags: [ ...shortUrlsListParams.tags ?? [], tag ] })}
|
|
|
|
/>
|
2021-10-17 14:43:37 +03:00
|
|
|
<Paginator paginator={pagination} serverId={getServerId(selectedServer)} />
|
2020-12-25 13:15:49 +03:00
|
|
|
</Card>
|
2020-11-14 00:44:26 +03:00
|
|
|
</>
|
2020-08-30 20:45:17 +03:00
|
|
|
);
|
2021-08-20 18:30:07 +03:00
|
|
|
}, () => [ Topics.visits ]);
|
2020-08-30 20:45:17 +03:00
|
|
|
|
|
|
|
export default ShortUrlsList;
|