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} />;
};
}