diff --git a/ui/src/cloud/components/OrgSettings.tsx b/ui/src/cloud/components/OrgSettings.tsx index 87ac96b84d..105cb2d76b 100644 --- a/ui/src/cloud/components/OrgSettings.tsx +++ b/ui/src/cloud/components/OrgSettings.tsx @@ -1,34 +1,56 @@ // Libraries -import {PureComponent} from 'react' +import {FunctionComponent, useEffect, useState} from 'react' import {connect} from 'react-redux' +import {get} from 'lodash' // Constants import {CLOUD} from 'src/shared/constants' +// Types +import {AppState, Organization} from 'src/types' + // Actions import {getOrgSettings as getOrgSettingsAction} from 'src/cloud/actions/orgsettings' +interface PassedInProps { + children: React.ReactElement +} +interface StateProps { + org: Organization +} interface DispatchProps { getOrgSettings: typeof getOrgSettingsAction } -class OrgSettings extends PureComponent { - public componentDidMount() { - if (CLOUD) { - this.props.getOrgSettings() - } - } +type Props = StateProps & DispatchProps & PassedInProps - public render() { - return this.props.children - } +const OrgSettings: FunctionComponent = ({ + org, + getOrgSettings, + children, +}) => { + const [hasFetchedOrgSettings, setHasFetchedOrgSettings] = useState( + false + ) + useEffect(() => { + if (CLOUD && org && !hasFetchedOrgSettings) { + setHasFetchedOrgSettings(true) + getOrgSettings() + } + }, [org]) + + return children } +const mstp = (state: AppState): StateProps => ({ + org: get(state, 'resources.orgs.org', null), +}) + const mdtp: DispatchProps = { getOrgSettings: getOrgSettingsAction, } -export default connect<{}, DispatchProps, {}>( - null, +export default connect( + mstp, mdtp )(OrgSettings)