2022-04-22 08:33:13 -05:00
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
|
|
import React, { ComponentType, FunctionComponent, useEffect } from 'react';
|
2022-09-19 04:49:35 -05:00
|
|
|
import { connect, MapDispatchToPropsParam, MapStateToPropsParam } from 'react-redux';
|
|
|
|
|
|
|
|
import { useDispatch } from 'app/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-09-05 07:56:08 -05:00
|
|
|
import { cleanUpAction, CleanUpAction } from '../actions/cleanUp';
|
2019-10-08 23:37:07 -05:00
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
export const connectWithCleanUp =
|
2022-09-05 07:56:08 -05:00
|
|
|
<TStateProps extends {} = {}, TDispatchProps = {}, TOwnProps = {}, State = {}, Statics = {}>(
|
2022-02-02 06:02:32 -06:00
|
|
|
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
|
|
|
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
2022-09-05 07:56:08 -05:00
|
|
|
cleanupAction: CleanUpAction
|
2022-02-02 06:02:32 -06:00
|
|
|
) =>
|
|
|
|
(Component: ComponentType<any>) => {
|
|
|
|
const ConnectedComponent = connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
// @ts-ignore
|
|
|
|
)(Component);
|
2019-10-08 23:37:07 -05:00
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
const ConnectedComponentWithCleanUp: FunctionComponent = (props) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
useEffect(() => {
|
|
|
|
return function cleanUp() {
|
2022-09-05 07:56:08 -05:00
|
|
|
dispatch(cleanUpAction({ cleanupAction: cleanupAction }));
|
2022-02-02 06:02:32 -06:00
|
|
|
};
|
|
|
|
}, [dispatch]);
|
|
|
|
// @ts-ignore
|
|
|
|
return <ConnectedComponent {...props} />;
|
|
|
|
};
|
2019-10-08 23:37:07 -05:00
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
ConnectedComponentWithCleanUp.displayName = `ConnectWithCleanUp(${ConnectedComponent.displayName})`;
|
|
|
|
hoistNonReactStatics(ConnectedComponentWithCleanUp, Component);
|
|
|
|
type Hoisted = typeof ConnectedComponentWithCleanUp & Statics;
|
2019-10-08 23:37:07 -05:00
|
|
|
|
2022-02-02 06:02:32 -06:00
|
|
|
return ConnectedComponentWithCleanUp as Hoisted;
|
|
|
|
};
|