2020-08-29 14:51:53 +03:00
|
|
|
import React, { FC, useEffect } from 'react';
|
|
|
|
import { RouteChildrenProps } from 'react-router';
|
|
|
|
import Message from '../../utils/Message';
|
2020-08-29 19:51:03 +03:00
|
|
|
import { isNotFoundServer, SelectedServer } from '../data';
|
2020-08-29 14:51:53 +03:00
|
|
|
|
|
|
|
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 />;
|
|
|
|
}
|
|
|
|
|
2020-08-29 19:51:03 +03:00
|
|
|
if (isNotFoundServer(selectedServer)) {
|
2020-08-29 14:51:53 +03:00
|
|
|
return <ServerError />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <WrappedComponent {...props} />;
|
|
|
|
};
|