Skip to content

Commit

Permalink
refactor: change menu item list
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Nov 29, 2023
1 parent 689071c commit 0fe4b3c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 69 deletions.
17 changes: 0 additions & 17 deletions src/taxonomy/taxonomy-card/TaxonomyCard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,4 @@
text-overflow: ellipsis;
white-space: nowrap;
}

.taxonomy-menu-item:focus {
/**
* There is a bug in the menu that auto focus the first item.
* We convert the focus style to a normal style.
*/
background-color: white !important;
font-weight: normal !important;
}

.taxonomy-menu-item:focus:hover {
/**
* Check the previous block about the focus.
* This enable a normal hover to focused items.
*/
background-color: $light-500 !important;
}
}
25 changes: 6 additions & 19 deletions src/taxonomy/taxonomy-card/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,12 @@ const TaxonomyCard = ({ className, original }) => {

const intl = useIntl();

const getHeaderActions = () => {
if (systemDefined) {
// We don't show the export menu, because the system-taxonomies
// can't be exported. The API returns and error.
// The entire menu has been hidden because currently only
// the export menu exists.
//
// TODO When adding more menus, change this logic to hide only the export menu.
return undefined;
}

return (
<TaxonomyMenu
id={id}
name={name}
iconMenu
/>
);
};
const getHeaderActions = () => (
<TaxonomyMenu
taxonomy={original}
iconMenu
/>
);

return (
<Card
Expand Down
11 changes: 1 addition & 10 deletions src/taxonomy/taxonomy-detail/TaxonomyDetailPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ const TaxonomyDetailPage = () => {

const getHeaderActions = () => (
<TaxonomyMenu
id={taxonomy.id}
name={taxonomy.name}
disabled={
// We don't show the export menu, because the system-taxonomies
// can't be exported. The API returns and error.
// The entire menu has been disabled because currently only
// the export menu exists.
// ToDo: When adding more menus, change this logic to hide only the export menu.
taxonomy.systemDefined
}
taxonomy={taxonomy}
/>
);

Expand Down
61 changes: 42 additions & 19 deletions src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,38 @@ import { importTaxonomyTags } from '../import-tags';
import messages from './messages';

const TaxonomyMenu = ({
id, name, iconMenu, disabled,
taxonomy, iconMenu,
}) => {
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(id, intl),
import: () => importTaxonomyTags(taxonomy.id, intl),
export: exportModalOpen,
};

const menuItemMessages = {
import: messages.importMenu,
export: messages.exportMenu,
};

const onClickMenuItem = (e, menuName) => {
e.preventDefault();
menuItemActions[menuName]?.();
Expand All @@ -35,8 +56,8 @@ const TaxonomyMenu = ({
<ExportModal
isOpen={isExportModalOpen}
onClose={exportModalClose}
taxonomyId={id}
taxonomyName={name}
taxonomyId={taxonomy.id}
taxonomyName={taxonomy.name}
/>
);

Expand All @@ -47,34 +68,36 @@ const TaxonomyMenu = ({
src={MoreVert}
iconAs={Icon}
variant="primary"
alt={intl.formatMessage(messages.actionsButtonAlt, { name })}
alt={intl.formatMessage(messages.actionsButtonAlt, { name: taxonomy.name })}
data-testid="taxonomy-menu-button"
disabled={disabled}
disabled={menuItems.length === 0}
>
{intl.formatMessage(messages.actionsButtonLabel)}
</Dropdown.Toggle>
<Dropdown.Menu data-testid="taxonomy-menu">
<Dropdown.Item data-testid="taxonomy-menu-import" onClick={(e) => onClickMenuItem(e, 'import')}>
{intl.formatMessage(messages.importMenu)}
</Dropdown.Item>
<Dropdown.Item data-testid="taxonomy-menu-export" onClick={(e) => onClickMenuItem(e, 'export')}>
{intl.formatMessage(messages.exportMenu)}
</Dropdown.Item>
{menuItems.map((item) => (
<Dropdown.Item
key={item}
data-testid={`taxonomy-menu-${item}`}
onClick={(e) => onClickMenuItem(e, item)}
>
{intl.formatMessage(menuItemMessages[item])}
</Dropdown.Item>
))}
</Dropdown.Menu>
{renderModals()}
</Dropdown>
);
};

TaxonomyMenu.propTypes = {
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
taxonomy: PropTypes.shape({
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
systemDefined: PropTypes.bool.isRequired,
allowFreeText: PropTypes.bool.isRequired,
}).isRequired,
iconMenu: PropTypes.bool.isRequired,
disabled: PropTypes.bool,
};

TaxonomyMenu.defaultProps = {
disabled: false,
};

export default TaxonomyMenu;
51 changes: 47 additions & 4 deletions src/taxonomy/taxonomy-menu/TaxonomyMenu.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,34 @@ jest.mock('../data/api', () => ({
}));

const TaxonomyMenuComponent = ({
systemDefined,
allowFreeText,
iconMenu,
disabled,
}) => (
<AppProvider store={store}>
<IntlProvider locale="en" messages={{}}>
<TaxonomyMenu id={taxonomyId} name={taxonomyName} iconMenu={iconMenu} disabled={disabled} />
<TaxonomyMenu
taxonomy={{
id: taxonomyId,
name: taxonomyName,
systemDefined,
allowFreeText,
}}
iconMenu={iconMenu}
/>
</IntlProvider>
</AppProvider>
);

TaxonomyMenuComponent.propTypes = {
iconMenu: PropTypes.bool.isRequired,
disabled: PropTypes.bool,
systemDefined: PropTypes.bool,
allowFreeText: PropTypes.bool,
};

TaxonomyMenuComponent.defaultProps = {
disabled: false,
systemDefined: false,
allowFreeText: false,
};

describe('<TaxonomyMenu />', async () => {
Expand Down Expand Up @@ -79,6 +90,38 @@ describe('<TaxonomyMenu />', async () => {
expect(getByTestId('taxonomy-menu-button')).toBeVisible();
});

test('doesnt show systemDefined taxonomies disabled menus', () => {
const { getByTestId } = render(<TaxonomyMenuComponent iconMenu={iconMenu} systemDefined />);

// Menu closed/doesn't exist yet
expect(() => getByTestId('taxonomy-menu')).toThrow();

// Click on the menu button to open
fireEvent.click(getByTestId('taxonomy-menu-button'));

// Menu opened
expect(getByTestId('taxonomy-menu')).toBeVisible();

// Check that the import menu is not show
expect(() => getByTestId('taxonomy-menu-import')).toThrow();
});

test('doesnt show freeText taxonomies disabled menus', () => {
const { getByTestId } = render(<TaxonomyMenuComponent iconMenu={iconMenu} allowFreeText />);

// Menu closed/doesn't exist yet
expect(() => getByTestId('taxonomy-menu')).toThrow();

// Click on the menu button to open
fireEvent.click(getByTestId('taxonomy-menu-button'));

// Menu opened
expect(getByTestId('taxonomy-menu')).toBeVisible();

// Check that the import menu is not show
expect(() => getByTestId('taxonomy-menu-import')).toThrow();
});

test('should open export modal on export menu click', () => {
const { getByTestId, getByText } = render(<TaxonomyMenuComponent iconMenu={iconMenu} />);

Expand Down

0 comments on commit 0fe4b3c

Please sign in to comment.