|
| 1 | +import { Text, TextList, TextListItem, TextVariants } from '@patternfly/react-core'; |
| 2 | +import type { Provider } from 'api/providers'; |
| 3 | +import { ProviderType } from 'api/providers'; |
| 4 | +import type { AxiosError } from 'axios'; |
| 5 | +import messages from 'locales/messages'; |
| 6 | +import React, { useEffect } from 'react'; |
| 7 | +import { useIntl } from 'react-intl'; |
| 8 | +import { useDispatch, useSelector } from 'react-redux'; |
| 9 | +import type { AnyAction } from 'redux'; |
| 10 | +import type { ThunkDispatch } from 'redux-thunk'; |
| 11 | +import { routes } from 'routes'; |
| 12 | +import { NotAvailable } from 'routes/components/page/notAvailable'; |
| 13 | +import { LoadingState } from 'routes/components/state/loadingState'; |
| 14 | +import type { RootState } from 'store'; |
| 15 | +import { FetchStatus } from 'store/common'; |
| 16 | +import { providersActions, providersSelectors } from 'store/providers'; |
| 17 | +import { formatPath, getReleasePath } from 'utils/paths'; |
| 18 | + |
| 19 | +import { styles } from './modal.styles'; |
| 20 | + |
| 21 | +interface CloudIntegrationOwnProps { |
| 22 | + uuid?: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface CloudIntegrationStateProps { |
| 26 | + provider: Provider; |
| 27 | + providerError: AxiosError; |
| 28 | + providerFetchStatus: FetchStatus; |
| 29 | + providerQueryString: string; |
| 30 | + uuid?: string; |
| 31 | +} |
| 32 | + |
| 33 | +export interface CloudIntegrationMapProps { |
| 34 | + // TBD... |
| 35 | +} |
| 36 | + |
| 37 | +type CloudIntegrationProps = CloudIntegrationOwnProps; |
| 38 | + |
| 39 | +const CloudIntegration: React.FC<CloudIntegrationProps> = ({ uuid }: CloudIntegrationProps) => { |
| 40 | + const intl = useIntl(); |
| 41 | + |
| 42 | + const { provider, providerError, providerFetchStatus } = useMapToProps({ uuid }); |
| 43 | + |
| 44 | + const title = intl.formatMessage(messages.optimizations); |
| 45 | + |
| 46 | + if (providerError) { |
| 47 | + return <NotAvailable title={title} />; |
| 48 | + } |
| 49 | + |
| 50 | + const release = getReleasePath(); |
| 51 | + |
| 52 | + if (providerFetchStatus === FetchStatus.inProgress) { |
| 53 | + return ( |
| 54 | + <div style={styles.loading}> |
| 55 | + <LoadingState /> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + return ( |
| 60 | + <> |
| 61 | + <Text component={TextVariants.h3}>{intl.formatMessage(messages.cloudIntegration)}</Text> |
| 62 | + <TextList isPlain> |
| 63 | + <TextListItem> |
| 64 | + <span style={styles.spacing}> |
| 65 | + {intl.formatMessage(messages.source, { value: provider?.source_type?.toLowerCase() })} |
| 66 | + </span> |
| 67 | + <a href={`${release}/settings/integrations/detail/${provider?.id}`}>{provider?.name}</a> |
| 68 | + </TextListItem> |
| 69 | + {provider?.cost_models?.length === 0 && ( |
| 70 | + <TextListItem> |
| 71 | + <a href={formatPath(routes.settings.path)}>{intl.formatMessage(messages.assignCostModel)}</a> |
| 72 | + </TextListItem> |
| 73 | + )} |
| 74 | + </TextList> |
| 75 | + </> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +// eslint-disable-next-line no-empty-pattern |
| 80 | +const useMapToProps = ({ uuid }): CloudIntegrationStateProps => { |
| 81 | + const dispatch: ThunkDispatch<RootState, any, AnyAction> = useDispatch(); |
| 82 | + |
| 83 | + const providerQueryString = uuid; |
| 84 | + const provider = useSelector( |
| 85 | + (state: RootState) => providersSelectors.selectProviders(state, ProviderType.uuid, providerQueryString) as Provider |
| 86 | + ); |
| 87 | + const providerError = useSelector((state: RootState) => |
| 88 | + providersSelectors.selectProvidersError(state, ProviderType.uuid, providerQueryString) |
| 89 | + ); |
| 90 | + const providerFetchStatus = useSelector((state: RootState) => |
| 91 | + providersSelectors.selectProvidersFetchStatus(state, ProviderType.uuid, providerQueryString) |
| 92 | + ); |
| 93 | + |
| 94 | + useEffect(() => { |
| 95 | + if (!providerError && providerFetchStatus !== FetchStatus.inProgress) { |
| 96 | + dispatch(providersActions.fetchProviders(ProviderType.uuid, providerQueryString)); |
| 97 | + } |
| 98 | + }, []); |
| 99 | + |
| 100 | + return { |
| 101 | + provider, |
| 102 | + providerError, |
| 103 | + providerFetchStatus, |
| 104 | + providerQueryString, |
| 105 | + }; |
| 106 | +}; |
| 107 | + |
| 108 | +export { CloudIntegration }; |
0 commit comments