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 } from 'ramda'; import React from 'react'; import { connect } from 'react-redux'; import { ShortUrlsRow } from './helpers/ShortUrlsRow'; import { listShortUrls } from './reducers/shortUrlsList'; import './ShortUrlsList.scss'; export class ShortUrlsList extends React.Component { refreshList = extraParams => { const { listShortUrls, shortUrlsListParams } = this.props; listShortUrls({ ...shortUrlsListParams, ...extraParams }); }; 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 }); } render() { const determineOrderDir = field => { if (this.state.orderField !== field) { return 'ASC'; } const newOrderMap = { 'ASC': 'DESC', 'DESC': undefined, }; return this.state.orderDir ? newOrderMap[this.state.orderDir] : 'ASC'; } const orderBy = field => { const newOrderDir = determineOrderDir(field); this.setState({ orderField: field, orderDir: newOrderDir }); this.refreshList({ orderBy: { [field]: newOrderDir } }) }; const renderOrderIcon = field => { if (this.state.orderField !== field || this.state.orderDir === undefined) { return null; } return ( ); }; return ( {this.renderShortUrls()}
orderBy('dateCreated')} > {renderOrderIcon('dateCreated')} Created at orderBy('shortCode')} > {renderOrderIcon('shortCode')} Short URL orderBy('originalUrl')} > {renderOrderIcon('originalUrl')} Long URL Tags orderBy('visits')} > {renderOrderIcon('visits')} Visits  
); } 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 => ( )); } } export default connect(pick(['selectedServer', 'shortUrlsListParams']), { listShortUrls })(ShortUrlsList);