Migrated to TS all servers helpers

This commit is contained in:
Alejandro Celaya
2020-08-29 13:51:53 +02:00
parent 8cc0695ee9
commit aee4c2d02f
14 changed files with 129 additions and 137 deletions

View File

@@ -0,0 +1,29 @@
import React, { FC, useEffect } from 'react';
import { RouteChildrenProps } from 'react-router';
import Message from '../../utils/Message';
import { isReachableServer, SelectedServer } from '../data';
interface WithSelectedServerProps extends RouteChildrenProps<{ serverId: string }> {
selectServer: (serverId: string) => void;
selectedServer: SelectedServer;
}
export const withSelectedServer = (WrappedComponent: FC<WithSelectedServerProps>, ServerError: FC) => (
props: WithSelectedServerProps,
) => {
const { selectServer, selectedServer, match } = props;
useEffect(() => {
match?.params?.serverId && selectServer(match?.params.serverId);
}, [ match?.params.serverId ]);
if (!selectedServer) {
return <Message loading />;
}
if (!isReachableServer(selectedServer)) {
return <ServerError />;
}
return <WrappedComponent {...props} />;
};