Moved mercure hub binding from custom hook to HOC

This commit is contained in:
Alejandro Celaya 2020-09-06 19:41:15 +02:00
parent 536d49aac9
commit 5d6d802d64
11 changed files with 56 additions and 54 deletions

View file

@ -0,0 +1,26 @@
import React, { FC, useEffect } from 'react';
import { CreateVisit } from '../../visits/types';
import { MercureInfo } from '../reducers/mercureInfo';
import { bindToMercureTopic } from './index';
export interface MercureBoundProps {
createNewVisit: (visitData: CreateVisit) => void;
loadMercureInfo: Function;
mercureInfo: MercureInfo;
}
export function boundToMercureHub<T = {}>(
WrappedComponent: FC<MercureBoundProps & T>,
getTopicForProps: (props: T) => string,
) {
return (props: MercureBoundProps & T) => {
const { createNewVisit, loadMercureInfo, mercureInfo } = props;
useEffect(
bindToMercureTopic(mercureInfo, getTopicForProps(props), createNewVisit, loadMercureInfo),
[ mercureInfo ],
);
return <WrappedComponent {...props} />;
};
}

View file

@ -1,4 +1,3 @@
import { useEffect } from 'react';
import { EventSourcePolyfill as EventSource } from 'event-source-polyfill'; import { EventSourcePolyfill as EventSource } from 'event-source-polyfill';
import { MercureInfo } from '../reducers/mercureInfo'; import { MercureInfo } from '../reducers/mercureInfo';
@ -23,18 +22,3 @@ export const bindToMercureTopic = <T>(mercureInfo: MercureInfo, topic: string, o
return () => es.close(); return () => es.close();
}; };
export const useMercureTopicBinding = <T>(
mercureInfo: MercureInfo,
topic: string,
onMessage: (message: T) => void,
onTokenExpired: Function,
) => {
useEffect(bindToMercureTopic(mercureInfo, topic, onMessage, onTokenExpired), [ mercureInfo ]);
};
export interface MercureBoundProps {
createNewVisit: (message: any) => void;
loadMercureInfo: Function;
mercureInfo: MercureInfo;
}

View file

