diff --git a/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx b/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx index ad87601952..44e1daf3fe 100644 --- a/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx +++ b/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx @@ -9,6 +9,7 @@ import { } from '@edx/paragon'; import { MoreVert } from '@edx/paragon/icons'; import PropTypes from 'prop-types'; +import _ from 'lodash'; import ExportModal from '../export-modal'; import { importTaxonomyTags } from '../import-tags'; @@ -19,39 +20,30 @@ const TaxonomyMenu = ({ }) => { const intl = useIntl(); - const getTaxonomyMenuItems = () => { - const { systemDefined, allowFreeText } = taxonomy; - const menuItems = ['import', 'export']; - if (systemDefined) { - // System defined taxonomies cannot be imported - return menuItems.filter((item) => !['import'].includes(item)); - } - if (allowFreeText) { - // Free text taxonomies cannot be imported - return menuItems.filter((item) => !['import'].includes(item)); - } - return menuItems; - }; - - const menuItems = getTaxonomyMenuItems(); - const [isExportModalOpen, exportModalOpen, exportModalClose] = useToggle(false); - const menuItemActions = { - import: () => importTaxonomyTags(taxonomy.id, intl), - export: exportModalOpen, - }; + const getTaxonomyMenuItems = () => { + let menuItems = { + import: { + title: intl.formatMessage(messages.importMenu), + action: () => importTaxonomyTags(taxonomy.id, intl), + // Hide import menu item if taxonomy is system defined or allows free text + hide: taxonomy.systemDefined || taxonomy.allowFreeText, + }, + export: { + title: intl.formatMessage(messages.exportMenu), + action: exportModalOpen, + }, + }; - const menuItemMessages = { - import: messages.importMenu, - export: messages.exportMenu, - }; + // Remove hidden menu items + menuItems = _.pickBy(menuItems, (value) => !value.hide); - const onClickMenuItem = (e, menuName) => { - e.preventDefault(); - menuItemActions[menuName]?.(); + return menuItems; }; + const menuItems = getTaxonomyMenuItems(); + const renderModals = () => isExportModalOpen && ( - {menuItems.map((item) => ( + {Object.keys(menuItems).map((key) => ( onClickMenuItem(e, item)} + key={key} + data-testid={`taxonomy-menu-${key}`} + onClick={ + (e) => { + e.preventDefault(); + menuItems[key].action?.(); + } + } > - {intl.formatMessage(menuItemMessages[item])} + {menuItems[key].title} ))}