|
1 | 1 | import { useReducer } from 'react'; |
2 | 2 | import { useIntl } from '@edx/frontend-platform/i18n'; |
3 | 3 |
|
4 | | -import { useCreateTag } from '../data/apiHooks'; |
| 4 | +import { useCreateTag, useUpdateTag } from '../data/apiHooks'; |
5 | 5 | import { TagTree } from './tagTree'; |
6 | 6 | import { TagListTableError } from './errors'; |
7 | 7 | import type { RowId } from '../tree-table/types'; |
@@ -45,6 +45,7 @@ interface UseEditActionsParams { |
45 | 45 | setCreatingParentId: React.Dispatch<React.SetStateAction<RowId | null>>; |
46 | 46 | exitDraftWithoutSave: () => void; |
47 | 47 | setEditingRowId: React.Dispatch<React.SetStateAction<RowId | null>>; |
| 48 | + updateTagMutation: ReturnType<typeof useUpdateTag>; |
48 | 49 | } |
49 | 50 |
|
50 | 51 | const getInlineValidationMessage = (value: string, intl: ReturnType<typeof useIntl>): string => { |
@@ -111,9 +112,20 @@ const useEditActions = ({ |
111 | 112 | setToast, |
112 | 113 | setIsCreatingTopTag, |
113 | 114 | setCreatingParentId, |
| 115 | + exitDraftWithoutSave, |
114 | 116 | setEditingRowId, |
| 117 | + updateTagMutation, |
115 | 118 | }: UseEditActionsParams) => { |
116 | 119 | const intl = useIntl(); |
| 120 | + |
| 121 | + const updateTableAfterRename = (oldValue: string, newValue: string) => { |
| 122 | + setTagTree((currentTagTree) => { |
| 123 | + const nextTree = currentTagTree || new TagTree([]); |
| 124 | + nextTree.editTagValue(oldValue, newValue); |
| 125 | + return nextTree; |
| 126 | + }); |
| 127 | + }; |
| 128 | + |
117 | 129 | const updateTableWithoutDataReload = (value: string, parentTagValue: string | null = null) => { |
118 | 130 | setTagTree((currentTagTree) => { |
119 | 131 | const nextTree = currentTagTree || new TagTree([]); |
@@ -178,14 +190,31 @@ const useEditActions = ({ |
178 | 190 |
|
179 | 191 | const handleUpdateTag = async (value: string, originalValue: string) => { |
180 | 192 | const trimmed = value.trim(); |
181 | | - if (trimmed && trimmed !== originalValue) { |
| 193 | + if (!validate(trimmed, 'soft')) { |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + if (trimmed === originalValue) { |
| 198 | + setEditingRowId(null); |
| 199 | + exitDraftWithoutSave(); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + try { |
| 204 | + setDraftError(''); |
| 205 | + await updateTagMutation.mutateAsync({ value: trimmed, originalValue }); |
| 206 | + updateTableAfterRename(originalValue, trimmed); |
182 | 207 | enterPreviewMode(); |
| 208 | + setEditingRowId(null); |
183 | 209 | setToast({ |
184 | 210 | show: true, |
185 | 211 | message: intl.formatMessage(messages.tagUpdateSuccessMessage, { name: trimmed }), |
186 | 212 | }); |
| 213 | + } catch (error) { |
| 214 | + const message = intl.formatMessage(messages.tagUpdateErrorMessage, { errorMessage: (error as Error)?.message }); |
| 215 | + setDraftError((error as Error)?.message || intl.formatMessage(messages.tagUpdateErrorMessage, { errorMessage: '' })); |
| 216 | + setToast({ show: true, message }); |
187 | 217 | } |
188 | | - setEditingRowId(null); |
189 | 218 | }; |
190 | 219 |
|
191 | 220 | return { |
|
0 commit comments