Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: total api calls handling #2583

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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