Sha256: 3df2623610ab2162732a4d201990ad67dfe05ed093abbd158b10f083c23a4b53

Contents?: true

Size: 1.55 KB

Versions: 21

Compression:

Stored size: 1.55 KB

Contents

import { useMutation } from '@apollo/client';
import { translate as __, sprintf } from 'foremanReact/common/I18n';

import { useCurrentPagination, pageToVars } from './pageParamsHelper';

const formatError = (error, name) =>
  sprintf(__('There was a following error when deleting %(name)s: %(error)s'), {
    name,
    error,
  });

const joinErrors = errors => errors.map(err => err.message).join(', ');

const onError = (showToast, resourceName) => error => {
  showToast({ type: 'error', message: formatError(error, resourceName) });
};

const onCompleted = (
  toggleModal,
  showToast,
  mutationName,
  successMsg,
  resourceName
) => data => {
  toggleModal();
  const { errors } = data[mutationName];
  if (Array.isArray(errors) && errors.length > 0) {
    showToast({
      type: 'error',
      message: formatError(joinErrors(errors), resourceName),
    });
  } else {
    showToast({
      type: 'success',
      message: successMsg,
    });
  }
};

export const prepareMutation = (
  history,
  showToast,
  refetchQuery,
  mutationName,
  successMsg,
  mutation,
  resourceName
) => toggleModal => {
  const pagination = pageToVars(useCurrentPagination(history));

  const options = {
    refetchQueries: [{ query: refetchQuery, variables: pagination }],
    onCompleted: onCompleted(
      toggleModal,
      showToast,
      mutationName,
      successMsg,
      resourceName
    ),
    onError: onError(showToast, resourceName),
  };

  return useMutation(mutation, options);
};

export const submitDelete = (mutation, id) => {
  mutation({ variables: { id } });
};

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
foreman_openscap-10.1.0 webpack/helpers/mutationHelper.js
foreman_openscap-10.0.3 webpack/helpers/mutationHelper.js
foreman_openscap-10.0.2 webpack/helpers/mutationHelper.js
foreman_openscap-10.0.1 webpack/helpers/mutationHelper.js
foreman_openscap-10.0.0 webpack/helpers/mutationHelper.js
foreman_openscap-9.0.4 webpack/helpers/mutationHelper.js
foreman_openscap-9.0.3 webpack/helpers/mutationHelper.js
foreman_openscap-9.0.2 webpack/helpers/mutationHelper.js
foreman_openscap-9.0.1 webpack/helpers/mutationHelper.js
foreman_openscap-9.0.0 webpack/helpers/mutationHelper.js
foreman_openscap-8.0.0 webpack/helpers/mutationHelper.js
foreman_openscap-7.1.1 webpack/helpers/mutationHelper.js
foreman_openscap-7.1.0 webpack/helpers/mutationHelper.js
foreman_openscap-7.0.0 webpack/helpers/mutationHelper.js
foreman_openscap-6.0.0 webpack/helpers/mutationHelper.js
foreman_openscap-5.2.3 webpack/helpers/mutationHelper.js
foreman_openscap-5.2.2 webpack/helpers/mutationHelper.js
foreman_openscap-5.1.1 webpack/helpers/mutationHelper.js
foreman_openscap-5.2.1 webpack/helpers/mutationHelper.js
foreman_openscap-5.2.0 webpack/helpers/mutationHelper.js