|
| 1 | +import { |
| 2 | + Icon, |
| 3 | + IconButton, |
| 4 | + IconButtonWithTooltip, |
| 5 | + Dropdown, |
| 6 | +} from '@openedx/paragon'; |
| 7 | +import { |
| 8 | + AddCircle, |
| 9 | + MoreVert, |
| 10 | +} from '@openedx/paragon/icons'; |
| 11 | +import { useIntl } from '@edx/frontend-platform/i18n'; |
| 12 | +import type { Row } from '@tanstack/react-table'; |
| 13 | + |
| 14 | +import type { |
| 15 | + RowId, |
| 16 | + TreeRowData, |
| 17 | +} from '@src/taxonomy/tree-table/types'; |
| 18 | +import type { TagListRowData } from './types'; |
| 19 | +import messages from './messages'; |
| 20 | + |
| 21 | +interface ActionsHeaderProps { |
| 22 | + onStartDraft: () => void; |
| 23 | + setDraftError: (error: string) => void; |
| 24 | + setIsCreatingTopRow: (isCreating: boolean) => void; |
| 25 | + setEditingRowId: (id: RowId | null) => void; |
| 26 | + setActiveActionMenuRowId: (id: RowId | null) => void; |
| 27 | + hasOpenDraft: boolean; |
| 28 | + disableTagActions: boolean; |
| 29 | + draftInProgressHintId: string; |
| 30 | + canAddTag: boolean; |
| 31 | +} |
| 32 | + |
| 33 | +const ActionsHeader = ({ |
| 34 | + onStartDraft, |
| 35 | + setDraftError, |
| 36 | + setIsCreatingTopRow, |
| 37 | + setEditingRowId, |
| 38 | + setActiveActionMenuRowId, |
| 39 | + hasOpenDraft, |
| 40 | + disableTagActions, |
| 41 | + canAddTag, |
| 42 | + draftInProgressHintId, |
| 43 | +}: ActionsHeaderProps) => { |
| 44 | + const intl = useIntl(); |
| 45 | + return ( |
| 46 | + <div className="d-flex justify-content-end"> |
| 47 | + <IconButtonWithTooltip |
| 48 | + tooltipPlacement="top" |
| 49 | + tooltipContent={<div>{intl.formatMessage(messages.createNewTagTooltip)}</div>} |
| 50 | + src={AddCircle} |
| 51 | + alt={intl.formatMessage(messages.createTagButtonLabel)} |
| 52 | + size="inline" |
| 53 | + onClick={() => { |
| 54 | + onStartDraft(); |
| 55 | + setDraftError(''); |
| 56 | + setIsCreatingTopRow(true); |
| 57 | + setEditingRowId(null); |
| 58 | + setActiveActionMenuRowId(null); |
| 59 | + }} |
| 60 | + disabled={disableTagActions || !canAddTag} |
| 61 | + aria-describedby={hasOpenDraft ? draftInProgressHintId : undefined} |
| 62 | + /> |
| 63 | + </div> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +interface ActionsMenuProps { |
| 68 | + rowData: TagListRowData; |
| 69 | + startSubtagDraft: () => void; |
| 70 | + disableAddSubtag: boolean; |
| 71 | + startEditRow: () => void; |
| 72 | + disableEditRow: boolean; |
| 73 | + reachedMaxDepth: (row: Row<TreeRowData>) => boolean; |
| 74 | + startDeleteRow: (row: Row<TreeRowData>) => void; |
| 75 | + disableDeleteRow: boolean; |
| 76 | + row: Row<TreeRowData>; |
| 77 | +} |
| 78 | + |
| 79 | +const ActionsMenu = ({ |
| 80 | + rowData, |
| 81 | + row, |
| 82 | + startSubtagDraft, |
| 83 | + disableAddSubtag, |
| 84 | + startEditRow, |
| 85 | + disableEditRow, |
| 86 | + reachedMaxDepth, |
| 87 | + startDeleteRow, |
| 88 | + disableDeleteRow, |
| 89 | +}: ActionsMenuProps) => { |
| 90 | + const intl = useIntl(); |
| 91 | + |
| 92 | + const deleteRowMenuItem = ( |
| 93 | + <Dropdown.Item |
| 94 | + onClick={() => startDeleteRow(row)} |
| 95 | + disabled={disableDeleteRow} |
| 96 | + > |
| 97 | + {intl.formatMessage(messages.deleteTag)} |
| 98 | + </Dropdown.Item> |
| 99 | + ); |
| 100 | + |
| 101 | + const editRowMenuItem = ( |
| 102 | + <Dropdown.Item |
| 103 | + onClick={startEditRow} |
| 104 | + disabled={disableEditRow} |
| 105 | + > |
| 106 | + {intl.formatMessage(messages.renameTag)} |
| 107 | + </Dropdown.Item> |
| 108 | + ); |
| 109 | + |
| 110 | + return ( |
| 111 | + <Dropdown> |
| 112 | + <Dropdown.Toggle |
| 113 | + id={`dropdown-toggle-for-tag-${rowData.id}`} |
| 114 | + as={IconButton} |
| 115 | + src={MoreVert} |
| 116 | + iconAs={Icon} |
| 117 | + variant="primary" |
| 118 | + aria-label={intl.formatMessage(messages.moreActionsForTag, { tagName: rowData.value })} |
| 119 | + size="sm" |
| 120 | + /> |
| 121 | + <Dropdown.Menu> |
| 122 | + <Dropdown.Item |
| 123 | + onClick={startSubtagDraft} |
| 124 | + disabled={reachedMaxDepth(row) || disableAddSubtag} |
| 125 | + > |
| 126 | + {intl.formatMessage(messages.addSubtag)} |
| 127 | + </Dropdown.Item> |
| 128 | + {editRowMenuItem} |
| 129 | + {deleteRowMenuItem} |
| 130 | + </Dropdown.Menu> |
| 131 | + </Dropdown> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +const Actions = { |
| 136 | + Header: ActionsHeader, |
| 137 | + Menu: ActionsMenu, |
| 138 | +}; |
| 139 | + |
| 140 | +export default Actions; |
0 commit comments