Skip to content

Commit

Permalink
feat: assign taxonomy to organizations [FC-0036] (#760)
Browse files Browse the repository at this point in the history
This PR adds a UI to assign organizations to a Taxonomy.

Co-authored-by: Jillian <[email protected]>
  • Loading branch information
rpenido and pomegranited authored Jan 12, 2024
1 parent bfcd3e6 commit 1fef358
Show file tree
Hide file tree
Showing 25 changed files with 1,161 additions and 214 deletions.
1 change: 1 addition & 0 deletions src/taxonomy/TaxonomyListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Check,
} from '@edx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';

import { Helmet } from 'react-helmet';

import { useOrganizationListData } from '../generic/data/apiHooks';
Expand Down
14 changes: 10 additions & 4 deletions src/taxonomy/TaxonomyListPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ const RootWrapper = () => (
<IntlProvider locale="en" messages={{}}>
<QueryClientProvider client={queryClient}>
<TaxonomyContext.Provider value={context}>
<QueryClientProvider client={queryClient}>
<TaxonomyListPage intl={injectIntl} />
</QueryClientProvider>
<TaxonomyListPage intl={injectIntl} />
</TaxonomyContext.Provider>
</QueryClientProvider>
</IntlProvider>
Expand All @@ -71,6 +69,10 @@ describe('<TaxonomyListPage />', () => {
axiosMock.onGet(organizationsListUrl).reply(200, organizations);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should render page and page title correctly', () => {
const { getByText } = render(<RootWrapper />);
expect(getByText('Taxonomies')).toBeInTheDocument();
Expand Down Expand Up @@ -134,7 +136,11 @@ describe('<TaxonomyListPage />', () => {
it('should show all "All taxonomies", "Unassigned" and org names in taxonomy org filter', async () => {
useIsTaxonomyListDataLoaded.mockReturnValue(true);
useTaxonomyListDataResponse.mockReturnValue({
results: taxonomies,
results: [{
id: 1,
name: 'Taxonomy',
description: 'This is a description',
}],
});

const {
Expand Down
16 changes: 16 additions & 0 deletions src/taxonomy/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ export const getExportTaxonomyApiUrl = (pk, format) => new URL(
`api/content_tagging/v1/taxonomies/${pk}/export/?output_format=${format}&download=1`,
getApiBaseUrl(),
).href;

export const getTaxonomyTemplateApiUrl = (format) => new URL(
`api/content_tagging/v1/taxonomies/import/template.${format}`,
getApiBaseUrl(),
).href;

/**
* Get the URL for a Taxonomy
* @param {number} pk
* @returns {string}
*/
export const getTaxonomyApiUrl = (pk) => new URL(`api/content_tagging/v1/taxonomies/${pk}/`, getApiBaseUrl()).href;

/**
Expand All @@ -47,6 +54,15 @@ export async function deleteTaxonomy(pk) {
await getAuthenticatedHttpClient().delete(getTaxonomyApiUrl(pk));
}

/** Get a Taxonomy
* @param {number} pk
* @returns {Promise<import("./types.mjs").TaxonomyData>}
*/
export async function getTaxonomy(pk) {
const { data } = await getAuthenticatedHttpClient().get(getTaxonomyApiUrl(pk));
return camelCaseObject(data);
}

/**
* Downloads the file of the exported taxonomy
* @param {number} pk
Expand Down
8 changes: 8 additions & 0 deletions src/taxonomy/data/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getTaxonomyListApiUrl,
getTaxonomyListData,
getTaxonomyApiUrl,
getTaxonomy,
deleteTaxonomy,
} from './api';

Expand Down Expand Up @@ -65,6 +66,13 @@ describe('taxonomy api calls', () => {
expect(axiosMock.history.delete[0].url).toEqual(getTaxonomyApiUrl());
});

it('should call get taxonomy', async () => {
axiosMock.onGet(getTaxonomyApiUrl(1)).reply(200);
await getTaxonomy(1);

expect(axiosMock.history.get[0].url).toEqual(getTaxonomyApiUrl(1));
});

it('Export should set window.location.href correctly', () => {
const pk = 1;
const format = 'json';
Expand Down
44 changes: 43 additions & 1 deletion src/taxonomy/data/apiHooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Ex. useTaxonomyListDataResponse & useIsTaxonomyListDataLoaded.
*/
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getTaxonomyListData, deleteTaxonomy } from './api';
import { getTaxonomyListData, deleteTaxonomy, getTaxonomy } from './api';

/**
* Builds the query to get the taxonomy list
Expand Down Expand Up @@ -41,6 +41,16 @@ export const useDeleteTaxonomy = () => {
return mutate;
};

/** Builds the query to get the taxonomy detail
* @param {number} taxonomyId
*/
const useTaxonomyDetailData = (taxonomyId) => (
useQuery({
queryKey: ['taxonomyDetail', taxonomyId],
queryFn: async () => getTaxonomy(taxonomyId),
})
);

/**
* Gets the taxonomy list data
* @param {string} org Optional organization query param
Expand All @@ -62,3 +72,35 @@ export const useTaxonomyListDataResponse = (org) => {
export const useIsTaxonomyListDataLoaded = (org) => (
useTaxonomyListData(org).status === 'success'
);

/**
* @param {number} taxonomyId
* @returns {Pick<import('@tanstack/react-query').UseQueryResult, "error" | "isError" | "isFetched" | "isSuccess">}
*/
export const useTaxonomyDetailDataStatus = (taxonomyId) => {
const {
isError,
error,
isFetched,
isSuccess,
} = useTaxonomyDetailData(taxonomyId);
return {
isError,
error,
isFetched,
isSuccess,
};
};

/**
* @param {number} taxonomyId
* @returns {import("./types.mjs").TaxonomyData | undefined}
*/
export const useTaxonomyDetailDataResponse = (taxonomyId) => {
const { isSuccess, data } = useTaxonomyDetailData(taxonomyId);
if (isSuccess) {
return data;
}

return undefined;
};
1 change: 1 addition & 0 deletions src/taxonomy/data/apiHooks.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery, useMutation } from '@tanstack/react-query';
import { act } from '@testing-library/react';

import {
useTaxonomyListDataResponse,
useIsTaxonomyListDataLoaded,
Expand Down
1 change: 1 addition & 0 deletions src/taxonomy/data/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @property {boolean} visibleToAuthors
* @property {number} tagsCount
* @property {string[]} orgs
* @property {boolean} allOrgs
*/

/**
Expand Down
Loading

0 comments on commit 1fef358

Please sign in to comment.