2021-12-24 15:14:13 +03:00
|
|
|
import { pipe } from 'ramda';
|
2021-11-09 01:41:17 +03:00
|
|
|
import { FC, useEffect, useMemo, useState } from 'react';
|
2020-12-25 13:15:49 +03:00
|
|
|
import { Card } from 'reactstrap';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2021-12-25 12:31:48 +03:00
|
|
|
import { OrderingDropdown } from '../utils/OrderingDropdown';
|
2021-12-24 13:05:22 +03:00
|
|
|
import { determineOrderDir, 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';
|
2021-11-11 23:28:17 +03:00
|
|
|
import { ShlinkShortUrlsListParams } from '../api/types';
|
2021-12-24 15:14:13 +03:00
|
|
|
import { DEFAULT_SHORT_URLS_ORDERING, Settings } from '../settings/reducers/settings';
|
2020-08-30 20:45:17 +03:00
|
|
|
import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
|
2020-12-07 13:17:19 +03:00
|
|
|
import { ShortUrlsTableProps } from './ShortUrlsTable';
|
2020-12-25 13:15:49 +03:00
|
|
|
import Paginator from './Paginator';
|
2022-02-06 22:07:18 +03:00
|
|
|
import { useShortUrlsQuery } from './helpers/hooks';
|
2021-12-25 21:51:25 +03:00
|
|
|
import { ShortUrlsOrderableFields, SHORT_URLS_ORDERABLE_FIELDS } from './data';
|
2022-03-12 22:51:30 +03:00
|
|
|
import { ShortUrlsFilteringProps } from './ShortUrlsFilteringBar';
|
2020-08-30 20:45:17 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
interface ShortUrlsListProps {
|
2020-08-30 20:45:17 +03:00
|
|
|
selectedServer: SelectedServer;
|
2020-12-07 13:17:19 +03:00
|
|
|
shortUrlsList: ShortUrlsListState;
|
2021-12-24 15:14:13 +03:00
|
|
|
listShortUrls: (params: ShlinkShortUrlsListParams) => void;
|
|
|
|
settings: Settings;
|
2020-08-30 20:45:17 +03:00
|
|
|
}
|
|
|
|
|
2022-03-12 22:51:30 +03:00
|
|
|
const ShortUrlsList = (
|
|
|
|
ShortUrlsTable: FC<ShortUrlsTableProps>,
|
|
|
|
ShortUrlsFilteringBar: FC<ShortUrlsFilteringProps>,
|
|
|
|
) => boundToMercureHub(({ listShortUrls, shortUrlsList, selectedServer, settings }: ShortUrlsListProps) => {
|
2021-11-09 01:41:17 +03:00
|
|
|
const serverId = getServerId(selectedServer);
|
2022-02-06 22:07:18 +03:00
|
|
|
const { page } = useParams();
|
2022-02-08 00:17:57 +03:00
|
|
|
const location = useLocation();
|
2022-02-08 21:40:51 +03:00
|
|
|
const [{ tags, search, startDate, endDate, orderBy, tagsMode }, toFirstPage ] = useShortUrlsQuery();
|
2021-12-25 21:51:25 +03:00
|
|
|
const [ actualOrderBy, setActualOrderBy ] = useState(
|
|
|
|
// This separated state handling is needed to be able to fall back to settings value, but only once when loaded
|
|
|
|
orderBy ?? settings.shortUrlsList?.defaultOrdering ?? DEFAULT_SHORT_URLS_ORDERING,
|
|
|
|
);
|
2021-11-11 00:25:56 +03:00
|
|
|
const selectedTags = useMemo(() => tags?.split(',') ?? [], [ tags ]);
|
2020-12-25 13:15:49 +03:00
|
|
|
const { pagination } = shortUrlsList?.shortUrls ?? {};
|
2021-12-25 21:51:25 +03:00
|
|
|
const handleOrderBy = (field?: ShortUrlsOrderableFields, dir?: OrderDir) => {
|
|
|
|
toFirstPage({ orderBy: { field, dir } });
|
|
|
|
setActualOrderBy({ field, dir });
|
|
|
|
};
|
2021-12-25 12:24:37 +03:00
|
|
|
const orderByColumn = (field: ShortUrlsOrderableFields) => () =>
|
2021-12-25 21:51:25 +03:00
|
|
|
handleOrderBy(field, determineOrderDir(field, actualOrderBy.field, actualOrderBy.dir));
|
|
|
|
const renderOrderIcon = (field: ShortUrlsOrderableFields) =>
|
|
|
|
<TableOrderIcon currentOrder={actualOrderBy} 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
|
|
|
|
|
|
|
useEffect(() => {
|
2021-12-24 15:14:13 +03:00
|
|
|
listShortUrls({
|
2022-02-06 22:07:18 +03:00
|
|
|
page,
|
2021-12-24 15:14:13 +03:00
|
|
|
searchTerm: search,
|
|
|
|
tags: selectedTags,
|
|
|
|
startDate,
|
|
|
|
endDate,
|
2021-12-25 21:51:25 +03:00
|
|
|
orderBy: actualOrderBy,
|
2022-01-31 12:15:25 +03:00
|
|
|
tagsMode,
|
2021-12-24 15:14:13 +03:00
|
|
|
});
|
2022-02-08 21:40:51 +03:00
|
|
|
}, [ page, search, selectedTags, startDate, endDate, actualOrderBy, tagsMode ]);
|
2020-08-30 20:45:17 +03:00
|
|
|
|
|
|
|
return (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2022-03-12 22:51:30 +03:00
|
|
|
<ShortUrlsFilteringBar selectedServer={selectedServer} className="mb-3" />
|
2020-12-30 22:05:04 +03:00
|
|
|
<div className="d-block d-lg-none mb-3">
|
2021-12-25 21:51:25 +03:00
|
|
|
<OrderingDropdown items={SHORT_URLS_ORDERABLE_FIELDS} order={actualOrderBy} 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;
|