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 from 'react'; import qs from 'qs'; import PropTypes from 'prop-types'; import { serverType } from '../servers/prop-types'; import SortingDropdown from '../utils/SortingDropdown'; import { determineOrderDir } from '../utils/utils'; import { shortUrlType } from './reducers/shortUrlsList'; import { shortUrlsListParamsType } from './reducers/shortUrlsListParams'; import './ShortUrlsList.scss'; const SORTABLE_FIELDS = { dateCreated: 'Created at', shortCode: 'Short URL', longUrl: 'Long URL', visits: 'Visits', }; const ShortUrlsList = (ShortUrlsRow) => class ShortUrlsList extends React.Component { static propTypes = { listShortUrls: PropTypes.func, resetShortUrlParams: PropTypes.func, shortUrlsListParams: shortUrlsListParamsType, match: PropTypes.object, location: PropTypes.object, loading: PropTypes.bool, error: PropTypes.bool, shortUrlsList: PropTypes.arrayOf(shortUrlType), selectedServer: serverType, }; refreshList = (extraParams) => { const { listShortUrls, shortUrlsListParams } = this.props; listShortUrls({ ...shortUrlsListParams, ...extraParams, }); }; handleOrderBy = (orderField, orderDir) => { this.setState({ orderField, orderDir }); this.refreshList({ orderBy: { [orderField]: orderDir } }); }; orderByColumn = (columnName) => () => this.handleOrderBy(columnName, determineOrderDir(columnName, this.state.orderField, this.state.orderDir)); renderOrderIcon = (field) => { if (this.state.orderField !== field) { return null; } return ( ); }; constructor(props) { super(props); const { orderBy } = props.shortUrlsListParams; this.state = { orderField: orderBy ? head(keys(orderBy)) : undefined, orderDir: orderBy ? head(values(orderBy)) : undefined, }; } componentDidMount() { const { match: { params }, location, shortUrlsListParams } = this.props; const query = qs.parse(location.search, { ignoreQueryPrefix: true }); this.refreshList({ page: params.page, tags: query.tag ? [ query.tag ] : shortUrlsListParams.tags }); } componentWillUnmount() { const { resetShortUrlParams } = this.props; resetShortUrlParams(); } renderShortUrls() { const { shortUrlsList, selectedServer, loading, error, shortUrlsListParams } = this.props; 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) => ( )); } render() { return (
{this.renderShortUrls()}
{this.renderOrderIcon('dateCreated')} Created at {this.renderOrderIcon('shortCode')} Short URL {this.renderOrderIcon('longUrl')} Long URL Tags {this.renderOrderIcon('visits')} Visits  
); } }; export default ShortUrlsList;