Skip to content

Commit

Permalink
Introduced useAdministrationSchoolsQuery composable
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianoertel committed Aug 22, 2024
1 parent de6d2b4 commit ccb6852
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/components/CreateAdministration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ import { useAuthStore } from '@/store/auth';
import useAdministrationVariantsQuery from '@/composables/queries/useAdministrationVariantsQuery';
import useAdministrationQuery from '@/composables/queries/useAdministrationQuery';
import useAdministrationDistrictsQuery from '@/composables/queries/useAdministrationDistrictsQuery';
import useAdministrationSchoolsQuery from '@/composables/queries/useAdministrationSchoolsQuery';
import TaskPicker from './TaskPicker.vue';
import ConsentPicker from './ConsentPicker.vue';
import OrgPicker from '@/components/OrgPicker.vue';
Expand Down Expand Up @@ -247,31 +248,16 @@ const { data: preExistingAdminInfo } = useAdministrationQuery(props.adminId, {
enabled: shouldFetchExistingAdministrationData,
});
const { data: preDistricts } = useAdministrationDistrictsQuery(props.adminId, {
enabled: shouldFetchExistingAdministrationData.value && Boolean(preExistingAdminInfo.value),
const shouldFetchExistingAdministrationOrgData = computed(() => {
return shouldFetchExistingAdministrationData && !!preExistingAdminInfo.value?.id;
});
// grab schools from preExistingAdminInfo.minimalOrgs.schools
const schoolsToGrab = computed(() => {
const schoolIds = _get(preExistingAdminInfo.value, 'minimalOrgs.schools', []);
return schoolIds.map((schoolId) => {
return {
collection: 'schools',
docId: schoolId,
select: ['name'],
};
});
});
const shouldGrabSchools = computed(() => {
return initialized.value && schoolsToGrab.value.length > 0;
const { data: preDistricts } = useAdministrationDistrictsQuery(props.adminId, {
enabled: shouldFetchExistingAdministrationOrgData,
});
const { data: preSchools } = useQuery({
queryKey: ['schools', 'minimalOrgs', props.adminId],
queryFn: () => fetchDocsById(schoolsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabSchools,
staleTime: 5 * 60 * 1000, // 5 minutes
const { data: preSchools } = useAdministrationSchoolsQuery(props.adminId, {
enabled: shouldFetchExistingAdministrationOrgData,
});
// Grab classes from preExistingAdminInfo.minimalOrgs.classes
Expand Down
52 changes: 52 additions & 0 deletions src/composables/queries/useAdministrationSchoolsQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useQuery } from '@tanstack/vue-query';
import _get from 'lodash/get';
import useAdministrationQuery from '@/composables/queries/useAdministrationQuery';
import { fetchDocsById } from '@/helpers/query/utils';
import { FIRESTORE_COLLECTIONS } from '@/constants/firebase';
import { ADMINISTRATION_SCHOOLS_QUERY_KEY } from '@/constants/queryKeys';

/**
* Administration Schools query.
*
* Query designed to fetch the schools assigned to an administration.
*
* @param {String} administrationId – The ID of the administration to fetch DSGF orgs for.
* @param {QueryOptions|undefined} queryOptions – Optional TanStack query options.
* @returns {UseQueryResult} The TanStack query result.
*/
const useAdministrationSchoolsQuery = (administrationId, queryOptions) => {
return useQuery({
queryKey: [ADMINISTRATION_SCHOOLS_QUERY_KEY, administrationId],
queryFn: async () => {
if (!administrationId) return [];

// Fetch the administration data.
const { data: administrationData } = useAdministrationQuery(administrationId);

// If not administrations were found, abort as there is no data to fetch.
if (!administrationData) return [];

// Build the query to fetch the schools.
// @TODO: This is currently required as fetchDocsById does not actually support fetching by IDs, but rather by
// document objects that contain the collection, docId, and select fields. This should eventually get refactored.
const schoolIds = _get(administrationData, 'minimalOrgs.schools', []);
const schoolDocuments = schoolIds.map((schoolId) => {
return {
collection: FIRESTORE_COLLECTIONS.SCHOOLS,
docId: schoolId,
select: ['name'],
};
});

// If there are no schools to fetch, abort as there is no data to fetch.
if (!schoolDocuments.length) {
return [];
}

return fetchDocsById(schoolDocuments);
},
...queryOptions,
});
};

export default useAdministrationSchoolsQuery;
1 change: 1 addition & 0 deletions src/constants/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const FIRESTORE_COLLECTIONS = Object.freeze({
DISTRICTS: 'districts',
USERS: 'users',
USER_CLAIMS: 'userClaims',
SCHOOLS: 'schools',
});
1 change: 1 addition & 0 deletions src/constants/queryKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const DSGF_ORGS_QUERY_KEY = 'dsgf-orgs';
export const ADMINISTRATION_QUERY_KEY = 'administration';
export const ADMINITRATION_VARIANTS_QUERY_KEY = 'administration-variants';
export const ADMINISTRATION_DISTRICTS_QUERY_KEY = 'administration-districts';
export const ADMINISTRATION_SCHOOLS_QUERY_KEY = 'administration-schools';

0 comments on commit ccb6852

Please sign in to comment.