<commit_before>import React, { useState, useMemo, useEffect } from 'react';
import PropTypes from 'prop-types';

import MiqFormRenderer from '@@ddf';
import createSchema from './cloud-volume-form.schema';
import miqRedirectBack from '../../helpers/miq-redirect-back';

const CloudVolumeForm = ({ recordId, storageManagerId }) => {
  const [{ fields, initialValues, isLoading }, setState] = useState({ fields: [], isLoading: !!recordId || !!storageManagerId });
  const submitLabel = !!recordId ? __('Save') : __('Add');

  const loadSchema = (appendState = {}) => ({ data: { form_schema: { fields } } }) => {
    setState((state) => ({
      ...state,
      ...appendState,
      fields,
    }));
  };

  useEffect(() => {
    if (recordId) {
      API.get(`/api/cloud_volumes/${recordId}`).then((initialValues) => {
        API.options(`/api/cloud_volumes?ems_id=${initialValues.ems_id}`).then(loadSchema({ initialValues, isLoading: false }));
      });
    }
    if (storageManagerId) {
      API.options(`/api/cloud_volumes?ems_id=${storageManagerId}`).then(loadSchema({ initialValues: { ems_id: storageManagerId }, isLoading: false }));
    }
  }, [recordId, storageManagerId]);

  const onSubmit = ({ edit: _edit, ...values }) => {
    miqSparkleOn();

    const request = recordId ? API.patch(`/api/cloud_volumes`, values) : API.post('/api/cloud_volumes', values);
    request.then(() => {
      const message = sprintf(
        recordId
          ? __('Modification of Cloud Volume "%s" has been successfully queued.')
          : __('Add of Cloud Volume "%s" has been successfully queued.'),
        values.name,
      );
      miqRedirectBack(message, undefined, '/cloud_volume/show_list');
    }).catch(miqSparkleOff);
  };

  const onCancel = () => {
    const message = sprintf(
      recordId
        ? __('Edit of Cloud Volume "%s" was canceled by the user.')
        : __('Add of new Cloud Volume was cancelled by the user.'),
      initialValues && initialValues.name,
    );
    miqRedirectBack(message, 'warning', '/cloud_volume/show_list');
  };<commit_msg>Fix edit cloud volume api call and pass record id<commit_after>import React, { useState, useMemo, useEffect } from 'react';
import PropTypes from 'prop-types';

import MiqFormRenderer from '@@ddf';
import createSchema from './cloud-volume-form.schema';
import miqRedirectBack from '../../helpers/miq-redirect-back';

const CloudVolumeForm = ({ recordId, storageManagerId }) => {
  const [{ fields, initialValues, isLoading }, setState] = useState({ fields: [], isLoading: !!recordId || !!storageManagerId });
  const submitLabel = !!recordId ? __('Save') : __('Add');

  const loadSchema = (appendState = {}) => ({ data: { form_schema: { fields } } }) => {
    setState((state) => ({
      ...state,
      ...appendState,
      fields,
    }));
  };

  useEffect(() => {
    if (recordId) {
      API.get(`/api/cloud_volumes/${recordId}`).then((initialValues) => {
        API.options(`/api/cloud_volumes?ems_id=${initialValues.ems_id}`).then(loadSchema({ initialValues, isLoading: false }));
      });
    }
    if (storageManagerId) {
      API.options(`/api/cloud_volumes?ems_id=${storageManagerId}`).then(loadSchema({ initialValues: { ems_id: storageManagerId }, isLoading: false }));
    }
  }, [recordId, storageManagerId]);

  const onSubmit = ({ edit: _edit, ...values }) => {
    miqSparkleOn();

    const request = recordId ? API.patch(`/api/cloud_volumes/${recordId}`, values) : API.post('/api/cloud_volumes', values);
    request.then(() => {
      const message = sprintf(
        recordId
          ? __('Modification of Cloud Volume "%s" has been successfully queued.')
          : __('Add of Cloud Volume "%s" has been successfully queued.'),
        values.name,
      );
      miqRedirectBack(message, undefined, '/cloud_volume/show_list');
    }).catch(miqSparkleOff);
  };

  const onCancel = () => {
    const message = sprintf(
      recordId
        ? __('Edit of Cloud Volume "%s" was canceled by the user.')
        : __('Add of new Cloud Volume was cancelled by the user.'),
      initialValues && initialValues.name,
    );
    miqRedirectBack(message, 'warning', '/cloud_volume/show_list');
  };
{'fraction_non_alphanumeric': 0.16714285714285715, 'fraction_numerical': 0.0, 'mean_word_length': 3.389759665621735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2363905', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1274, 'n_words': 293}