1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00
portainer/app/azure/queries.ts
itsconquest a894e3182a
refactor(azure/aci): migrate dashboard view to react [EE-2189] (#6518)
* refactor(azure/aci): migrate dashboard view to react [EE-2189]

* move aggregate function to azure utils file

* fix type

* introduce dashboard item component

* add error notificatons

* hide resource groups widget if failed to load

* make dashboard a default export

* revert mistake

* refactor based on suggestions

* use object for error data instead of array

* return unused utils file

* move length calculations out of return statement

* only return first error of resource groups queries

* refactor imports/exports, fix bug with errors & add test

* WIP dashboard tests

* allow mocking multiple resource groups

* test for total number of resource groups

* update lock file to fix lint action issue

* finish dashboard tests

* dashboarditem story

* fix(auth): remove caching of user

* add option for link to dashboard item

* rename dashboard test case to match file

* remove optional link and update storybook

* create aria label based on already provided text

* change param name to be clearer
2022-02-25 12:22:56 +13:00

70 lines
1.7 KiB
TypeScript

import _ from 'lodash';
import { useQueries, useQuery } from 'react-query';
import { EnvironmentId } from '@/portainer/environments/types';
import { getResourceGroups } from './services/resource-groups.service';
import { getSubscriptions } from './services/subscription.service';
import { Subscription } from './types';
export function useSubscriptions(environmentId: EnvironmentId) {
return useQuery(
'azure.subscriptions',
() => getSubscriptions(environmentId),
{
meta: {
error: {
title: 'Failure',
message: 'Unable to retrieve Azure subscriptions',
},
},
}
);
}
export function useResourceGroups(
environmentId: EnvironmentId,
subscriptions: Subscription[] = []
) {
const queries = useQueries(
subscriptions.map((subscription) => ({
queryKey: [
'azure',
environmentId,
'subscriptions',
subscription.subscriptionId,
'resourceGroups',
],
queryFn: async () => {
const groups = await getResourceGroups(
environmentId,
subscription.subscriptionId
);
return [subscription.subscriptionId, groups] as const;
},
meta: {
error: {
title: 'Failure',
message: 'Unable to retrieve Azure resource groups',
},
},
}))
);
return {
resourceGroups: Object.fromEntries(
_.compact(
queries.map((q) => {
if (q.data) {
return q.data;
}
return null;
})
)
),
isLoading: queries.some((q) => q.isLoading),
isError: queries.some((q) => q.isError),
error: queries.find((q) => q.error)?.error || null,
};
}