-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced useAdministrationSchoolsQuery composable
- Loading branch information
1 parent
de6d2b4
commit ccb6852
Showing
4 changed files
with
61 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters