import { $q, $templateCache } from 'ngimport'; import React from 'react'; import type { Observable } from 'rxjs'; import { ServerGroupDetails } from './ServerGroupDetails'; import type { Application } from '../../application'; import { CloudProviderRegistry } from '../../cloudProvider'; import type { IServerGroup } from '../../domain'; import { AngularJSAdapter, ReactInjector } from '../../reactShims'; export interface IServerGroupDetailsWrapperProps { app: Application; serverGroup: { name: string; accountId: string; region: string; }; } export type DetailsGetter = (props: IServerGroupDetailsProps, autoClose: () => void) => Observable; export interface IServerGroupDetailsWrapperState { angular: { template: string; controller: string; }; detailsGetter: DetailsGetter; sections: Array>; Actions: React.ComponentType; } export interface IServerGroupActionsProps { app: Application; serverGroup: IServerGroup; } export interface IServerGroupDetailsSectionProps { app: Application; serverGroup: IServerGroup; } export interface IServerGroupDetailsProps extends IServerGroupDetailsWrapperProps { Actions: React.ComponentType; detailsGetter: DetailsGetter; sections: Array>; } export interface IServerGroupDetailsState { loading: boolean; serverGroup: IServerGroup; } export class ServerGroupDetailsWrapper extends React.Component< IServerGroupDetailsWrapperProps, IServerGroupDetailsWrapperState > { constructor(props: IServerGroupDetailsWrapperProps) { super(props); this.state = { angular: { template: undefined, controller: undefined, }, Actions: undefined, detailsGetter: undefined, sections: [], }; } private getServerGroupDetailsTemplate(): void { const { provider } = ReactInjector.$stateParams; $q.all([ CloudProviderRegistry.getValue(provider, 'serverGroup.detailsActions'), CloudProviderRegistry.getValue(provider, 'serverGroup.detailsGetter'), CloudProviderRegistry.getValue(provider, 'serverGroup.detailsSections'), CloudProviderRegistry.getValue(provider, 'serverGroup.detailsTemplateUrl'), CloudProviderRegistry.getValue(provider, 'serverGroup.detailsController'), ]).then( ( values: [ React.ComponentClass, DetailsGetter, Array>, string, string, ], ) => { const [Actions, detailsGetter, sections, templateUrl, controller] = values; const template = templateUrl ? $templateCache.get(templateUrl) : undefined; this.setState({ angular: { template, controller }, Actions, detailsGetter, sections }); }, ); } public componentDidMount(): void { this.getServerGroupDetailsTemplate(); } public componentWillReceiveProps(): void { this.getServerGroupDetailsTemplate(); } public render() { const { app, serverGroup } = this.props; const { angular: { template, controller }, Actions, detailsGetter, sections, } = this.state; if (Actions && detailsGetter && sections) { // react return ( ); } // angular if (template && controller) { return ( ); } return null; } }