@ -6,8 +6,8 @@ import qs from 'qs';
import { RouteComponentProps } from 'react-router'; import { RouteComponentProps } from 'react-router';
import SortingDropdown from '../utils/SortingDropdown'; import SortingDropdown from '../utils/SortingDropdown';
import { determineOrderDir, OrderDir } from '../utils/utils'; import { determineOrderDir, OrderDir } from '../utils/utils';
import { MercureBoundProps, useMercureTopicBinding } from '../mercure/helpers';
import { SelectedServer } from '../servers/data'; import { SelectedServer } from '../servers/data';
import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList'; import { ShortUrlsList as ShortUrlsListState } from './reducers/shortUrlsList';
import { ShortUrlsRowProps } from './helpers/ShortUrlsRow'; import { ShortUrlsRowProps } from './helpers/ShortUrlsRow';
import { ShortUrl } from './data'; import { ShortUrl } from './data';
@ -31,14 +31,14 @@ export interface WithList {
shortUrlsList: ShortUrl[]; shortUrlsList: ShortUrl[];
} }
export interface ShortUrlsListProps extends ShortUrlsListState, RouteComponentProps<RouteParams>, MercureBoundProps { export interface ShortUrlsListProps extends ShortUrlsListState, RouteComponentProps<RouteParams> {
selectedServer: SelectedServer; selectedServer: SelectedServer;
listShortUrls: (params: ShortUrlsListParams) => void; listShortUrls: (params: ShortUrlsListParams) => void;
shortUrlsListParams: ShortUrlsListParams; shortUrlsListParams: ShortUrlsListParams;
resetShortUrlParams: () => void; resetShortUrlParams: () => void;
} }
const ShortUrlsList = (ShortUrlsRow: FC<ShortUrlsRowProps>) => ({ const ShortUrlsList = (ShortUrlsRow: FC<ShortUrlsRowProps>) => boundToMercureHub(({
listShortUrls, listShortUrls,
resetShortUrlParams, resetShortUrlParams,
shortUrlsListParams, shortUrlsListParams,
@ -48,9 +48,6 @@ const ShortUrlsList = (ShortUrlsRow: FC<ShortUrlsRowProps>) => ({
error, error,
shortUrlsList, shortUrlsList,
selectedServer, selectedServer,
createNewVisit,
loadMercureInfo,
mercureInfo,
}: ShortUrlsListProps & WithList) => { }: ShortUrlsListProps & WithList) => {
const { orderBy } = shortUrlsListParams; const { orderBy } = shortUrlsListParams;
const [ order, setOrder ] = useState<{ orderField?: OrderableFields; orderDir?: OrderDir }>({ const [ order, setOrder ] = useState<{ orderField?: OrderableFields; orderDir?: OrderDir }>({
@ -116,7 +113,6 @@ const ShortUrlsList = (ShortUrlsRow: FC<ShortUrlsRowProps>) => ({
return resetShortUrlParams; return resetShortUrlParams;
}, []); }, []);
useMercureTopicBinding(mercureInfo, 'https://shlink.io/new-visit', createNewVisit, loadMercureInfo);
return ( return (
<React.Fragment> <React.Fragment>
@ -168,6 +164,6 @@ const ShortUrlsList = (ShortUrlsRow: FC<ShortUrlsRowProps>) => ({
</table> </table>
</React.Fragment> </React.Fragment>
); );
}; }, () => 'https://shlink.io/new-visit');
export default ShortUrlsList; export default ShortUrlsList;

View file

@ -2,30 +2,29 @@ import React, { FC, useEffect, useState } from 'react';
import { splitEvery } from 'ramda'; import { splitEvery } from 'ramda';
import Message from '../utils/Message'; import Message from '../utils/Message';
import SearchField from '../utils/SearchField'; import SearchField from '../utils/SearchField';
import { MercureBoundProps, useMercureTopicBinding } from '../mercure/helpers';
import { SelectedServer } from '../servers/data'; import { SelectedServer } from '../servers/data';
import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
import { TagsList as TagsListState } from './reducers/tagsList'; import { TagsList as TagsListState } from './reducers/tagsList';
import { TagCardProps } from './TagCard'; import { TagCardProps } from './TagCard';
const { ceil } = Math; const { ceil } = Math;
const TAGS_GROUPS_AMOUNT = 4; const TAGS_GROUPS_AMOUNT = 4;
export interface TagsListProps extends MercureBoundProps { export interface TagsListProps {
filterTags: (searchTerm: string) => void; filterTags: (searchTerm: string) => void;
forceListTags: Function; forceListTags: Function;
tagsList: TagsListState; tagsList: TagsListState;
selectedServer: SelectedServer; selectedServer: SelectedServer;
} }
const TagsList = (TagCard: FC<TagCardProps>) => ( const TagsList = (TagCard: FC<TagCardProps>) => boundToMercureHub((
{ filterTags, forceListTags, tagsList, selectedServer, createNewVisit, loadMercureInfo, mercureInfo }: TagsListProps, { filterTags, forceListTags, tagsList, selectedServer }: TagsListProps,
) => { ) => {
const [ displayedTag, setDisplayedTag ] = useState<string | undefined>(); const [ displayedTag, setDisplayedTag ] = useState<string | undefined>();
useEffect(() => { useEffect(() => {
forceListTags(); forceListTags();
}, []); }, []);
useMercureTopicBinding(mercureInfo, 'https://shlink.io/new-visit', createNewVisit, loadMercureInfo);
const renderContent = () => { const renderContent = () => {
if (tagsList.loading) { if (tagsList.loading) {
@ -76,6 +75,6 @@ const TagsList = (TagCard: FC<TagCardProps>) => (
</div> </div>
</React.Fragment> </React.Fragment>
); );
}; }, () => 'https://shlink.io/new-visit');
export default TagsList; export default TagsList;

View file

@ -1,14 +1,14 @@
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import qs from 'qs'; import qs from 'qs';
import { RouteComponentProps } from 'react-router'; import { RouteComponentProps } from 'react-router';
import { MercureBoundProps, useMercureTopicBinding } from '../mercure/helpers'; import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
import { ShlinkVisitsParams } from '../utils/services/types'; import { ShlinkVisitsParams } from '../utils/services/types';
import { ShortUrlVisits as ShortUrlVisitsState } from './reducers/shortUrlVisits'; import { ShortUrlVisits as ShortUrlVisitsState } from './reducers/shortUrlVisits';
import ShortUrlVisitsHeader from './ShortUrlVisitsHeader'; import ShortUrlVisitsHeader from './ShortUrlVisitsHeader';
import { ShortUrlDetail } from './reducers/shortUrlDetail'; import { ShortUrlDetail } from './reducers/shortUrlDetail';
import VisitsStats from './VisitsStats'; import VisitsStats from './VisitsStats';
export interface ShortUrlVisitsProps extends RouteComponentProps<{ shortCode: string }>, MercureBoundProps { export interface ShortUrlVisitsProps extends RouteComponentProps<{ shortCode: string }> {
getShortUrlVisits: (shortCode: string, query?: ShlinkVisitsParams) => void; getShortUrlVisits: (shortCode: string, query?: ShlinkVisitsParams) => void;
shortUrlVisits: ShortUrlVisitsState; shortUrlVisits: ShortUrlVisitsState;
getShortUrlDetail: Function; getShortUrlDetail: Function;
@ -16,7 +16,7 @@ export interface ShortUrlVisitsProps extends RouteComponentProps<{ shortCode: st
cancelGetShortUrlVisits: () => void; cancelGetShortUrlVisits: () => void;
} }
const ShortUrlVisits = ({ const ShortUrlVisits = boundToMercureHub(({
history: { goBack }, history: { goBack },
match, match,
location: { search }, location: { search },
@ -25,9 +25,6 @@ const ShortUrlVisits = ({
getShortUrlVisits, getShortUrlVisits,
getShortUrlDetail, getShortUrlDetail,
cancelGetShortUrlVisits, cancelGetShortUrlVisits,
createNewVisit,
loadMercureInfo,
mercureInfo,
}: ShortUrlVisitsProps) => { }: ShortUrlVisitsProps) => {
const { params } = match; const { params } = match;
const { shortCode } = params; const { shortCode } = params;
@ -38,13 +35,12 @@ const ShortUrlVisits = ({
useEffect(() => { useEffect(() => {
getShortUrlDetail(shortCode, domain); getShortUrlDetail(shortCode, domain);
}, []); }, []);
useMercureTopicBinding(mercureInfo, `https://shlink.io/new-visit/${shortCode}`, createNewVisit, loadMercureInfo);
return ( return (
<VisitsStats getVisits={loadVisits} cancelGetVisits={cancelGetShortUrlVisits} visitsInfo={shortUrlVisits}> <VisitsStats getVisits={loadVisits} cancelGetVisits={cancelGetShortUrlVisits} visitsInfo={shortUrlVisits}>
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} /> <ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />
</VisitsStats> </VisitsStats>
); );
}; }, ({ match }) => `https://shlink.io/new-visit/${match.params.shortCode}`);
export default ShortUrlVisits; export default ShortUrlVisits;

View file

@ -1,39 +1,34 @@
import React from 'react'; import React from 'react';
import { RouteComponentProps } from 'react-router'; import { RouteComponentProps } from 'react-router';
import { MercureBoundProps, useMercureTopicBinding } from '../mercure/helpers'; import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub';
import ColorGenerator from '../utils/services/ColorGenerator'; import ColorGenerator from '../utils/services/ColorGenerator';
import { ShlinkVisitsParams } from '../utils/services/types'; import { ShlinkVisitsParams } from '../utils/services/types';
import { TagVisits as TagVisitsState } from './reducers/tagVisits'; import { TagVisits as TagVisitsState } from './reducers/tagVisits';
import TagVisitsHeader from './TagVisitsHeader'; import TagVisitsHeader from './TagVisitsHeader';
import VisitsStats from './VisitsStats'; import VisitsStats from './VisitsStats';
export interface TagVisitsProps extends RouteComponentProps<{ tag: string }>, MercureBoundProps { export interface TagVisitsProps extends RouteComponentProps<{ tag: string }> {
getTagVisits: (tag: string, query: any) => void; getTagVisits: (tag: string, query: any) => void;
tagVisits: TagVisitsState; tagVisits: TagVisitsState;
cancelGetTagVisits: () => void; cancelGetTagVisits: () => void;
} }
const TagVisits = (colorGenerator: ColorGenerator) => ({ const TagVisits = (colorGenerator: ColorGenerator) => boundToMercureHub(({
history: { goBack }, history: { goBack },
match, match,
getTagVisits, getTagVisits,
tagVisits, tagVisits,
cancelGetTagVisits, cancelGetTagVisits,
createNewVisit,
loadMercureInfo,
mercureInfo,
}: TagVisitsProps) => { }: TagVisitsProps) => {
const { params } = match; const { params } = match;
const { tag } = params; const { tag } = params;
const loadVisits = (params: ShlinkVisitsParams) => getTagVisits(tag, params); const loadVisits = (params: ShlinkVisitsParams) => getTagVisits(tag, params);
useMercureTopicBinding(mercureInfo, 'https://shlink.io/new-visit', createNewVisit, loadMercureInfo);
return ( return (
<VisitsStats getVisits={loadVisits} cancelGetVisits={cancelGetTagVisits} visitsInfo={tagVisits}> <VisitsStats getVisits={loadVisits} cancelGetVisits={cancelGetTagVisits} visitsInfo={tagVisits}>
<TagVisitsHeader tagVisits={tagVisits} goBack={goBack} colorGenerator={colorGenerator} /> <TagVisitsHeader tagVisits={tagVisits} goBack={goBack} colorGenerator={colorGenerator} />
</VisitsStats> </VisitsStats>
); );
}; }, () => 'https://shlink.io/new-visit');
export default TagVisits; export default TagVisits;

View file

@ -7,10 +7,9 @@ import appFactory from '../src/App';
describe('<App />', () => { describe('<App />', () => {
let wrapper: ShallowWrapper; let wrapper: ShallowWrapper;
const MainHeader = () => null; const MainHeader = () => null;
const DummyComponent = () => null;
beforeEach(() => { beforeEach(() => {
const App = appFactory(MainHeader, DummyComponent, DummyComponent, DummyComponent, DummyComponent, DummyComponent); const App = appFactory(MainHeader, () => null, () => null, () => null, () => null, () => null, () => null);
wrapper = shallow(<App fetchServers={identity} servers={{}} />); wrapper = shallow(<App fetchServers={identity} servers={{}} />);
}); });

View file

@ -5,6 +5,7 @@ import { faCaretDown as caretDownIcon, faCaretUp as caretUpIcon } from '@fortawe
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import shortUrlsListCreator, { ShortUrlsListProps, SORTABLE_FIELDS } from '../../src/short-urls/ShortUrlsList'; import shortUrlsListCreator, { ShortUrlsListProps, SORTABLE_FIELDS } from '../../src/short-urls/ShortUrlsList';
import { ShortUrl } from '../../src/short-urls/data'; import { ShortUrl } from '../../src/short-urls/data';
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
describe('<ShortUrlsList />', () => { describe('<ShortUrlsList />', () => {
let wrapper: ShallowWrapper; let wrapper: ShallowWrapper;
@ -18,6 +19,7 @@ describe('<ShortUrlsList />', () => {
wrapper = shallow( wrapper = shallow(
<ShortUrlsList <ShortUrlsList
{...Mock.all<ShortUrlsListProps>()} {...Mock.all<ShortUrlsListProps>()}
{...Mock.of<MercureBoundProps>({ mercureInfo: { loading: true } })}
listShortUrls={listShortUrlsMock} listShortUrls={listShortUrlsMock}
resetShortUrlParams={resetShortUrlParamsMock} resetShortUrlParams={resetShortUrlParamsMock}
shortUrlsListParams={{ shortUrlsListParams={{
@ -39,9 +41,8 @@ describe('<ShortUrlsList />', () => {
}), }),
] ]
} }
mercureInfo={{ loading: true } as any}
/>, />,
); ).dive(); // Dive is needed as this component is wrapped in a HOC
}); });
afterEach(jest.resetAllMocks); afterEach(jest.resetAllMocks);

View file

@ -7,6 +7,7 @@ import Message from '../../src/utils/Message';
import SearchField from '../../src/utils/SearchField'; import SearchField from '../../src/utils/SearchField';
import { rangeOf } from '../../src/utils/utils'; import { rangeOf } from '../../src/utils/utils';
import { TagsList } from '../../src/tags/reducers/tagsList'; import { TagsList } from '../../src/tags/reducers/tagsList';
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
describe('<TagsList />', () => { describe('<TagsList />', () => {
let wrapper: ShallowWrapper; let wrapper: ShallowWrapper;
@ -18,11 +19,12 @@ describe('<TagsList />', () => {
wrapper = shallow( wrapper = shallow(
<TagsListComp <TagsListComp
{...Mock.all<TagsListProps>()} {...Mock.all<TagsListProps>()}
{...Mock.all<MercureBoundProps>()}
forceListTags={identity} forceListTags={identity}
filterTags={filterTags} filterTags={filterTags}
tagsList={Mock.of<TagsList>(tagsList)} tagsList={Mock.of<TagsList>(tagsList)}
/>, />,
); ).dive(); // Dive is needed as this component is wrapped in a HOC
return wrapper; return wrapper;
}; };

View file

@ -9,6 +9,7 @@ import ShortUrlVisitsHeader from '../../src/visits/ShortUrlVisitsHeader';
import { ShortUrlVisits as ShortUrlVisitsState } from '../../src/visits/reducers/shortUrlVisits'; import { ShortUrlVisits as ShortUrlVisitsState } from '../../src/visits/reducers/shortUrlVisits';
import { ShortUrlDetail } from '../../src/visits/reducers/shortUrlDetail'; import { ShortUrlDetail } from '../../src/visits/reducers/shortUrlDetail';
import VisitsStats from '../../src/visits/VisitsStats'; import VisitsStats from '../../src/visits/VisitsStats';
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
describe('<ShortUrlVisits />', () => { describe('<ShortUrlVisits />', () => {
let wrapper: ShallowWrapper; let wrapper: ShallowWrapper;
@ -25,6 +26,7 @@ describe('<ShortUrlVisits />', () => {
wrapper = shallow( wrapper = shallow(
<ShortUrlVisits <ShortUrlVisits
{...Mock.all<ShortUrlVisitsProps>()} {...Mock.all<ShortUrlVisitsProps>()}
{...Mock.all<MercureBoundProps>()}
getShortUrlDetail={identity} getShortUrlDetail={identity}
getShortUrlVisits={getShortUrlVisitsMock} getShortUrlVisits={getShortUrlVisitsMock}
match={match} match={match}
@ -34,7 +36,7 @@ describe('<ShortUrlVisits />', () => {
shortUrlDetail={Mock.all<ShortUrlDetail>()} shortUrlDetail={Mock.all<ShortUrlDetail>()}
cancelGetShortUrlVisits={() => {}} cancelGetShortUrlVisits={() => {}}
/>, />,
); ).dive(); // Dive is needed as this component is wrapped in a HOC
}); });
afterEach(() => wrapper.unmount()); afterEach(() => wrapper.unmount());

View file

@ -8,6 +8,7 @@ import TagVisitsHeader from '../../src/visits/TagVisitsHeader';
import ColorGenerator from '../../src/utils/services/ColorGenerator'; import ColorGenerator from '../../src/utils/services/ColorGenerator';
import { TagVisits as TagVisitsStats } from '../../src/visits/reducers/tagVisits'; import { TagVisits as TagVisitsStats } from '../../src/visits/reducers/tagVisits';
import VisitsStats from '../../src/visits/VisitsStats'; import VisitsStats from '../../src/visits/VisitsStats';
import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub';
describe('<TagVisits />', () => { describe('<TagVisits />', () => {
let wrapper: ShallowWrapper; let wrapper: ShallowWrapper;
@ -25,13 +26,14 @@ describe('<TagVisits />', () => {
wrapper = shallow( wrapper = shallow(
<TagVisits <TagVisits
{...Mock.all<TagVisitsProps>()} {...Mock.all<TagVisitsProps>()}
{...Mock.all<MercureBoundProps>()}
getTagVisits={getTagVisitsMock} getTagVisits={getTagVisitsMock}
match={match} match={match}
history={history} history={history}
tagVisits={Mock.of<TagVisitsStats>({ loading: true, visits: [] })} tagVisits={Mock.of<TagVisitsStats>({ loading: true, visits: [] })}
cancelGetTagVisits={() => {}} cancelGetTagVisits={() => {}}
/>, />,
); ).dive(); // Dive is needed as this component is wrapped in a HOC
}); });
afterEach(() => wrapper.unmount()); afterEach(() => wrapper.unmount());