shlink-web-client/src/short-urls/ShortUrlsList.tsx

84 lines
3.3 KiB
TypeScript
Raw Normal View History

import { pipe } from 'ramda';
2021-11-09 01:41:17 +03:00
import { FC, useEffect, useMemo, useState } from 'react';
import { RouteComponentProps } from 'react-router';
import { Card } from 'reactstrap';
import SortingDropdown from '../utils/SortingDropdown';
import { determineOrderDir, OrderDir } from '../utils/helpers/ordering';
import { getServerId, SelectedServer } from '../servers/data';
import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
import { Topics } from '../mercure/helpers/Topics';
import { TableOrderIcon } from '../utils/table/TableOrderIcon';
import { ShlinkShortUrlsListParams } from '../api/types';
import { DEFAULT_SHORT_URLS_ORDERING, Settings } from '../settings/reducers/settings';
import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
import { OrderableFields, ShortUrlsOrder, SORTABLE_FIELDS } from './reducers/shortUrlsListParams';
import { ShortUrlsTableProps } from './ShortUrlsTable';
import Paginator from './Paginator';
2021-11-09 01:41:17 +03:00
import { ShortUrlListRouteParams, useShortUrlsQuery } from './helpers/hooks';
2021-11-09 01:41:17 +03:00
interface ShortUrlsListProps extends RouteComponentProps<ShortUrlListRouteParams> {
selectedServer: SelectedServer;
shortUrlsList: ShortUrlsListState;
listShortUrls: (params: ShlinkShortUrlsListParams) => void;
settings: Settings;
}
2021-11-09 01:41:17 +03:00
const ShortUrlsList = (ShortUrlsTable: FC<ShortUrlsTableProps>, SearchBar: FC) => boundToMercureHub(({
listShortUrls,
match,
location,
2021-11-09 01:41:17 +03:00
history,
shortUrlsList,
selectedServer,
settings,
}: ShortUrlsListProps) => {
2021-11-09 01:41:17 +03:00
const serverId = getServerId(selectedServer);
const initialOrderBy = settings.shortUrlList?.defaultOrdering ?? DEFAULT_SHORT_URLS_ORDERING;
const [ order, setOrder ] = useState<ShortUrlsOrder>(initialOrderBy);
const [{ tags, search, startDate, endDate }, toFirstPage ] = useShortUrlsQuery({ history, match, location });
const selectedTags = useMemo(() => tags?.split(',') ?? [], [ tags ]);
const { pagination } = shortUrlsList?.shortUrls ?? {};
2021-11-09 01:41:17 +03:00
const handleOrderBy = (field?: OrderableFields, dir?: OrderDir) => setOrder({ field, dir });
const orderByColumn = (field: OrderableFields) => () =>
2021-11-01 15:41:16 +03:00
handleOrderBy(field, determineOrderDir(field, order.field, order.dir));
const renderOrderIcon = (field: OrderableFields) => <TableOrderIcon currentOrder={order} field={field} />;
2021-11-09 01:41:17 +03:00
const addTag = pipe(
(newTag: string) => [ ...new Set([ ...selectedTags, newTag ]) ].join(','),
2021-11-09 01:41:17 +03:00
(tags) => toFirstPage({ tags }),
);
useEffect(() => {
listShortUrls({
page: match.params.page,
searchTerm: search,
tags: selectedTags,
itemsPerPage: undefined,
startDate,
endDate,
orderBy: order,
});
}, [ match.params.page, search, selectedTags, startDate, endDate, order ]);
return (
2020-11-14 00:44:26 +03:00
<>
2021-11-09 01:41:17 +03:00
<div className="mb-3"><SearchBar /></div>
<div className="d-block d-lg-none mb-3">
<SortingDropdown items={SORTABLE_FIELDS} order={order} onChange={handleOrderBy} />
</div>
<Card body className="pb-1">
<ShortUrlsTable
selectedServer={selectedServer}
shortUrlsList={shortUrlsList}
2021-11-09 01:41:17 +03:00
orderByColumn={orderByColumn}
renderOrderIcon={renderOrderIcon}
onTagClick={addTag}
/>
2021-11-09 01:41:17 +03:00
<Paginator paginator={pagination} serverId={serverId} currentQueryString={location.search} />
</Card>
2020-11-14 00:44:26 +03:00
</>
);
}, () => [ Topics.visits ]);
export default ShortUrlsList;