2018-06-15 22:49:25 +03:00
|
|
|
import React from 'react';
|
2018-07-21 13:15:53 +03:00
|
|
|
import { CopyToClipboard } from 'react-copy-to-clipboard';
|
2018-06-16 12:24:42 +03:00
|
|
|
import Moment from 'react-moment';
|
2018-06-15 22:49:25 +03:00
|
|
|
import { connect } from 'react-redux';
|
2018-06-17 11:59:04 +03:00
|
|
|
import { ButtonDropdown, DropdownItem, DropdownMenu, DropdownToggle } from 'reactstrap';
|
|
|
|
import { isEmpty } from 'ramda';
|
2018-07-22 10:37:57 +03:00
|
|
|
import caretUpIcon from '@fortawesome/fontawesome-free-solid/faCaretUp';
|
|
|
|
import caretDownIcon from '@fortawesome/fontawesome-free-solid/faCaretDown';
|
2018-06-17 11:59:04 +03:00
|
|
|
import pieChartIcon from '@fortawesome/fontawesome-free-solid/faChartPie';
|
2018-07-22 09:36:46 +03:00
|
|
|
import pictureIcon from '@fortawesome/fontawesome-free-regular/faImage';
|
2018-06-17 11:59:04 +03:00
|
|
|
import qrIcon from '@fortawesome/fontawesome-free-solid/faQrcode';
|
2018-07-22 09:36:46 +03:00
|
|
|
import copyIcon from '@fortawesome/fontawesome-free-regular/faCopy';
|
2018-06-17 11:59:04 +03:00
|
|
|
import menuIcon from '@fortawesome/fontawesome-free-solid/faEllipsisV';
|
|
|
|
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
|
2018-06-16 12:24:42 +03:00
|
|
|
import Tag from '../utils/Tag';
|
2018-07-27 19:21:10 +03:00
|
|
|
import PreviewModal from './helpers/PreviewModal';
|
2018-07-27 19:05:09 +03:00
|
|
|
import QrCodeModal from './helpers/QrCodeModal';
|
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-06-17 11:59:04 +03:00
|
|
|
<Row shortUrl={shortUrl} selectedServer={selectedServer} key={shortUrl.shortCode} />
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
static renderTags(tags) {
|
|
|
|
if (isEmpty(tags)) {
|
|
|
|
return <i className="nowrap"><small>No tags</small></i>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags.map(tag => <Tag text={tag} />);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Row extends React.Component {
|
2018-07-21 13:15:53 +03:00
|
|
|
state = { displayMenu: false, copiedToClipboard: false };
|
2018-06-17 11:59:04 +03:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { shortUrl, selectedServer } = this.props;
|
2018-07-21 13:15:53 +03:00
|
|
|
const completeShortUrl = !selectedServer ? shortUrl.shortCode : `${selectedServer.url}/${shortUrl.shortCode}`;
|
2018-06-17 11:59:04 +03:00
|
|
|
|
|
|
|
return (
|
2018-07-15 11:39:05 +03:00
|
|
|
<tr
|
|
|
|
onMouseEnter={() => this.setState({ displayMenu: true })}
|
|
|
|
onMouseLeave={() => this.setState({ displayMenu: false })}
|
2018-06-17 11:59:04 +03:00
|
|
|
>
|
|
|
|
<td className="nowrap short-urls-list__cell">
|
|
|
|
<Moment format="YYYY-MM-DD HH:mm" interval={0}>{shortUrl.dateCreated}</Moment>
|
|
|
|
</td>
|
|
|
|
<td className="short-urls-list__cell">
|
2018-07-21 13:15:53 +03:00
|
|
|
<a href={completeShortUrl} target="_blank">{completeShortUrl}</a>
|
2018-06-16 12:24:42 +03:00
|
|
|
</td>
|
2018-07-21 13:15:53 +03:00
|
|
|
<td className="short-urls-list__cell short-urls-list__cell--relative">
|
2018-06-16 12:24:42 +03:00
|
|
|
<a href={shortUrl.originalUrl} target="_blank">{shortUrl.originalUrl}</a>
|
2018-07-21 13:15:53 +03:00
|
|
|
<small
|
|
|
|
className="badge badge-warning short-urls-list__copy-hint"
|
|
|
|
hidden={!this.state.copiedToClipboard}
|
|
|
|
>
|
|
|
|
Copied short URL!
|
|
|
|
</small>
|
2018-06-16 12:24:42 +03:00
|
|
|
</td>
|
2018-06-17 11:59:04 +03:00
|
|
|
<td className="short-urls-list__cell">{ShortUrlsList.renderTags(shortUrl.tags)}</td>
|
|
|
|
<td className="short-urls-list__cell text-right">{shortUrl.visitsCount}</td>
|
|
|
|
<td className="short-urls-list__cell">
|
2018-07-21 13:15:53 +03:00
|
|
|
<RowMenu
|
|
|
|
display={this.state.displayMenu}
|
|
|
|
shortUrl={completeShortUrl}
|
|
|
|
onCopyToClipboard={() => {
|
|
|
|
this.setState({ copiedToClipboard: true });
|
|
|
|
setTimeout(() => this.setState({ copiedToClipboard: false }), 2000);
|
|
|
|
}}
|
|
|
|
/>
|
2018-06-17 11:59:04 +03:00
|
|
|
</td>
|
2018-06-16 12:24:42 +03:00
|
|
|
</tr>
|
2018-06-17 11:59:04 +03:00
|
|
|
)
|
2018-06-15 22:49:25 +03:00
|
|
|
}
|
2018-06-17 11:59:04 +03:00
|
|
|
}
|
2018-06-16 12:24:42 +03:00
|
|
|
|
2018-06-17 11:59:04 +03:00
|
|
|
class RowMenu extends React.Component {
|
2018-07-27 19:21:10 +03:00
|
|
|
state = { isOpen: false, isQrModalOpen: false, isPreviewOpen: false };
|
2018-06-17 11:59:04 +03:00
|
|
|
toggle = () => this.setState({ isOpen: ! this.state.isOpen });
|
2018-06-16 12:24:42 +03:00
|
|
|
|
2018-06-17 11:59:04 +03:00
|
|
|
render () {
|
2018-07-21 19:48:33 +03:00
|
|
|
const { display, shortUrl, onCopyToClipboard } = this.props;
|
|
|
|
const baseClass = 'short-urls-list__dropdown-toggle';
|
|
|
|
const toggleClass = ! display ? `${baseClass} short-urls-list__dropdown-toggle--hidden` : baseClass;
|
2018-07-27 19:05:09 +03:00
|
|
|
const toggleQrCode = () => this.setState({ isQrModalOpen: !this.state.isQrModalOpen });
|
2018-07-27 19:21:10 +03:00
|
|
|
const togglePreview = () => this.setState({ isPreviewOpen: !this.state.isPreviewOpen });
|
2018-06-17 11:59:04 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ButtonDropdown toggle={this.toggle} isOpen={this.state.isOpen} direction="left">
|
2018-07-21 19:48:33 +03:00
|
|
|
<DropdownToggle color="white" size="sm" caret className={toggleClass}>
|
2018-06-17 11:59:04 +03:00
|
|
|
<FontAwesomeIcon icon={menuIcon} />
|
|
|
|
</DropdownToggle>
|
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownItem>
|
|
|
|
<FontAwesomeIcon icon={pieChartIcon} /> Visit Stats
|
|
|
|
</DropdownItem>
|
2018-07-27 19:21:10 +03:00
|
|
|
|
2018-06-17 11:59:04 +03:00
|
|
|
<DropdownItem divider />
|
2018-07-27 19:21:10 +03:00
|
|
|
|
|
|
|
<DropdownItem onClick={togglePreview}>
|
2018-06-17 11:59:04 +03:00
|
|
|
<FontAwesomeIcon icon={pictureIcon} /> Preview
|
|
|
|
</DropdownItem>
|
2018-07-27 19:21:10 +03:00
|
|
|
<PreviewModal
|
|
|
|
url={shortUrl}
|
|
|
|
isOpen={this.state.isPreviewOpen}
|
|
|
|
toggle={togglePreview}
|
|
|
|
/>
|
|
|
|
|
2018-07-27 19:05:09 +03:00
|
|
|
<DropdownItem onClick={toggleQrCode}>
|
2018-06-17 11:59:04 +03:00
|
|
|
<FontAwesomeIcon icon={qrIcon} /> QR code
|
|
|
|
</DropdownItem>
|
2018-07-27 19:05:09 +03:00
|
|
|
<QrCodeModal
|
|
|
|
url={shortUrl}
|
|
|
|
isOpen={this.state.isQrModalOpen}
|
|
|
|
toggle={toggleQrCode}
|
|
|
|
/>
|
2018-07-27 19:21:10 +03:00
|
|
|
|
2018-06-17 11:59:04 +03:00
|
|
|
<DropdownItem divider />
|
2018-07-27 19:21:10 +03:00
|
|
|
|
2018-07-21 19:48:33 +03:00
|
|
|
<CopyToClipboard text={shortUrl} onCopy={onCopyToClipboard}>
|
|
|
|
<DropdownItem>
|
|
|
|
<FontAwesomeIcon icon={copyIcon} /> Copy to clipboard
|
|
|
|
</DropdownItem>
|
|
|
|
</CopyToClipboard>
|
2018-06-17 11:59:04 +03:00
|
|
|
</DropdownMenu>
|
|
|
|
</ButtonDropdown>
|
|
|
|
);
|
2018-06-16 12:24:42 +03:00
|
|
|
}
|
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);
|