mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 17:57:26 +03:00
Created common component for visits header
This commit is contained in:
parent
f856bc218a
commit
7a94b1730d
8 changed files with 141 additions and 86 deletions
|
@ -5,7 +5,7 @@ import { MercureInfoType } from '../mercure/reducers/mercureInfo';
|
||||||
import { bindToMercureTopic } from '../mercure/helpers';
|
import { bindToMercureTopic } from '../mercure/helpers';
|
||||||
import { SettingsType } from '../settings/reducers/settings';
|
import { SettingsType } from '../settings/reducers/settings';
|
||||||
import { shortUrlVisitsType } from './reducers/shortUrlVisits';
|
import { shortUrlVisitsType } from './reducers/shortUrlVisits';
|
||||||
import VisitsHeader from './VisitsHeader';
|
import ShortUrlVisitsHeader from './ShortUrlVisitsHeader';
|
||||||
import { shortUrlDetailType } from './reducers/shortUrlDetail';
|
import { shortUrlDetailType } from './reducers/shortUrlDetail';
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
|
@ -67,7 +67,7 @@ const ShortUrlVisits = (VisitsStats) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<VisitsStats getVisits={loadVisits} cancelGetVisits={cancelGetShortUrlVisits} visitsInfo={shortUrlVisits}>
|
<VisitsStats getVisits={loadVisits} cancelGetVisits={cancelGetShortUrlVisits} visitsInfo={shortUrlVisits}>
|
||||||
<VisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={history.goBack} />
|
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={history.goBack} />
|
||||||
</VisitsStats>
|
</VisitsStats>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
52
src/visits/ShortUrlVisitsHeader.js
Normal file
52
src/visits/ShortUrlVisitsHeader.js
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import { UncontrolledTooltip } from 'reactstrap';
|
||||||
|
import Moment from 'react-moment';
|
||||||
|
import React from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { ExternalLink } from 'react-external-link';
|
||||||
|
import { shortUrlDetailType } from './reducers/shortUrlDetail';
|
||||||
|
import { shortUrlVisitsType } from './reducers/shortUrlVisits';
|
||||||
|
import VisitsHeader from './VisitsHeader';
|
||||||
|
import './ShortUrlVisitsHeader.scss';
|
||||||
|
|
||||||
|
const propTypes = {
|
||||||
|
shortUrlDetail: shortUrlDetailType.isRequired,
|
||||||
|
shortUrlVisits: shortUrlVisitsType.isRequired,
|
||||||
|
goBack: PropTypes.func.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function ShortUrlVisitsHeader({ shortUrlDetail, shortUrlVisits, goBack }) {
|
||||||
|
const { shortUrl, loading } = shortUrlDetail;
|
||||||
|
const { visits } = shortUrlVisits;
|
||||||
|
const shortLink = shortUrl && shortUrl.shortUrl ? shortUrl.shortUrl : '';
|
||||||
|
const longLink = shortUrl && shortUrl.longUrl ? shortUrl.longUrl : '';
|
||||||
|
|
||||||
|
const renderDate = () => (
|
||||||
|
<span>
|
||||||
|
<b id="created" className="short-url-visits-header__created-at">
|
||||||
|
<Moment fromNow>{shortUrl.dateCreated}</Moment>
|
||||||
|
</b>
|
||||||
|
<UncontrolledTooltip placement="bottom" target="created">
|
||||||
|
<Moment format="YYYY-MM-DD HH:mm">{shortUrl.dateCreated}</Moment>
|
||||||
|
</UncontrolledTooltip>
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
const visitsStatsTitle = (
|
||||||
|
<React.Fragment>
|
||||||
|
Visits for <ExternalLink href={shortLink} />
|
||||||
|
</React.Fragment>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<VisitsHeader title={visitsStatsTitle} goBack={goBack} visits={visits} shortUrl={shortUrl}>
|
||||||
|
<hr />
|
||||||
|
<div>Created: {renderDate()}</div>
|
||||||
|
<div>
|
||||||
|
Long URL:{' '}
|
||||||
|
{loading && <small>Loading...</small>}
|
||||||
|
{!loading && <ExternalLink href={longLink} />}
|
||||||
|
</div>
|
||||||
|
</VisitsHeader>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ShortUrlVisitsHeader.propTypes = propTypes;
|
3
src/visits/ShortUrlVisitsHeader.scss
Normal file
3
src/visits/ShortUrlVisitsHeader.scss
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.short-url-visits-header__created-at {
|
||||||
|
cursor: default;
|
||||||
|
}
|
|
@ -1,67 +1,41 @@
|
||||||
import { Button, Card, UncontrolledTooltip } from 'reactstrap';
|
import { Button, Card } from 'reactstrap';
|
||||||
import Moment from 'react-moment';
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { ExternalLink } from 'react-external-link';
|
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons';
|
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons';
|
||||||
import ShortUrlVisitsCount from '../short-urls/helpers/ShortUrlVisitsCount';
|
import ShortUrlVisitsCount from '../short-urls/helpers/ShortUrlVisitsCount';
|
||||||
import { shortUrlDetailType } from './reducers/shortUrlDetail';
|
import { shortUrlType } from '../short-urls/reducers/shortUrlsList';
|
||||||
import { shortUrlVisitsType } from './reducers/shortUrlVisits';
|
import { VisitType } from './types';
|
||||||
import './VisitsHeader.scss';
|
|
||||||
|
|
||||||
const propTypes = {
|
const propTypes = {
|
||||||
shortUrlDetail: shortUrlDetailType.isRequired,
|
visits: PropTypes.arrayOf(VisitType).isRequired,
|
||||||
shortUrlVisits: shortUrlVisitsType.isRequired,
|
|
||||||
goBack: PropTypes.func.isRequired,
|
goBack: PropTypes.func.isRequired,
|
||||||
|
title: PropTypes.node.isRequired,
|
||||||
|
children: PropTypes.node,
|
||||||
|
shortUrl: shortUrlType,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function VisitsHeader({ shortUrlDetail, shortUrlVisits, goBack }) {
|
const VisitsHeader = ({ visits, goBack, shortUrl, children, title }) => (
|
||||||
const { shortUrl, loading } = shortUrlDetail;
|
<header>
|
||||||
const { visits } = shortUrlVisits;
|
<Card className="bg-light" body>
|
||||||
const shortLink = shortUrl && shortUrl.shortUrl ? shortUrl.shortUrl : '';
|
<h2 className="d-flex justify-content-between align-items-center">
|
||||||
const longLink = shortUrl && shortUrl.longUrl ? shortUrl.longUrl : '';
|
<Button color="link" size="lg" className="p-0 mr-3" onClick={goBack}>
|
||||||
|
<FontAwesomeIcon icon={faArrowLeft} />
|
||||||
const renderDate = () => (
|
</Button>
|
||||||
<span>
|
<span className="text-center d-none d-sm-block">
|
||||||
<b id="created" className="visits-header__created-at"><Moment fromNow>{shortUrl.dateCreated}</Moment></b>
|
{title}
|
||||||
<UncontrolledTooltip placement="bottom" target="created">
|
</span>
|
||||||
<Moment format="YYYY-MM-DD HH:mm">{shortUrl.dateCreated}</Moment>
|
<span className="badge badge-main ml-3">
|
||||||
</UncontrolledTooltip>
|
Visits:{' '}
|
||||||
</span>
|
<ShortUrlVisitsCount visitsCount={visits.length} shortUrl={shortUrl} />
|
||||||
);
|
</span>
|
||||||
const visitsStatsTitle = (
|
</h2>
|
||||||
<React.Fragment>
|
<h3 className="text-center d-block d-sm-none mb-0">{title}</h3>
|
||||||
Visit stats for <ExternalLink href={shortLink} />
|
{children}
|
||||||
</React.Fragment>
|
</Card>
|
||||||
);
|
</header>
|
||||||
|
);
|
||||||
return (
|
|
||||||
<header>
|
|
||||||
<Card className="bg-light" body>
|
|
||||||
<h2 className="d-flex justify-content-between align-items-center">
|
|
||||||
<Button color="link" size="lg" className="p-0 mr-3" onClick={goBack}>
|
|
||||||
<FontAwesomeIcon icon={faArrowLeft} />
|
|
||||||
</Button>
|
|
||||||
<span className="text-center d-none d-sm-block">
|
|
||||||
{visitsStatsTitle}
|
|
||||||
</span>
|
|
||||||
<span className="badge badge-main ml-3">
|
|
||||||
Visits:{' '}
|
|
||||||
<ShortUrlVisitsCount visitsCount={visits.length} shortUrl={shortUrl} />
|
|
||||||
</span>
|
|
||||||
</h2>
|
|
||||||
<h3 className="text-center d-block d-sm-none mb-0">{visitsStatsTitle}</h3>
|
|
||||||
<hr />
|
|
||||||
<div>Created: {renderDate()}</div>
|
|
||||||
<div>
|
|
||||||
Long URL:{' '}
|
|
||||||
{loading && <small>Loading...</small>}
|
|
||||||
{!loading && <ExternalLink href={longLink} />}
|
|
||||||
</div>
|
|
||||||
</Card>
|
|
||||||
</header>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
VisitsHeader.propTypes = propTypes;
|
VisitsHeader.propTypes = propTypes;
|
||||||
|
|
||||||
|
export default VisitsHeader;
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
.visits-header__created-at {
|
|
||||||
cursor: default;
|
|
||||||
}
|
|
|
@ -2,7 +2,7 @@ import React from 'react';
|
||||||
import { shallow } from 'enzyme';
|
import { shallow } from 'enzyme';
|
||||||
import { identity } from 'ramda';
|
import { identity } from 'ramda';
|
||||||
import createShortUrlVisits from '../../src/visits/ShortUrlVisits';
|
import createShortUrlVisits from '../../src/visits/ShortUrlVisits';
|
||||||
import VisitsHeader from '../../src/visits/VisitsHeader';
|
import ShortUrlVisitsHeader from '../../src/visits/ShortUrlVisitsHeader';
|
||||||
|
|
||||||
describe('<ShortUrlVisits />', () => {
|
describe('<ShortUrlVisits />', () => {
|
||||||
let wrapper;
|
let wrapper;
|
||||||
|
@ -41,7 +41,7 @@ describe('<ShortUrlVisits />', () => {
|
||||||
|
|
||||||
it('renders visit stats and visits header', () => {
|
it('renders visit stats and visits header', () => {
|
||||||
const visitStats = wrapper.find(VisitsStats);
|
const visitStats = wrapper.find(VisitsStats);
|
||||||
const visitHeader = wrapper.find(VisitsHeader);
|
const visitHeader = wrapper.find(ShortUrlVisitsHeader);
|
||||||
|
|
||||||
expect(visitStats).toHaveLength(1);
|
expect(visitStats).toHaveLength(1);
|
||||||
expect(visitHeader).toHaveLength(1);
|
expect(visitHeader).toHaveLength(1);
|
||||||
|
|
40
test/visits/ShortUrlVisitsHeader.test.js
Normal file
40
test/visits/ShortUrlVisitsHeader.test.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import React from 'react';
|
||||||
|
import { shallow } from 'enzyme';
|
||||||
|
import Moment from 'react-moment';
|
||||||
|
import { ExternalLink } from 'react-external-link';
|
||||||
|
import ShortUrlVisitsHeader from '../../src/visits/ShortUrlVisitsHeader';
|
||||||
|
|
||||||
|
describe('<ShortUrlVisitsHeader />', () => {
|
||||||
|
let wrapper;
|
||||||
|
const shortUrlDetail = {
|
||||||
|
shortUrl: {
|
||||||
|
shortUrl: 'https://doma.in/abc123',
|
||||||
|
longUrl: 'https://foo.bar/bar/foo',
|
||||||
|
dateCreated: '2018-01-01T10:00:00+01:00',
|
||||||
|
},
|
||||||
|
loading: false,
|
||||||
|
};
|
||||||
|
const shortUrlVisits = {
|
||||||
|
visits: [{}, {}, {}],
|
||||||
|
};
|
||||||
|
const goBack = jest.fn();
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
wrapper = shallow(
|
||||||
|
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />
|
||||||
|
);
|
||||||
|
});
|
||||||
|
afterEach(() => wrapper.unmount());
|
||||||
|
|
||||||
|
it('shows when the URL was created', () => {
|
||||||
|
const moment = wrapper.find(Moment).first();
|
||||||
|
|
||||||
|
expect(moment.prop('children')).toEqual(shortUrlDetail.shortUrl.dateCreated);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows the long URL', () => {
|
||||||
|
const longUrlLink = wrapper.find(ExternalLink).last();
|
||||||
|
|
||||||
|
expect(longUrlLink.prop('href')).toEqual(shortUrlDetail.shortUrl.longUrl);
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,46 +1,35 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { shallow } from 'enzyme';
|
import { shallow } from 'enzyme';
|
||||||
import Moment from 'react-moment';
|
|
||||||
import { ExternalLink } from 'react-external-link';
|
|
||||||
import VisitsHeader from '../../src/visits/VisitsHeader';
|
import VisitsHeader from '../../src/visits/VisitsHeader';
|
||||||
|
|
||||||
describe('<VisitsHeader />', () => {
|
describe('<VisitsHeader />', () => {
|
||||||
let wrapper;
|
let wrapper;
|
||||||
const shortUrlDetail = {
|
const visits = [{}, {}, {}];
|
||||||
shortUrl: {
|
const title = 'My header title';
|
||||||
shortUrl: 'https://doma.in/abc123',
|
|
||||||
longUrl: 'https://foo.bar/bar/foo',
|
|
||||||
dateCreated: '2018-01-01T10:00:00+01:00',
|
|
||||||
},
|
|
||||||
loading: false,
|
|
||||||
};
|
|
||||||
const shortUrlVisits = {
|
|
||||||
visits: [{}, {}, {}],
|
|
||||||
};
|
|
||||||
const goBack = jest.fn();
|
const goBack = jest.fn();
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
wrapper = shallow(<VisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />);
|
wrapper = shallow(
|
||||||
|
<VisitsHeader visits={visits} goBack={goBack} title={title} />
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => wrapper.unmount());
|
afterEach(() => wrapper.unmount());
|
||||||
|
afterEach(jest.resetAllMocks);
|
||||||
|
|
||||||
it('shows the amount of visits', () => {
|
it('shows the amount of visits', () => {
|
||||||
const visitsBadge = wrapper.find('.badge');
|
const visitsBadge = wrapper.find('.badge');
|
||||||
|
|
||||||
expect(visitsBadge.html()).toContain(
|
expect(visitsBadge.html()).toContain(
|
||||||
`Visits: <span><strong class="short-url-visits-count__amount">${shortUrlVisits.visits.length}</strong></span>`
|
`Visits: <span><strong class="short-url-visits-count__amount">${visits.length}</strong></span>`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shows when the URL was created', () => {
|
it('shows the title in two places', () => {
|
||||||
const moment = wrapper.find(Moment).first();
|
const titles = wrapper.find('.text-center');
|
||||||
|
|
||||||
expect(moment.prop('children')).toEqual(shortUrlDetail.shortUrl.dateCreated);
|
expect(titles).toHaveLength(2);
|
||||||
});
|
expect(titles.at(0).html()).toContain(title);
|
||||||
|
expect(titles.at(1).html()).toContain(title);
|
||||||
it('shows the long URL', () => {
|
|
||||||
const longUrlLink = wrapper.find(ExternalLink).last();
|
|
||||||
|
|
||||||
expect(longUrlLink.prop('href')).toEqual(shortUrlDetail.shortUrl.longUrl);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue