@@ -3,7 +3,7 @@ import { AxiosError } from 'axios';
33import { useIntl } from '@edx/frontend-platform/i18n' ;
44
55import globalMessages from '@src/messages' ;
6- import { useCreateTag , useUpdateTag } from '@src/taxonomy/data/apiHooks' ;
6+ import { useCreateTag , useDeleteTag , useUpdateTag } from '@src/taxonomy/data/apiHooks' ;
77import type { RowId , TreeRowData } from '@src/taxonomy/tree-table/types' ;
88import { TagTree } from './tagTree' ;
99import { TagListTableError } from './errors' ;
@@ -56,6 +56,7 @@ interface UseEditActionsParams {
5656 setActiveActionMenuRowId : React . Dispatch < React . SetStateAction < RowId | null > > ;
5757 setConfirmDeleteDialogOpen : React . Dispatch < React . SetStateAction < boolean > > ;
5858 setConfirmDeleteDialogContext : React . Dispatch < React . SetStateAction < Row < TreeRowData > | null > > ;
59+ deleteTagMutation : ReturnType < typeof useDeleteTag > ;
5960}
6061
6162const getInlineValidationMessage = ( value : string , intl : ReturnType < typeof useIntl > ) : string => {
@@ -131,6 +132,7 @@ const useEditActions = ({
131132 exitDraftWithoutSave,
132133 setEditingRowId,
133134 updateTagMutation,
135+ deleteTagMutation,
134136 setConfirmDeleteDialogOpen,
135137 setConfirmDeleteDialogContext,
136138} : UseEditActionsParams ) => {
@@ -287,18 +289,18 @@ const useEditActions = ({
287289 setConfirmDeleteDialogContext ( row ) ;
288290 } ;
289291
290- const someDeleteTagAPICall = async ( value : string ) => {
291- // Placeholder for actual delete API call
292- return new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
293- } ;
294-
295292 const handleDeleteTag = async ( row : Row < TreeRowData > ) => {
296293 const rowData = getTagListRowData ( row ) ;
297294 const count = getTagWithDescendantsCount ( rowData ) ;
295+ // If the tag in the frontend state does not have subtags,
296+ // don't allow the backend to delete subtags.
297+ // That prevents problems in case of stale frontend state.
298+ const shouldDeleteSubtags = count > 1 ;
298299 try {
299- // In view mode, the table reloads on change, reflecting the deletion without needing to manually update the table state
300+ // In view mode, the table reloads on change, reflecting the deletion
301+ // without needing to manually update the table state
300302 enterViewMode ( ) ;
301- await someDeleteTagAPICall ( rowData . value ) ; // Replace with actual delete API call
303+ await deleteTagMutation . mutateAsync ( { value : rowData . value , withSubtags : shouldDeleteSubtags } ) ;
302304 setToast ( {
303305 show : true ,
304306 message : intl . formatMessage ( messages . tagsDeleteSuccessMessage , { count } ) ,
0 commit comments