Skip to content

Commit

Permalink
Extract actionFunction into an own JS module
Browse files Browse the repository at this point in the history
Several hooks will use this function. Therefore move it to an own JS
module.
  • Loading branch information
bjoernricks committed Mar 7, 2025
1 parent c762650 commit fae226f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
26 changes: 1 addition & 25 deletions src/web/entity/EntityComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,11 @@
import {isDefined} from 'gmp/utils/identity';
import {useDispatch} from 'react-redux';
import useEntityDownload from 'web/entity/hooks/useEntityDownload';
import {actionFunction} from 'web/entity/hooks/utils';
import useGmp from 'web/hooks/useGmp';
import {createDeleteEntity} from 'web/store/entities/utils/actions';
import PropTypes from 'web/utils/PropTypes';

/**
* Executes a promise and handles success and error callbacks.
*
* @param {Promise} promise - The promise to be executed.
* @param {Function} [onSuccess] - Optional callback function to be called on successful resolution of the promise.
* @param {Function} [onError] - Optional callback function to be called if the promise is rejected.
* @returns {Promise<*>} - The result of the onSuccess callback if provided, otherwise the resolved value of the promise.
* If the promise is rejected the result of the onError callback if provided.
* Otherwise the error from the rejected promise is thrown.
* @throws {*} - The error from the rejected promise if onError callback is not provided.
*/
const actionFunction = async (promise, onSuccess, onError) => {
try {
const response = await promise;
if (isDefined(onSuccess)) {
return onSuccess(response);
}
} catch (error) {
if (isDefined(onError)) {
return onError(error);
}
throw error;
}
};

const EntityComponent = ({
children,
name,
Expand Down
31 changes: 31 additions & 0 deletions src/web/entity/hooks/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {isDefined} from 'gmp/utils/identity';

/**
* Executes a promise and handles success and error callbacks.
*
* @param {Promise} promise - The promise to be executed.
* @param {Function} [onSuccess] - Optional callback function to be called on successful resolution of the promise.
* @param {Function} [onError] - Optional callback function to be called if the promise is rejected.
* @returns {Promise<*>} - The result of the onSuccess callback if provided, otherwise the resolved value of the promise.
* If the promise is rejected the result of the onError callback if provided.
* Otherwise the error from the rejected promise is thrown.
* @throws {*} - The error from the rejected promise if onError callback is not provided.
*/
export const actionFunction = async (promise, onSuccess, onError) => {
try {
const response = await promise;
if (isDefined(onSuccess)) {
return onSuccess(response);
}
} catch (error) {
if (isDefined(onError)) {
return onError(error);
}
throw error;
}
};

0 comments on commit fae226f

Please sign in to comment.