shlink-web-client/src/servers/helpers/withSelectedServer.tsx

35 lines
1,017 B
TypeScript
Raw Normal View History

2020-11-14 00:44:26 +03:00
import { FC, useEffect } from 'react';
import { RouteComponentProps } from 'react-router';
2020-08-29 14:51:53 +03:00
import Message from '../../utils/Message';
import { isNotFoundServer, SelectedServer } from '../data';
import NoMenuLayout from '../../common/NoMenuLayout';
2020-08-29 14:51:53 +03:00
interface WithSelectedServerProps extends RouteComponentProps<{ serverId: string }> {
2020-08-29 14:51:53 +03:00
selectServer: (serverId: string) => void;
selectedServer: SelectedServer;
}
export function withSelectedServer<T = {}>(WrappedComponent: FC<WithSelectedServerProps & T>, ServerError: FC) {
return (props: WithSelectedServerProps & T) => {
const { selectServer, selectedServer, match } = props;
2020-08-29 14:51:53 +03:00
useEffect(() => {
selectServer(match.params.serverId);
}, [ match.params.serverId ]);
2020-08-29 14:51:53 +03:00
if (!selectedServer) {
return (
<NoMenuLayout>
<Message loading noMargin />
</NoMenuLayout>
);
}
2020-08-29 14:51:53 +03:00
if (isNotFoundServer(selectedServer)) {
return <ServerError />;
}
2020-08-29 14:51:53 +03:00
return <WrappedComponent {...props} />;
};
}