webpack/ForemanTasks/Components/TasksTable/TasksBulkActions.js in foreman-tasks-1.1.3 vs webpack/ForemanTasks/Components/TasksTable/TasksBulkActions.js in foreman-tasks-1.2.0

- old
+ new

@@ -1,26 +1,37 @@ import API from 'foremanReact/API'; import { addToast } from 'foremanReact/redux/actions/toasts'; -import { translate as __ } from 'foremanReact/common/I18n'; -import { TOAST_TYPES } from '../common/ToastTypesConstants'; +import { translate as __, sprintf } from 'foremanReact/common/I18n'; import { + BULK_CANCEL_PATH, + BULK_RESUME_PATH, + BULK_FORCE_CANCEL_PATH, +} from './TasksTableConstants'; +import { TASKS_RESUME_REQUEST, TASKS_RESUME_SUCCESS, TASKS_RESUME_FAILURE, TASKS_CANCEL_REQUEST, TASKS_CANCEL_SUCCESS, TASKS_CANCEL_FAILURE, - BULK_CANCEL_PATH, - BULK_RESUME_PATH, -} from './TasksTableConstants'; + TASKS_FORCE_CANCEL_REQUEST, + TASKS_FORCE_CANCEL_SUCCESS, + TASKS_FORCE_CANCEL_FAILURE, +} from '../TaskActions/TaskActionsConstants'; import { reloadPage } from './TasksTableActions'; import { convertDashboardQuery, resumeToastInfo, cancelToastInfo, toastDispatch, -} from './TasksTableActionHelpers'; +} from '../TaskActions/TaskActionHelpers'; +import { + successToastData, + errorToastData, + warningToastData, + infoToastData, +} from '../common/ToastsHelpers'; export const bulkByIdRequest = (resumeTasks, path) => { const ids = resumeTasks.map(task => task.id); const url = `/foreman_tasks/api/tasks/${path}`; const data = { task_ids: ids }; @@ -39,14 +50,13 @@ }; const handleErrorResume = (error, dispatch) => { dispatch({ type: TASKS_RESUME_FAILURE, error }); dispatch( - addToast({ - type: TOAST_TYPES.ERROR, - message: `${__(`Cannot resume tasks at the moment`)} ${error}`, - }) + addToast( + errorToastData(`${__(`Cannot resume tasks at the moment`)} ${error}`) + ) ); }; export const bulkResumeById = ({ selected, @@ -54,14 +64,13 @@ parentTaskID, }) => async dispatch => { const resumeTasks = selected.filter(task => task.isResumable); if (resumeTasks.length < selected.length) dispatch( - addToast({ - type: TOAST_TYPES.WARNING, - message: __('Not all the selected tasks can be resumed'), - }) + addToast( + warningToastData(__('Not all the selected tasks can be resumed')) + ) ); if (resumeTasks.length) { dispatch({ type: TASKS_RESUME_REQUEST }); try { const { data } = await bulkByIdRequest(resumeTasks, BULK_RESUME_PATH); @@ -90,38 +99,35 @@ query, parentTaskID, }) => async dispatch => { dispatch({ type: TASKS_RESUME_REQUEST }); dispatch( - addToast({ - type: 'info', - message: __('Resuming selected tasks, this might take a while'), - }) + addToast( + infoToastData(__('Resuming selected tasks, this might take a while')) + ) ); await bulkBySearchRequest({ query, path: BULK_RESUME_PATH, parentTaskID }); }; const handleErrorCancel = (error, dispatch) => { dispatch({ type: TASKS_CANCEL_FAILURE, error }); dispatch( - addToast({ - type: TOAST_TYPES.ERROR, - message: `${__(`Cannot cancel tasks at the moment`)} ${error}`, - }) + addToast( + errorToastData(`${__(`Cannot cancel tasks at the moment`)} ${error}`) + ) ); }; export const bulkCancelBySearch = ({ query, parentTaskID, }) => async dispatch => { dispatch({ type: TASKS_CANCEL_REQUEST }); dispatch( - addToast({ - type: 'info', - message: __('Canceling selected tasks, this might take a while'), - }) + addToast( + infoToastData(__('Canceling selected tasks, this might take a while')) + ) ); await bulkBySearchRequest({ query, path: BULK_CANCEL_PATH, parentTaskID }); }; export const bulkCancelById = ({ @@ -130,14 +136,13 @@ parentTaskID, }) => async dispatch => { const cancelTasks = selected.filter(task => task.isCancellable); if (cancelTasks.length < selected.length) dispatch( - addToast({ - type: TOAST_TYPES.WARNING, - message: __('Not all the selected tasks can be cancelled'), - }) + addToast( + warningToastData(__('Not all the selected tasks can be cancelled')) + ) ); if (cancelTasks.length) { dispatch({ type: TASKS_CANCEL_REQUEST }); try { const { data } = await bulkByIdRequest(cancelTasks, BULK_CANCEL_PATH); @@ -159,6 +164,84 @@ } } catch (error) { handleErrorCancel(error, dispatch); } } +}; + +const handleErrorForceCancel = (error, dispatch) => { + dispatch({ type: TASKS_FORCE_CANCEL_FAILURE, error }); + dispatch( + addToast( + errorToastData( + `${__(`Cannot force cancel tasks at the moment`)} ${error}` + ) + ) + ); +}; + +export const bulkForceCancelById = ({ + selected, + url, + parentTaskID, +}) => async dispatch => { + const stopTasks = selected.filter(task => task.state !== 'stopped'); + if (stopTasks.length < selected.length) + dispatch( + addToast( + warningToastData( + sprintf( + '%s task(s) are already stopped', + selected.length - stopTasks.length + ) + ) + ) + ); + if (stopTasks.length > 0) { + dispatch({ type: TASKS_FORCE_CANCEL_REQUEST }); + try { + const { data } = await bulkByIdRequest(stopTasks, BULK_FORCE_CANCEL_PATH); + dispatch({ type: TASKS_FORCE_CANCEL_SUCCESS }); + if (data.stopped_length) { + dispatch( + addToast( + successToastData( + sprintf('%s task(s) cancelled with force', data.stopped_length) + ) + ) + ); + } + if (data.skipped_length > 0) + dispatch( + addToast( + warningToastData( + sprintf('%s task(s) are already stopped', data.skipped_length) + ) + ) + ); + if (data.stopped_length > 0) { + reloadPage(url, parentTaskID, dispatch); + } + } catch (error) { + handleErrorForceCancel(error, dispatch); + } + } +}; + +export const bulkForceCancelBySearch = ({ + query, + parentTaskID, +}) => async dispatch => { + dispatch({ type: TASKS_FORCE_CANCEL_REQUEST }); + dispatch( + addToast( + infoToastData( + __('Canceling with force selected tasks, this might take a while') + ) + ) + ); + await bulkBySearchRequest({ + query, + path: BULK_FORCE_CANCEL_PATH, + parentTaskID, + }); };