mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-16 04:23:47 +00:00
Moved mercure hub binding from custom hook to HOC
This commit is contained in:
26
src/mercure/helpers/boundToMercureHub.tsx
Normal file
26
src/mercure/helpers/boundToMercureHub.tsx
Normal 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} />;
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { useEffect } from 'react';
|
||||
import { EventSourcePolyfill as EventSource } from 'event-source-polyfill';
|
||||
import { MercureInfo } from '../reducers/mercureInfo';
|
||||
|
||||
@@ -23,18 +22,3 @@ export const bindToMercureTopic = <T>(mercureInfo: MercureInfo, topic: string, o
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user