Skip to content

Commit

Permalink
fix: total api calls handling (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg committed Aug 18, 2023
1 parent 0c46684 commit ff0da20
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
5 changes: 1 addition & 4 deletions frontend/common/services/useOrganisationUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,12 @@ export async function getOrganisationUsage(
typeof organisationUsageService.endpoints.getOrganisationUsage.initiate
>[1],
) {
store.dispatch(
return store.dispatch(
organisationUsageService.endpoints.getOrganisationUsage.initiate(
data,
options,
),
)
return Promise.all(
store.dispatch(organisationUsageService.util.getRunningQueriesThunk()),
)
}
// END OF FUNCTION_EXPORTS

Expand Down
32 changes: 21 additions & 11 deletions frontend/web/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Format from 'common/utils/format'
import ConfigProvider from 'common/providers/ConfigProvider'
import Permission from 'common/providers/Permission'
import { getOrganisationUsage } from 'common/services/useOrganisationUsage'
import AccountStore from 'common/stores/account-store'
const App = class extends Component {
static propTypes = {
children: propTypes.element.isRequired,
Expand All @@ -35,6 +36,7 @@ const App = class extends Component {
}

state = {
activeOrganisation: 0,
asideIsVisible: !isMobile,
pin: '',
totalApiCalls: 0,
Expand All @@ -48,9 +50,27 @@ const App = class extends Component {
componentDidMount = () => {
getBuildVersion()
this.listenTo(ProjectStore, 'change', () => this.forceUpdate())
this.listenTo(AccountStore, 'change', this.getOrganisationUsage)
this.getOrganisationUsage()
window.addEventListener('scroll', this.handleScroll)
}

getOrganisationUsage = () => {
if (
AccountStore.getOrganisation()?.id &&
this.state.activeOrganisation !== AccountStore.getOrganisation().id
) {
getOrganisationUsage(getStore(), {
organisationId: AccountStore.getOrganisation()?.id,
}).then((res) => {
this.setState({
activeOrganisation: AccountStore.getOrganisation().id,
totalApiCalls: res?.data?.totals.total,
})
})
}
}

toggleDarkMode = () => {
const newValue = !Utils.getFlagsmithHasFeature('dark_mode')
flagsmith.setTrait('dark_mode', newValue)
Expand Down Expand Up @@ -237,17 +257,7 @@ const App = class extends Component {
if (document.location.href.includes('widget')) {
return <div>{this.props.children}</div>
}
if (
AccountStore.getOrganisation() &&
AccountStore.getOrganisation().id &&
this.state.totalApiCalls == 0
) {
getOrganisationUsage(getStore(), {
organisationId: AccountStore.getOrganisation()?.id,
}).then((res) => {
this.setState({ totalApiCalls: res[0]?.data?.totals.total })
})
}

return (
<Provider store={getStore()}>
<AccountProvider
Expand Down

3 comments on commit ff0da20

@vercel
Copy link

@vercel vercel bot commented on ff0da20 Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on ff0da20 Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io

@vercel
Copy link

@vercel vercel bot commented on ff0da20 Aug 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.