-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable allocation of restricted runs
ENT-9411
- Loading branch information
Showing
10 changed files
with
182 additions
and
29 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
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
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
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
48 changes: 48 additions & 0 deletions
48
...nts/learner-credit-management/data/hooks/useCatalogContainsContentItemsMultipleQueries.js
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,48 @@ | ||
import { useQueries } from '@tanstack/react-query'; | ||
import { camelCaseObject } from '@edx/frontend-platform/utils'; | ||
|
||
import EnterpriseCatalogApiServiceV2 from '../../../../data/services/EnterpriseCatalogApiServiceV2'; | ||
import { learnerCreditManagementQueryKeys } from '../constants'; | ||
|
||
/** | ||
* Retrieves a response from the following enterprise-catalog endpoint for a SINGLE content key: | ||
* | ||
* /api/v2/enterprise-catalogs/{uuid}/contains_content_items/?course_run_ids={content_key} | ||
* | ||
* @param {*} queryKey The queryKey from the associated `useQuery` call. | ||
* @returns The contains_content_items response. | ||
*/ | ||
const getCatalogContainsContentItem = async ({ queryKey }) => { | ||
const catalogUuid = queryKey[2]; | ||
const contentKey = queryKey[4]; | ||
const response = await EnterpriseCatalogApiServiceV2.retrieveContainsContentItems(catalogUuid, contentKey); | ||
return camelCaseObject(response.data); | ||
}; | ||
|
||
const useCatalogContainsContentItemsMultipleQueries = (catalogUuid, contentKeys = [], { queryOptions } = {}) => { | ||
const multipleResults = useQueries({ | ||
queries: contentKeys.map((contentKey) => ({ | ||
queryKey: learnerCreditManagementQueryKeys.catalogContainsContentItem(catalogUuid, contentKey), | ||
queryFn: getCatalogContainsContentItem, | ||
enabled: !!catalogUuid, | ||
...queryOptions, | ||
})), | ||
}); | ||
return { | ||
data: multipleResults.map(result => result.data), | ||
// Reproduce the above results, but in a form that is more convenient for | ||
// consumers. This only works because we can safely assume the results | ||
// from useQueries are ordered the same as its inputs. | ||
dataByContentKey: Object.fromEntries(multipleResults.map((result, i) => [contentKeys[i], result.data])), | ||
// This whole hook is considered to be still loading if at least one query | ||
// is still loading, implying either that the upstream waterfall query to | ||
// fetch the policy has not yet returned, or at least one call to | ||
// contains-content-items is still being requested. | ||
isLoading: multipleResults.length !== 0 && multipleResults.some(result => result.isLoading), | ||
isFetching: multipleResults.length !== 0 && multipleResults.some(result => result.isFetching), | ||
isError: multipleResults.length !== 0 && multipleResults.some(result => result.isError), | ||
errorByContentKey: Object.fromEntries(multipleResults.map((result, i) => [contentKeys[i], result.error])), | ||
}; | ||
}; | ||
|
||
export default useCatalogContainsContentItemsMultipleQueries; |
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,34 @@ | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
|
||
import { configuration } from '../../config'; | ||
|
||
class EnterpriseCatalogApiServiceV2 { | ||
static baseUrl = `${configuration.ENTERPRISE_CATALOG_BASE_URL}/api/v2`; | ||
|
||
static apiClient = getAuthenticatedHttpClient; | ||
|
||
static enterpriseCatalogsUrl = `${EnterpriseCatalogApiServiceV2.baseUrl}/enterprise-catalogs/`; | ||
|
||
/** | ||
* Retrieves the enterprise-catalog based contains_content_items endpoint for | ||
* ONE content key: | ||
* | ||
* /api/v2/enterprise-catalogs/{uuid}/contains_content_items/?course_run_ids={content_key} | ||
* | ||
* This endpoint technically supports an arbitrary number of content keys, | ||
* but this function only supports one. | ||
* | ||
* @param {*} catalogUuid The catalog to check for content inclusion. | ||
* @param {*} contentKey The content to check for inclusion in the requested catalog. | ||
*/ | ||
static retrieveContainsContentItems(catalogUuid, contentKey) { | ||
const queryParams = new URLSearchParams(); | ||
queryParams.append('course_run_ids', contentKey); | ||
const baseCatalogUrl = `${EnterpriseCatalogApiServiceV2.enterpriseCatalogsUrl}${catalogUuid}`; | ||
return EnterpriseCatalogApiServiceV2.apiClient().get( | ||
`${baseCatalogUrl}/contains_content_items/?${queryParams.toString()}`, | ||
); | ||
} | ||
} | ||
|
||
export default EnterpriseCatalogApiServiceV2; |