Sha256: fbee7c04812413467c6373c2e9030dacdbdc63ed082b30f9e7d1b6e460fd212d

Contents?: true

Size: 1.85 KB

Versions: 47

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/components/ToastsList';
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

47 entries across 47 versions & 1 rubygems

Version Path
foreman_rh_cloud-9.0.59 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-11.0.2 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-11.0.1 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-11.0.0 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.58 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-10.0.2 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.57 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-10.0.1 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.56 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.55 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.54 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.53 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.52 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-8.0.52 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-9.0.51 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-8.0.51 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-7.0.48 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-7.0.47 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-8.0.50 webpack/common/ForemanTasks/ForemanTasksActions.js
foreman_rh_cloud-8.0.49 webpack/common/ForemanTasks/ForemanTasksActions.js