diff --git a/packages/react-components/src/Accounts/Accounts.tsx b/packages/react-components/src/Accounts/Accounts.tsx index 4d128d1b..756f3069 100644 --- a/packages/react-components/src/Accounts/Accounts.tsx +++ b/packages/react-components/src/Accounts/Accounts.tsx @@ -128,45 +128,37 @@ const Accounts: React.FC = ({ host, token, userGroupsDefaultSele setRows(changedRows); }; - const handleSubmit = (row, isNew = false) => { - const handleError = (error, action) => { - try { - if (error instanceof Error) { - const errorMessage = error.message; - - const parsedError = JSON.parse(errorMessage); - const firstErrorMessage = parsedError.errors[0]?.description; - setErrorMessage(firstErrorMessage); - } else { - console.log(`${action}: Received non-Error object:`, error); - } - } catch (e) { - console.log(`${action}: Failed to parse error response.`, e); + const handleError = (error, action) => { + try { + if (error instanceof Error) { + const errorMessage = error.message; + const parsedError = JSON.parse(errorMessage); + const firstErrorMessage = parsedError.errors[0]?.description; + setErrorMessage(firstErrorMessage); + } else { + console.log(`${action}: Received non-Error object:`, error); } + } catch (e) { + console.log(`${action}: Failed to parse error response.`, e); + } + }; + + const handleSubmit = (row, isNew = false) => { + const onSuccess = () => { + setErrorMessage(''); + fetchData(); }; - + + const onError = (error) => { + handleError(error, isNew ? 'Create Account' : 'Update Account'); + }; + if (isNew) { - return createAccount(host, token, { ...row }).subscribe( - () => { - setErrorMessage(''); - fetchData(); - }, - (error) => { - handleError(error, 'Create Account'); - } - ); + return createAccount(host, token, { ...row }).subscribe(onSuccess, onError); } else { - return updateAccount(host, token, { ...row }).subscribe( - () => { - setErrorMessage(''); - fetchData(); - }, - (error) => { - handleError(error, 'Update Account'); - } - ); + return updateAccount(host, token, { ...row }).subscribe(onSuccess, onError); } - }; + }; const handleDelete = (row) => { deleteAccount(host, token, row.id).subscribe( diff --git a/packages/react-components/src/common/Table/PopupEditing.tsx b/packages/react-components/src/common/Table/PopupEditing.tsx index a9225967..f4d6c93b 100644 --- a/packages/react-components/src/common/Table/PopupEditing.tsx +++ b/packages/react-components/src/common/Table/PopupEditing.tsx @@ -129,17 +129,15 @@ const PopupEditing = React.memo( if (isNew) { commitAddedRows({ rowIds }); - onSave(editedRow, isNew); - } else { - onSave(editedRow); - if (errorMessage !== '') { - onSave(editedRow); - stopEditRows({ rowIds }); - commitChangedRows({ rowIds }); - } else { - onSave(editedRow); - } - } + } + + if (errorMessage !== '') { + stopEditRows({ rowIds }); + commitChangedRows({ rowIds }); + } + + onSave(editedRow, isNew); + } catch (error) { console.error('Error applying changes:', error); cancelChanges();