-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
319 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// @ts-check | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { camelCaseObject, getConfig } from '@edx/frontend-platform'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
|
||
const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL; | ||
const getTagListApiUrl = (taxonomyId, page) => new URL( | ||
`api/content_tagging/v1/taxonomies/${taxonomyId}/tags/?page=${page + 1}`, | ||
getApiBaseUrl(), | ||
).href; | ||
|
||
// ToDo: fix types | ||
/** | ||
* @param {number} taxonomyId | ||
* @param {import('../types.mjs').QueryOptions} options | ||
* @returns {import('@tanstack/react-query').UseQueryResult<import('../types.mjs').TaxonomyData>} | ||
*/ // eslint-disable-next-line import/prefer-default-export | ||
export const useTagListData = (taxonomyId, options) => { | ||
const { pageIndex } = options; | ||
return useQuery({ | ||
queryKey: ['tagList', taxonomyId, pageIndex], | ||
queryFn: () => getAuthenticatedHttpClient().get(getTagListApiUrl(taxonomyId, pageIndex)) | ||
.then(camelCaseObject) | ||
.then((response) => response.data), | ||
}); | ||
}; |
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,27 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { | ||
useTagListData, | ||
} from './api'; | ||
|
||
const mockHttpClient = { | ||
get: jest.fn(), | ||
}; | ||
|
||
jest.mock('@tanstack/react-query', () => ({ | ||
useQuery: jest.fn(), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
getAuthenticatedHttpClient: jest.fn(() => mockHttpClient), | ||
})); | ||
|
||
describe('useTagListData', () => { | ||
it('should call useQuery with the correct parameters', () => { | ||
useTagListData('1', { pageIndex: 3 }); | ||
|
||
expect(useQuery).toHaveBeenCalledWith({ | ||
queryKey: ['tagList', '1', 3], | ||
queryFn: expect.any(Function), | ||
}); | ||
}); | ||
}); |
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,42 @@ | ||
// @ts-check | ||
import { | ||
useTagListData, | ||
} from './api'; | ||
|
||
/* eslint-disable max-len */ | ||
/** | ||
* @param {number} taxonomyId | ||
* @param {import("../types.mjs").QueryOptions} options | ||
* @returns {Pick<import('@tanstack/react-query').UseQueryResult, "error" | "isError" | "isFetched" | "isLoading" | "isSuccess" >} | ||
*/ /* eslint-enable max-len */ | ||
export const useTagListDataStatus = (taxonomyId, options) => { | ||
const { | ||
error, | ||
isError, | ||
isFetched, | ||
isLoading, | ||
isSuccess, | ||
} = useTagListData(taxonomyId, options); | ||
return { | ||
error, | ||
isError, | ||
isFetched, | ||
isLoading, | ||
isSuccess, | ||
}; | ||
}; | ||
|
||
// ToDo: fix types | ||
/** | ||
* @param {number} taxonomyId | ||
* @param {import("../types.mjs").QueryOptions} options | ||
* @returns {import("../types.mjs").TaxonomyData | undefined} | ||
*/ | ||
export const useTagListDataResponse = (taxonomyId, options) => { | ||
const { isSuccess, data } = useTagListData(taxonomyId, options); | ||
if (isSuccess) { | ||
return data; | ||
} | ||
|
||
return undefined; | ||
}; |
Oops, something went wrong.