Skip to content

Commit

Permalink
feat: Import new taxonomy dialog flow (openedx#1017)
Browse files Browse the repository at this point in the history
This PR updates the existing import tags wizard to also handle  importing new taxonomies.
  • Loading branch information
yusuf-musleh authored May 24, 2024
1 parent c3df0b0 commit d0b3328
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 175 deletions.
37 changes: 12 additions & 25 deletions src/taxonomy/TaxonomyListPage.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-check
import React, { useCallback, useContext, useState } from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import {
Button,
Expand All @@ -12,6 +12,7 @@ import {
Tooltip,
SelectMenu,
MenuItem,
useToggle,
} from '@openedx/paragon';
import {
Add,
Expand All @@ -25,33 +26,24 @@ import { useOrganizationListData } from '../generic/data/apiHooks';
import SubHeader from '../generic/sub-header/SubHeader';
import getPageHeadTitle from '../generic/utils';
import { ALL_TAXONOMIES, apiUrls, UNASSIGNED } from './data/api';
import { useImportNewTaxonomy, useTaxonomyList } from './data/apiHooks';
import { importTaxonomy } from './import-tags';
import { useTaxonomyList } from './data/apiHooks';
import { ImportTagsWizard } from './import-tags';
import messages from './messages';
import TaxonomyCard from './taxonomy-card';
import { TaxonomyContext } from './common/context';

const TaxonomyListHeaderButtons = ({ canAddTaxonomy }) => {
const intl = useIntl();
const importMutation = useImportNewTaxonomy();
const { setToastMessage } = useContext(TaxonomyContext);

const showImportInProgressAlert = useCallback(() => {
/* istanbul ignore next */
if (setToastMessage) {
setToastMessage(intl.formatMessage(messages.importInProgressAlertDescription));
}
}, [setToastMessage]);

const closeImportInProgressAlert = useCallback(() => {
/* istanbul ignore next */
if (setToastMessage) {
setToastMessage(null);
}
}, [setToastMessage]);
const [isImportModalOpen, importModalOpen, importModalClose] = useToggle(false);

return (
<>
{isImportModalOpen && (
<ImportTagsWizard
isOpen={isImportModalOpen}
onClose={importModalClose}
/>
)}
<OverlayTrigger
placement="top"
overlay={(
Expand Down Expand Up @@ -86,12 +78,7 @@ const TaxonomyListHeaderButtons = ({ canAddTaxonomy }) => {
</OverlayTrigger>
<Button
iconBefore={Add}
onClick={() => importTaxonomy(
intl,
importMutation,
showImportInProgressAlert,
closeImportInProgressAlert,
)}
onClick={importModalOpen}
data-testid="taxonomy-import-button"
disabled={!canAddTaxonomy}
>
Expand Down
12 changes: 4 additions & 8 deletions src/taxonomy/TaxonomyListPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import MockAdapter from 'axios-mock-adapter';
import initializeStore from '../store';
import { apiUrls } from './data/api';
import TaxonomyListPage from './TaxonomyListPage';
import { importTaxonomy } from './import-tags';
import { TaxonomyContext } from './common/context';

let store;
Expand All @@ -37,10 +36,6 @@ const listTaxonomiesOrg1Url = `${listTaxonomiesUrl}&org=Org+1`;
const listTaxonomiesOrg2Url = `${listTaxonomiesUrl}&org=Org+2`;
const organizations = ['Org 1', 'Org 2'];

jest.mock('./import-tags', () => ({
importTaxonomy: jest.fn(),
}));

const context = {
toastMessage: null,
setToastMessage: jest.fn(),
Expand Down Expand Up @@ -125,15 +120,16 @@ describe('<TaxonomyListPage />', () => {
expect(importButton).toBeDisabled();
});

it('calls the import taxonomy action when the import button is clicked', async () => {
it('opens the import dialog modal when the import button is clicked', async () => {
axiosMock.onGet(listTaxonomiesUrl).reply(200, { results: [], canAddTaxonomy: true });

const { getByRole } = render(<RootWrapper />);
const { getByRole, getByText } = render(<RootWrapper />);
const importButton = getByRole('button', { name: 'Import' });
// Once the API response is received and rendered, the Import button should be enabled:
await waitFor(() => { expect(importButton).not.toBeDisabled(); });
fireEvent.click(importButton);
expect(importTaxonomy).toHaveBeenCalled();

expect(getByText('Upload file')).toBeInTheDocument();
});

it('should show all "All taxonomies", "Unassigned" and org names in taxonomy org filter', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/taxonomy/data/apiHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const useImportPlan = (taxonomyId, file) => useQuery({
* @type {import("@tanstack/react-query").QueryFunction<string|null>}
*/
queryFn: async () => {
if (file === null) {
if (!taxonomyId || file === null) {
return null;
}
const formData = new FormData();
Expand Down
Loading

0 comments on commit d0b3328

Please sign in to comment.