Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/taxonomy/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const apiUrls = {
tagsPlanImport: (taxonomyId: number) => makeUrl(`${taxonomyId}/tags/import/plan/`),
createTag: (taxonomyId: number) => makeUrl(`${taxonomyId}/tags/`),
updateTag: (taxonomyId: number) => makeUrl(`${taxonomyId}/tags/`),
deleteTag: (taxonomyId: number) => makeUrl(`${taxonomyId}/tags/`),
} satisfies Record<string, (...args: any[]) => string>;

/**
Expand Down
20 changes: 20 additions & 0 deletions src/taxonomy/data/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,23 @@ export const useUpdateTag = (taxonomyId: number) => {
},
});
};

export const useDeleteTag = (taxonomyId: number) => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: async ({ value, withSubtags }: { value: string; withSubtags: boolean; }) => {
const body = { tags: [value], with_subtags: withSubtags };
await getAuthenticatedHttpClient().delete(apiUrls.deleteTag(taxonomyId), {
data: body,
});
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: taxonomyQueryKeys.taxonomyTagList(taxonomyId),
});
// In the metadata, 'tagsCount' (and possibly other fields) will have changed:
queryClient.invalidateQueries({ queryKey: taxonomyQueryKeys.taxonomyMetadata(taxonomyId) });
},
});
};
138 changes: 138 additions & 0 deletions src/taxonomy/tag-list/Actions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
Icon,
IconButton,
IconButtonWithTooltip,
Dropdown,
} from '@openedx/paragon';
import {
AddCircle,
MoreVert,
} from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';
import type { Row } from '@tanstack/react-table';

import type {
RowId,
TreeRowData,
} from '@src/taxonomy/tree-table/types';
import type { TagListRowData } from './types';
import messages from './messages';

interface ActionsHeaderProps {
onStartDraft: () => void;
setDraftError: (error: string) => void;
setIsCreatingTopRow: (isCreating: boolean) => void;
setEditingRowId: (id: RowId | null) => void;
setActiveActionMenuRowId: (id: RowId | null) => void;
hasOpenDraft: boolean;
draftInProgressHintId: string;
canAddTag: boolean;
}

const ActionsHeader = ({
onStartDraft,
setDraftError,
setIsCreatingTopRow,
setEditingRowId,
setActiveActionMenuRowId,
hasOpenDraft,
canAddTag,
draftInProgressHintId,
}: ActionsHeaderProps) => {
const intl = useIntl();
return (
<div className="d-flex justify-content-end">
<IconButtonWithTooltip
tooltipPlacement="top"
tooltipContent={<div>{intl.formatMessage(messages.createNewTagTooltip)}</div>}
src={AddCircle}
alt={intl.formatMessage(messages.createTagButtonLabel)}
size="inline"
onClick={() => {
onStartDraft();
setDraftError('');
setIsCreatingTopRow(true);
setEditingRowId(null);
setActiveActionMenuRowId(null);
}}
disabled={hasOpenDraft || !canAddTag}
aria-describedby={hasOpenDraft ? draftInProgressHintId : undefined}
/>
</div>
);
};

interface ActionsMenuProps {
rowData: TagListRowData;
startSubtagDraft: () => void;
disableAddSubtag: boolean;
startEditRow: () => void;
disableEditRow: boolean;
reachedMaxDepth: (row: Row<TreeRowData>) => boolean;
startDeleteRow: (row: Row<TreeRowData>) => void;
disableDeleteRow: boolean;
row: Row<TreeRowData>;
}

const ActionsMenu = ({
rowData,
row,
startSubtagDraft,
disableAddSubtag,
startEditRow,
disableEditRow,
reachedMaxDepth,
startDeleteRow,
disableDeleteRow,
}: ActionsMenuProps) => {
const intl = useIntl();

const deleteRowMenuItem = (
<Dropdown.Item
onClick={() => startDeleteRow(row)}
disabled={disableDeleteRow}
>
{intl.formatMessage(messages.deleteTag)}
</Dropdown.Item>
);

const editRowMenuItem = (
<Dropdown.Item
onClick={startEditRow}
disabled={disableEditRow}
>
{intl.formatMessage(messages.renameTag)}
</Dropdown.Item>
);

return (
<Dropdown>
<Dropdown.Toggle
id={`dropdown-toggle-for-tag-${rowData.id}`}
as={IconButton}
src={MoreVert}
iconAs={Icon}
variant="primary"
aria-label={intl.formatMessage(messages.moreActionsForTag, { tagName: rowData.value })}
size="sm"
/>
<Dropdown.Menu>
<Dropdown.Item
onClick={startSubtagDraft}
disabled={reachedMaxDepth(row) || disableAddSubtag}
>
{intl.formatMessage(messages.addSubtag)}
</Dropdown.Item>
{editRowMenuItem}
{deleteRowMenuItem}
</Dropdown.Menu>
</Dropdown>
);
};

const Actions = {
Header: ActionsHeader,
Menu: ActionsMenu,
};

export default Actions;
Loading