-
Notifications
You must be signed in to change notification settings - Fork 5
/
useAdministrationsQuery.js
60 lines (53 loc) · 2.34 KB
/
useAdministrationsQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { computed, ref, toValue } from 'vue';
import { useQuery } from '@tanstack/vue-query';
import _isEmpty from 'lodash/isEmpty';
import { administrationPageFetcher } from '@/helpers/query/administrations';
import useUserClaimsQuery from '@/composables/queries/useUserClaimsQuery';
import useUserType from '@/composables/useUserType';
import { ADMINISTRATIONS_QUERY_KEY } from '@/constants/queryKeys';
/**
* Administrations query.
*
* @param {ref<String>} orderBy – A Vue ref containing the field to order the query by.
* @param {ref<Boolean>} [testAdministrationsOnly=false] – A Vue ref containing whether to fetch only test data.
* @param {QueryOptions|undefined} queryOptions – Optional TanStack query options.
* @returns {UseQueryResult} The TanStack query result.
*/
const useAdministrationsQuery = (orderBy, testAdministrationsOnly = false, queryOptions = undefined) => {
// Fetch the user claims.
const { data: userClaims } = useUserClaimsQuery({
enabled: queryOptions?.enabled ?? true,
});
// Get admin status and administation orgs.
const { isSuperAdmin } = useUserType(userClaims);
const administrationOrgs = computed(() => userClaims.value?.claims?.minimalAdminOrgs);
const exhaustiveAdministrationOrgs = computed(() => userClaims.value?.claims?.adminOrgs);
// Ensure all necessary data is loaded before enabling the query.
const claimsLoaded = computed(() => !_isEmpty(userClaims?.value?.claims));
const isQueryEnabled = computed(() => claimsLoaded.value && (queryOptions?.enabled ?? true));
// Set pagination data to fetch all administrations since pagination is not yet supported.
const currentPage = ref(0);
const itemsPerPage = ref(10000);
// Build query key, based on whether or not we only fetch test administrations.
const queryKey = computed(() =>
toValue(testAdministrationsOnly)
? [ADMINISTRATIONS_QUERY_KEY, 'test-data', currentPage, itemsPerPage, orderBy]
: [ADMINISTRATIONS_QUERY_KEY, currentPage, itemsPerPage, orderBy],
);
return useQuery({
queryKey,
queryFn: () =>
administrationPageFetcher(
orderBy,
itemsPerPage,
currentPage,
isSuperAdmin,
administrationOrgs,
exhaustiveAdministrationOrgs,
testAdministrationsOnly,
),
enabled: isQueryEnabled,
...queryOptions,
});
};
export default useAdministrationsQuery;