2018-07-28 10:15:41 +03:00
|
|
|
import caretDownIcon from '@fortawesome/fontawesome-free-solid/faCaretDown';
|
|
|
|
import caretUpIcon from '@fortawesome/fontawesome-free-solid/faCaretUp';
|
|
|
|
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
|
|
|
|
import { isEmpty } from 'ramda';
|
2018-06-15 22:49:25 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2018-06-16 12:24:42 +03:00
|
|
|
import Tag from '../utils/Tag';
|
2018-07-28 10:20:51 +03:00
|
|
|
import { ShortUrlsRow } from './helpers/ShortUrlsRow';
|
2018-06-15 22:49:25 +03:00
|
|
|
import { listShortUrls } from './reducers/shortUrlsList';
|
2018-06-17 11:59:04 +03:00
|
|
|
import './ShortUrlsList.scss';
|
2018-06-15 22:49:25 +03:00
|
|
|
|
|
|
|
export class ShortUrlsList extends React.Component {
|
2018-07-22 10:37:57 +03:00
|
|
|
refreshList = extraParams => {
|
|
|
|
const { listShortUrls, shortUrlsListParams, match: { params } } = this.props;
|
|
|
|
listShortUrls(params.serverId, {
|
|
|
|
...shortUrlsListParams,
|
|
|
|
...extraParams
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
const orderBy = props.shortUrlsListParams.orderBy;
|
|
|
|
this.state = {
|
|
|
|
orderField: orderBy ? Object.keys(orderBy)[0] : 'dateCreated',
|
|
|
|
orderDir: orderBy ? Object.values(orderBy)[0] : 'ASC',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 22:49:25 +03:00
|
|
|
componentDidMount() {
|
2018-07-18 21:01:56 +03:00
|
|
|
const { match: { params } } = this.props;
|
2018-07-22 10:37:57 +03:00
|
|
|
this.refreshList({ page: params.page });
|
2018-06-15 22:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-07-22 10:37:57 +03:00
|
|
|
const orderBy = field => {
|
|
|
|
const newOrderDir = this.state.orderField !== field ? 'ASC' : (this.state.orderDir === 'DESC' ? 'ASC' : 'DESC');
|
|
|
|
this.setState({ orderField: field, orderDir: newOrderDir });
|
|
|
|
this.refreshList({ orderBy: { [field]: newOrderDir } })
|
|
|
|
};
|
|
|
|
const renderOrderIcon = field => {
|
|
|
|
if (this.state.orderField !== field) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={this.state.orderDir === 'ASC' ? caretUpIcon : caretDownIcon}
|
|
|
|
className="short-urls-list__header-icon"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-06-15 22:49:25 +03:00
|
|
|
return (
|
2018-06-17 11:59:04 +03:00
|
|
|
<table className="table table-striped table-hover">
|
2018-06-16 12:24:42 +03:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2018-07-22 10:37:57 +03:00
|
|
|
<th
|
|
|
|
className="short-urls-list__header short-urls-list__header--with-action"
|
|
|
|
onClick={() => orderBy('dateCreated')}
|
|
|
|
>
|
|
|
|
{renderOrderIcon('dateCreated')}
|
|
|
|
Created at
|
|
|
|
</th>
|
|
|
|
<th
|
|
|
|
className="short-urls-list__header short-urls-list__header--with-action"
|
|
|
|
onClick={() => orderBy('shortCode')}
|
|
|
|
>
|
|
|
|
{renderOrderIcon('shortCode')}
|
|
|
|
Short URL
|
|
|
|
</th>
|
|
|
|
<th
|
|
|
|
className="short-urls-list__header short-urls-list__header--with-action"
|
|
|
|
onClick={() => orderBy('originalUrl')}
|
|
|
|
>
|
|
|
|
{renderOrderIcon('originalUrl')}
|
|
|
|
Original URL
|
|
|
|
</th>
|
|
|
|
<th className="short-urls-list__header">Tags</th>
|
|
|
|
<th
|
|
|
|
className="short-urls-list__header short-urls-list__header--with-action"
|
|
|
|
onClick={() => orderBy('visits')}
|
|
|
|
>
|
|
|
|
<span className="nowrap">{renderOrderIcon('visits')} Visits</span>
|
|
|
|
</th>
|
|
|
|
<th className="short-urls-list__header"> </th>
|
2018-06-16 12:24:42 +03:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{this.renderShortUrls()}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2018-06-15 22:49:25 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderShortUrls() {
|
2018-07-24 20:17:01 +03:00
|
|
|
const { shortUrlsList, selectedServer, loading, error } = this.props;
|
|
|
|
if (error) {
|
|
|
|
return <tr><td colSpan="6" className="text-center table-danger">Something went wrong while loading short URLs :(</td></tr>;
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:39:05 +03:00
|
|
|
if (loading) {
|
2018-06-17 11:59:04 +03:00
|
|
|
return <tr><td colSpan="6" className="text-center">Loading...</td></tr>;
|
2018-06-15 22:49:25 +03:00
|
|
|
}
|
|
|
|
|
2018-07-15 11:39:05 +03:00
|
|
|
if (! loading && isEmpty(shortUrlsList)) {
|
|
|
|
return <tr><td colSpan="6" className="text-center">No results found</td></tr>;
|
|
|
|
}
|
|
|
|
|
2018-06-15 22:49:25 +03:00
|
|
|
return shortUrlsList.map(shortUrl => (
|
2018-07-28 10:20:51 +03:00
|
|
|
<ShortUrlsRow shortUrl={shortUrl} selectedServer={selectedServer} key={shortUrl.shortCode} />
|
2018-06-17 11:59:04 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
static renderTags(tags) {
|
|
|
|
if (isEmpty(tags)) {
|
|
|
|
return <i className="nowrap"><small>No tags</small></i>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags.map(tag => <Tag text={tag} />);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 22:49:25 +03:00
|
|
|
export default connect(state => ({
|
2018-06-16 12:24:42 +03:00
|
|
|
selectedServer: state.selectedServer,
|
2018-06-17 18:12:16 +03:00
|
|
|
shortUrlsListParams: state.shortUrlsListParams,
|
2018-06-15 22:49:25 +03:00
|
|
|
}), { listShortUrls })(ShortUrlsList);
|