Extracted visits count component to reuse it in other places

This commit is contained in:
Alejandro Celaya
2020-01-11 19:58:04 +01:00
parent f2cb30409a
commit 3f2162fe62
6 changed files with 96 additions and 67 deletions

View File

@@ -0,0 +1,39 @@
import React from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
import { UncontrolledTooltip } from 'reactstrap';
import { shortUrlType } from '../reducers/shortUrlsList';
const propTypes = {
shortUrl: shortUrlType,
};
const ShortUrlVisitsCount = ({ shortUrl }) => {
const { visitsCount, meta } = shortUrl;
const maxVisits = meta && meta.maxVisits;
if (!maxVisits) {
return <span>{visitsCount}</span>;
}
return (
<React.Fragment>
<span>
{visitsCount}
<small id="maxVisitsControl" className="short-urls-row__max-visits-control">
{' '}/ {maxVisits}{' '}
<sup>
<FontAwesomeIcon icon={infoIcon} />
</sup>
</small>
</span>
<UncontrolledTooltip target="maxVisitsControl" placement="bottom">
This short URL will not accept more than <b>{maxVisits}</b> visits.
</UncontrolledTooltip>
</React.Fragment>
);
};
ShortUrlVisitsCount.propTypes = propTypes;
export default ShortUrlVisitsCount;

View File

@@ -2,42 +2,14 @@ import { isEmpty } from 'ramda';
import React from 'react';
import Moment from 'react-moment';
import PropTypes from 'prop-types';
import { UncontrolledTooltip } from 'reactstrap';
import { faInfoCircle as infoIcon } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { shortUrlsListParamsType } from '../reducers/shortUrlsListParams';
import { serverType } from '../../servers/prop-types';
import ExternalLink from '../../utils/ExternalLink';
import { shortUrlType } from '../reducers/shortUrlsList';
import Tag from '../../tags/helpers/Tag';
import ShortUrlVisitsCount from './ShortUrlVisitsCount';
import './ShortUrlsRow.scss';
const renderVisitsCount = (shortUrl) => {
const { visitsCount, meta } = shortUrl;
const maxVisits = meta && meta.maxVisits;
if (!maxVisits) {
return <span>{visitsCount}</span>;
}
return (
<React.Fragment>
<span>
{visitsCount}
<small id="maxVisitsControl" className="short-urls-row__max-visits-control">
{' '}/ {maxVisits}{' '}
<sup>
<FontAwesomeIcon icon={infoIcon} />
</sup>
</small>
</span>
<UncontrolledTooltip target="maxVisitsControl" placement="bottom">
This short URL will not accept more than <b>{maxVisits}</b> visits.
</UncontrolledTooltip>
</React.Fragment>
);
};
const ShortUrlsRow = (
ShortUrlsRowMenu,
colorGenerator,
@@ -86,7 +58,7 @@ const ShortUrlsRow = (
</td>
<td className="short-urls-row__cell" data-th="Tags: ">{this.renderTags(shortUrl.tags)}</td>
<td className="short-urls-row__cell text-md-right" data-th="Visits: ">
{renderVisitsCount(shortUrl)}
<ShortUrlVisitsCount shortUrl={shortUrl} />
</td>
<td className="short-urls-row__cell short-urls-row__cell--relative">
<small

View File

@@ -14,6 +14,12 @@ export const shortUrlType = PropTypes.shape({
shortCode: PropTypes.string,
shortUrl: PropTypes.string,
longUrl: PropTypes.string,
visitsCount: PropTypes.number,
meta: PropTypes.shape({
validSince: PropTypes.string,
validUntil: PropTypes.string,
maxVisits: PropTypes.number,
}),
tags: PropTypes.arrayOf(PropTypes.string),
});