import caretDownIcon from '@fortawesome/fontawesome-free-solid/faCaretDown' import caretUpIcon from '@fortawesome/fontawesome-free-solid/faCaretUp' import FontAwesomeIcon from '@fortawesome/react-fontawesome' import { head, isEmpty, pick, toPairs } from 'ramda' import React from 'react' import { connect } from 'react-redux' import { DropdownItem, DropdownMenu, DropdownToggle, UncontrolledDropdown } from 'reactstrap' import { ShortUrlsRow } from './helpers/ShortUrlsRow' import { listShortUrls } from './reducers/shortUrlsList' import './ShortUrlsList.scss' const SORTABLE_FIELDS = { dateCreated: 'Created at', shortCode: 'Short URL', originalUrl: 'Long URL', visits: 'Visits', }; export class ShortUrlsList extends React.Component { refreshList = extraParams => { const { listShortUrls, shortUrlsListParams } = this.props; listShortUrls({ ...shortUrlsListParams, ...extraParams }); }; determineOrderDir = field => { if (this.state.orderField !== field) { return 'ASC'; } const newOrderMap = { 'ASC': 'DESC', 'DESC': undefined, }; return this.state.orderDir ? newOrderMap[this.state.orderDir] : 'ASC'; } orderBy = field => { const newOrderDir = this.determineOrderDir(field); this.setState({ orderField: newOrderDir !== undefined ? field : undefined, orderDir: newOrderDir }); this.refreshList({ orderBy: { [field]: newOrderDir } }) }; renderOrderIcon = (field, className = 'short-urls-list__header-icon') => { if (this.state.orderField !== field) { return null; } return ( ); }; constructor(props) { super(props); const { orderBy } = props.shortUrlsListParams; this.state = { orderField: orderBy ? head(Object.keys(orderBy)) : undefined, orderDir: orderBy ? head(Object.values(orderBy)) : undefined, } } componentDidMount() { const { match: { params } } = this.props; this.refreshList({ page: params.page }); } 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 => ( )); } renderMobileOrderingControls() { return (
Order by {toPairs(SORTABLE_FIELDS).map(([key, value]) => this.orderBy(key)}> {value} {this.renderOrderIcon(key, 'short-urls-list__header-icon--mobile')} )}
); } render() { return ( {this.renderMobileOrderingControls()} {this.renderShortUrls()}
this.orderBy('dateCreated')} > {this.renderOrderIcon('dateCreated')} Created at this.orderBy('shortCode')} > {this.renderOrderIcon('shortCode')} Short URL this.orderBy('originalUrl')} > {this.renderOrderIcon('originalUrl')} Long URL Tags this.orderBy('visits')} > {this.renderOrderIcon('visits')} Visits  
); } } export default connect(pick(['selectedServer', 'shortUrlsListParams']), { listShortUrls })(ShortUrlsList);