diff --git a/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/BulkBuildHostModal.js b/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/BulkBuildHostModal.js index 3bcc13d40a6..6066c25e4ef 100644 --- a/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/BulkBuildHostModal.js +++ b/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/BulkBuildHostModal.js @@ -11,14 +11,11 @@ import { Radio, } from '@patternfly/react-core'; import { addToast } from '../../../ToastsList/slice'; -import { actionTypeGenerator } from '../../../../redux/API/APIActionTypeGenerator'; import { translate as __ } from '../../../../common/I18n'; -import { urlWithSearch } from '../../../../common/urlHelpers'; - +import { failedHostsToastParams } from '../helpers'; import { STATUS } from '../../../../constants'; import { selectAPIStatus } from '../../../../redux/API/APISelectors'; import { bulkBuildHosts, HOST_BUILD_KEY } from './actions'; -import { foremanUrl } from '../../../../common/helpers'; const BulkBuildHostModal = ({ isOpen, @@ -39,31 +36,12 @@ const BulkBuildHostModal = ({ closeModal(); }; - const searchLink = ({ query, message, baseUrl }) => { - return { - children: message, - href: urlWithSearch(baseUrl, query), - }; - }; - const handleError = ({ response }) => { handleModalClose(); - const { FAILURE } = actionTypeGenerator(HOST_BUILD_KEY); - const data = response.data.error; - const query = `id ^ (${data.failed_host_ids.join(',')})`; - const hostsBaseUrl = foremanUrl('new/hosts'); - dispatch( - addToast({ - type: 'danger', - link: searchLink({ - query, - message: __('Failed hosts'), - baseUrl: hostsBaseUrl, - }), - message: data.message, - key: FAILURE, - }) + addToast( + failedHostsToastParams({ ...response.data.error, key: HOST_BUILD_KEY }) + ) ); }; const handleSave = () => { diff --git a/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/actions.js b/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/actions.js index c62d9e8e09c..23c72b932d1 100644 --- a/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/actions.js +++ b/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/buildHosts/actions.js @@ -1,4 +1,3 @@ -import { translate as __ } from '../../../../common/I18n'; import { APIActions } from '../../../../redux/API'; import { foremanUrl } from '../../../../common/helpers'; diff --git a/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/helpers.js b/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/helpers.js new file mode 100644 index 00000000000..62181bbd6a7 --- /dev/null +++ b/webpack/assets/javascripts/react_app/components/HostsIndex/BulkActions/helpers.js @@ -0,0 +1,34 @@ +import { actionTypeGenerator } from '../../../redux/API/APIActionTypeGenerator'; +import { translate as __ } from '../../../common/I18n'; +import { urlWithSearch } from '../../../common/urlHelpers'; +import { foremanUrl } from '../../../common/helpers'; + +export const searchLink = ({ query, message, baseUrl }) => { + return { + children: message, + href: urlWithSearch(baseUrl, query), + }; +}; + +export const failedHostsToastParams = ({ + message, + failed_host_ids: failedHostIds, + key, +}) => { + const { FAILURE } = actionTypeGenerator(key); + const toastParams = { + type: 'danger', + message, + key: FAILURE, + }; + if (failedHostIds) { + const query = `id ^ (${failedHostIds.join(',')})`; + toastParams.link = searchLink({ + query, + message: __('Failed hosts'), + baseUrl: foremanUrl('new/hosts'), + }); + } + + return toastParams; +};