fix(ui): wait for org to be available then fetch org settings just once

pull/18111/head
Timmy Luong 2020-05-14 16:26:19 -07:00
parent 020f895013
commit a2f1a5073d
1 changed files with 34 additions and 12 deletions

View File

@ -1,34 +1,56 @@
// Libraries // Libraries
import {PureComponent} from 'react' import {FunctionComponent, useEffect, useState} from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {get} from 'lodash'
// Constants // Constants
import {CLOUD} from 'src/shared/constants' import {CLOUD} from 'src/shared/constants'
// Types
import {AppState, Organization} from 'src/types'
// Actions // Actions
import {getOrgSettings as getOrgSettingsAction} from 'src/cloud/actions/orgsettings' import {getOrgSettings as getOrgSettingsAction} from 'src/cloud/actions/orgsettings'
interface PassedInProps {
children: React.ReactElement<any>
}
interface StateProps {
org: Organization
}
interface DispatchProps { interface DispatchProps {
getOrgSettings: typeof getOrgSettingsAction getOrgSettings: typeof getOrgSettingsAction
} }
class OrgSettings extends PureComponent<DispatchProps> { type Props = StateProps & DispatchProps & PassedInProps
public componentDidMount() {
if (CLOUD) {
this.props.getOrgSettings()
}
}
public render() { const OrgSettings: FunctionComponent<Props> = ({
return this.props.children org,
} getOrgSettings,
children,
}) => {
const [hasFetchedOrgSettings, setHasFetchedOrgSettings] = useState<boolean>(
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 = { const mdtp: DispatchProps = {
getOrgSettings: getOrgSettingsAction, getOrgSettings: getOrgSettingsAction,
} }
export default connect<{}, DispatchProps, {}>( export default connect<StateProps, DispatchProps, PassedInProps>(
null, mstp,
mdtp mdtp
)(OrgSettings) )(OrgSettings)