Sha256: 1ea17679d67834b1c66e8255177caaa4685174b1d421ef9858bde7f11c419a80

Contents?: true

Size: 1.85 KB

Versions: 26

Compression:

Stored size: 1.85 KB

Contents

import React from 'react';
import { get } from 'foremanReact/redux/API';
import { withInterval } from 'foremanReact/redux/middlewares/IntervalMiddleware';
import { addToast } from 'foremanReact/redux/actions/toasts';
import { translate as __ } from 'foremanReact/common/I18n';
import { foremanTaskDetailsUrl } from './ForemanTasksHelpers';
import { foremanUrl } from '../../ForemanRhCloudHelpers';

export const setupTaskPolling = ({
  taskId,
  key,
  onTaskSuccess,
  onTaskError,
  taskErrorMessage,
  dispatch,
}) =>
  withInterval(
    get({
      key,
      url: foremanTaskDetailsUrl(taskId),
      handleSuccess: ({ data }, stopTaskInterval) => {
        if (data.result === 'success') {
          stopTaskInterval();
          onTaskSuccess(data, dispatch);
        }
        if (data.result === 'error') {
          stopTaskInterval();
          if (taskErrorMessage === undefined) {
            taskErrorMessage = errorData =>
              `${__('The task failed with the following error:')} ${
                errorData.humanized.errors
              }`;
          }
          if (onTaskError === undefined) {
            onTaskError = errorData =>
              dispatch(defaultTaskErrorHandler(errorData, taskErrorMessage));
          }
          onTaskError(data, dispatch);
        }
      },
      errorToast: error => `Could not get task details: ${error}`,
    })
  );

export const taskRelatedToast = (taskID, type, message) =>
  addToast({
    type,
    message: (
      <span>
        {message}
        <br />
        <a
          target="_blank"
          rel="noopener noreferrer"
          href={foremanUrl(`/foreman_tasks/tasks/${taskID}`)}
        >
          {__('view the task page for more details')}
        </a>
      </span>
    ),
  });

const defaultTaskErrorHandler = (data, taskErrorMessage) =>
  taskRelatedToast(data.id, 'error', taskErrorMessage(data));

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
foreman_rh_cloud-4.0.36 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.33 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.35 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.32 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.32 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.31 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.30 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.29 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.29 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-5.0.29 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.28 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-5.0.28 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.27 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.26 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.26 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.25.1 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.25 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.25 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-3.0.24.1 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-4.0.24.1 webpack/common/ForemanTasks/ForemanTasksActions.js