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

88 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-11-09 01:41:17 +03:00
import { head, keys, pipe, values } from 'ramda';
import { FC, useEffect, useMemo, useState } from 'react';
import { RouteComponentProps } from 'react-router';
import { Card } from 'reactstrap';
import SortingDropdown from '../utils/SortingDropdown';
2021-11-01 15:41:16 +03:00
import { determineOrderDir, Order, 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 { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
import { OrderableFields, ShortUrlsListParams, 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: 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(({
listShortUrls,
resetShortUrlParams,
shortUrlsListParams,
match,
location,
2021-11-09 01:41:17 +03:00
history,
shortUrlsList,
selectedServer,
}: ShortUrlsListProps) => {
2021-11-09 01:41:17 +03:00
const serverId = getServerId(selectedServer);
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)),
});
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 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 });
};
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 }),
);
2021-11-09 01:41:17 +03:00
useEffect(() => resetShortUrlParams, []);
useEffect(() => {
refreshList(
{ page: match.params.page, searchTerm: search, tags: selectedTags, itemsPerPage: undefined, startDate, endDate },
);
}, [ match.params.page, search, selectedTags, startDate, endDate ]);
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;