2021-11-09 01:41:17 +03:00
|
|
|
import { head, keys, pipe, values } from 'ramda';
|
|
|
|
import { FC, useEffect, useMemo, 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';
|
2021-02-28 12:12:30 +03:00
|
|
|
import { Topics } from '../mercure/helpers/Topics';
|
2021-11-07 00:34:29 +03:00
|
|
|
import { TableOrderIcon } from '../utils/table/TableOrderIcon';
|
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';
|
2021-11-09 01:41:17 +03:00
|
|
|
import { ShortUrlListRouteParams, useShortUrlsQuery } from './helpers/hooks';
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2021-11-09 01:41:17 +03:00
|
|
|
interface ShortUrlsListProps extends RouteComponentProps<ShortUrlListRouteParams> {
|
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>;
|
|
|
|
|
2021-11-09 01:41:17 +03:00
|
|
|
const ShortUrlsList = (ShortUrlsTable: FC<ShortUrlsTableProps>, SearchBar: FC) => boundToMercureHub(({
|
2020-08-30 20:45:17 +03:00
|
|
|
listShortUrls,
|
|
|
|
resetShortUrlParams,
|
|
|
|
shortUrlsListParams,
|
|
|
|
match,
|
|
|
|
location,
|
2021-11-09 01:41:17 +03:00
|
|
|
history,
|
2020-08-30 20:45:17 +03:00
|
|
|
shortUrlsList,
|
|
|
|
selectedServer,
|
2020-12-07 13:17:19 +03:00
|
|
|
}: ShortUrlsListProps) => {
|
2021-11-09 01:41:17 +03:00
|
|
|
const serverId = getServerId(selectedServer);
|
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
|
|
|
});
|
2021-11-11 00:25:56 +03:00
|
|
|
const [{ tags, search, startDate, endDate }, toFirstPage ] = useShortUrlsQuery({ history, match, location });
|
|
|
|
const selectedTags = useMemo(() => tags?.split(',') ?? [], [ tags ]);
|
2020-12-25 13:15:49 +03:00
|
|
|
const { pagination } = shortUrlsList?.shortUrls ?? {};
|
2021-11-09 01:41:17 +03:00
|
|
|
|
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));
|
2021-11-07 00:34:29 +03:00
|
|
|
const renderOrderIcon = (field: OrderableFields) => <TableOrderIcon currentOrder={order} field={field} />;
|
2021-11-09 01:41:17 +03:00
|
|
|
const addTag = pipe(
|
2021-11-11 00:25:56 +03:00
|
|
|
(newTag: string) => [ ...new Set([ ...selectedTags, newTag ]) ].join(','),
|
2021-11-09 01:41:17 +03:00
|
|
|
(tags) => toFirstPage({ tags }),
|
|
|
|
);
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2021-11-09 01:41:17 +03:00
|
|
|
useEffect(() => resetShortUrlParams, []);
|
2020-08-30 20:45:17 +03:00
|
|
|
useEffect(() => {
|
2021-11-11 00:25:56 +03:00
|
|
|
refreshList(
|
|
|
|
{ page: match.params.page, searchTerm: search, tags: selectedTags, itemsPerPage: undefined, startDate, endDate },
|
|
|
|
);
|
|
|
|
}, [ match.params.page, search, selectedTags, startDate, endDate ]);
|
2020-08-30 20:45:17 +03:00
|
|
|
|
|
|
|
return (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2021-11-09 01:41:17 +03:00
|
|
|
<div className="mb-3"><SearchBar /></div>
|
2020-12-30 22:05:04 +03:00
|
|
|
<div className="d-block d-lg-none mb-3">
|
2021-11-06 14:26:20 +03:00
|
|
|
<SortingDropdown items={SORTABLE_FIELDS} order={order} onChange={handleOrderBy} />
|
2020-08-30 20:45:17 +03:00
|
|
|
</div>
|
2020-12-25 13:15:49 +03:00
|
|
|
<Card body className="pb-1">
|
|
|
|
<ShortUrlsTable
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
shortUrlsList={shortUrlsList}
|
2021-11-09 01:41:17 +03:00
|
|
|
orderByColumn={orderByColumn}
|
|
|
|
renderOrderIcon={renderOrderIcon}
|
|
|
|
onTagClick={addTag}
|
2020-12-25 13:15:49 +03:00
|
|
|
/>
|
2021-11-09 01:41:17 +03:00
|
|
|
<Paginator paginator={pagination} serverId={serverId} currentQueryString={location.search} />
|
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;
|