import { faCaretDown as caretDownIcon, faCaretUp as caretUpIcon } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { head, isEmpty, keys, values } from 'ramda'; import React, { useState, useEffect, FC } from 'react'; import qs from 'qs'; import { RouteComponentProps } from 'react-router'; import SortingDropdown from '../utils/SortingDropdown'; import { determineOrderDir, OrderDir } from '../utils/utils'; import { SelectedServer } from '../servers/data'; import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub'; import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList'; import { ShortUrlsRowProps } from './helpers/ShortUrlsRow'; import { ShortUrl } from './data'; import { ShortUrlsListParams } from './reducers/shortUrlsListParams'; import './ShortUrlsList.scss'; export const SORTABLE_FIELDS = { dateCreated: 'Created at', shortCode: 'Short URL', longUrl: 'Long URL', visits: 'Visits', }; type OrderableFields = keyof typeof SORTABLE_FIELDS; interface RouteParams { page: string; serverId: string; } export interface WithList { shortUrlsList: ShortUrl[]; } export interface ShortUrlsListProps extends ShortUrlsListState, RouteComponentProps { selectedServer: SelectedServer; listShortUrls: (params: ShortUrlsListParams) => void; shortUrlsListParams: ShortUrlsListParams; resetShortUrlParams: () => void; } const ShortUrlsList = (ShortUrlsRow: FC) => boundToMercureHub(({ listShortUrls, resetShortUrlParams, shortUrlsListParams, match, location, loading, error, shortUrlsList, selectedServer, }: ShortUrlsListProps & WithList) => { const { orderBy } = shortUrlsListParams; const [ order, setOrder ] = useState<{ orderField?: OrderableFields; orderDir?: OrderDir }>({ orderField: orderBy && (head(keys(orderBy)) as OrderableFields), orderDir: orderBy && head(values(orderBy)), }); const refreshList = (extraParams: ShortUrlsListParams) => listShortUrls({ ...shortUrlsListParams, ...extraParams }); const handleOrderBy = (orderField?: OrderableFields, orderDir?: OrderDir) => { setOrder({ orderField, orderDir }); refreshList({ orderBy: orderField ? { [orderField]: orderDir } : undefined }); }; const orderByColumn = (field: OrderableFields) => () => handleOrderBy(field, determineOrderDir(field, order.orderField, order.orderDir)); const renderOrderIcon = (field: OrderableFields) => { if (order.orderField !== field) { return null; } if (!order.orderDir) { return null; } return ( ); }; const renderShortUrls = () => { if (error) { return ( Something went wrong while loading short URLs :( ); } if (loading) { return Loading...; } if (!loading && isEmpty(shortUrlsList)) { return No results found; } return shortUrlsList.map((shortUrl) => ( )); }; useEffect(() => { const query = qs.parse(location.search, { ignoreQueryPrefix: true }); const tags = query.tag ? [ query.tag as string ] : shortUrlsListParams.tags; refreshList({ page: match.params.page, tags }); return resetShortUrlParams; }, []); return (
{renderShortUrls()}
{renderOrderIcon('dateCreated')} Created at {renderOrderIcon('shortCode')} Short URL {renderOrderIcon('longUrl')} Long URL Tags {renderOrderIcon('visits')} Visits  
); }, () => 'https://shlink.io/new-visit'); export default ShortUrlsList;