Stop injecting dependencies in selectServer action

This commit is contained in:
Alejandro Celaya
2025-11-14 08:50:08 +01:00
parent e9951e95a9
commit b295240d28
6 changed files with 33 additions and 37 deletions

View File

@@ -2,34 +2,37 @@ import { Message } from '@shlinkio/shlink-frontend-kit';
import type { FC } from 'react';
import { useEffect } from 'react';
import { useParams } from 'react-router';
import type { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
import { NoMenuLayout } from '../../common/NoMenuLayout';
import type { FCWithDeps } from '../../container/utils';
import { useDependencies } from '../../container/utils';
import type { SelectedServer } from '../data';
import { isNotFoundServer } from '../data';
import type { SelectServerOptions } from '../reducers/selectedServer';
export type WithSelectedServerProps = {
selectServer: (serverId: string) => void;
selectServer: (options: SelectServerOptions) => void;
selectedServer: SelectedServer;
};
type WithSelectedServerPropsDeps = {
export type WithSelectedServerPropsDeps = {
ServerError: FC;
buildShlinkApiClient: ShlinkApiClientBuilder;
};
export function withSelectedServer<T extends object>(
WrappedComponent: FCWithDeps<WithSelectedServerProps & T, WithSelectedServerPropsDeps>,
) {
const ComponentWrapper: FCWithDeps<WithSelectedServerProps & T, WithSelectedServerPropsDeps> = (props) => {
const { ServerError } = useDependencies(ComponentWrapper);
const { ServerError, buildShlinkApiClient } = useDependencies(ComponentWrapper);
const params = useParams<{ serverId: string }>();
const { selectServer, selectedServer } = props;
useEffect(() => {
if (params.serverId) {
selectServer(params.serverId);
selectServer({ serverId: params.serverId, buildShlinkApiClient });
}
}, [params.serverId, selectServer]);
}, [buildShlinkApiClient, params.serverId, selectServer]);
if (!selectedServer) {
return (