Skip to content

Commit

Permalink
Implement redirects for submit/cancel on the cloud provider form
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Feb 25, 2020
1 parent 827e78d commit 684f66d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/helpers/ems_cloud_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module EmsCloudHelper
include_concern 'TextualSummary'
include_concern 'ComplianceSummaryHelper'

def edit_redirect_path(lastaction, ems)
lastaction == 'show_list' ? ems_clouds_path : ems_cloud_path(ems)
end
end
37 changes: 27 additions & 10 deletions app/javascript/forms/provider-forms/cloud-provider-form/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { componentTypes, validatorTypes } from '@data-driven-forms/react-form-re

import { API } from '../../../http_api';
import MiqFormRenderer from '../../data-driven-form';
import miqRedirectBack from '../../../helpers/miq-redirect-back';

const typeSelectorSchema = {
fields: [
Expand Down Expand Up @@ -54,7 +55,7 @@ const initialSchema = includeType => ([
},
]);

const CloudProviderForm = ({ providerId, ...props }) => {
const CloudProviderForm = ({ providerId, kind, redirect, ...props }) => {
const [{ type, schema, values }, setState] = useState({ schema: { fields: [] } });

const loadProviderSchema = (type, newValues = {}) => {
Expand All @@ -73,11 +74,12 @@ const CloudProviderForm = ({ providerId, ...props }) => {
},
values: { type, ...newValues },
});
});
}).then(miqSparkleOff);
};

useEffect(() => {
if (providerId) {
miqSparkleOn();
API.get(`/api/providers/${providerId}?attributes=endpoints,authentications,zone_name`).then(({
type,
endpoints: _endpoints,
Expand All @@ -101,21 +103,29 @@ const CloudProviderForm = ({ providerId, ...props }) => {

const typeSelected = ({ active, values: { type: newType } = {} }) => {
if (active === 'type' && type !== newType) {
miqSparkleOn();
loadProviderSchema(newType);
}
};

const onSubmit = ({ type, ..._data }, { getState }) => {
const onCancel = () => {
const message = sprintf(providerId ? __('Edit of %s was cancelled by the user') : __('Add of %s was cancelled by the user'), kind);
miqRedirectBack(message, 'success', redirect);
};

const onSubmit = (_data, { getState }) => {
miqSparkleOn();

const message = sprintf(__('%s %s was saved'), kind, values.name);

// Retrieve fields from the schema, but omit the validator components as the API doesn't like them
const fields = Object.keys(getState().modified).filter(field => !field.match(/^authentications\.[^.]+\.valid$/));
// Filter out fields that are not available in the form schema
const data = _.pick(_data, fields);
const data = { ..._.pick(_data, fields), ddf: true };

if (providerId) {
API.patch(`/api/providers/${providerId}`, { ...data, ddf: true });
} else {
API.post('/api/providers', { ...data, type, ddf: true });
}
const request = providerId ? API.patch(`/api/providers/${providerId}`, data) : API.post('/api/providers', { type, ...data });

request.then(() => miqRedirectBack(message, 'success', redirect)).catch(miqSparkleOff);
};

return (
Expand All @@ -129,7 +139,14 @@ const CloudProviderForm = ({ providerId, ...props }) => {
/>
) }
<EditingContext.Provider value={providerId}>
<MiqFormRenderer schema={schema} onSubmit={onSubmit} initialValues={values} clearedValue={null} />
<MiqFormRenderer
schema={schema}
onSubmit={onSubmit}
onCancel={onCancel}
initialValues={values}
clearedValue={null}
clearOnUnmount
/>
</EditingContext.Provider>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion app/views/ems_cloud/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= react('CloudProviderForm', :providerId => @ems.id)
= react('CloudProviderForm', :providerId => @ems.id, :redirect => edit_redirect_path(@lastaction, @ems), :kind => ui_lookup(:model => 'ManageIQ::Providers::CloudManager'))

= form_for(@ems,
:url => ems_cloud_path(@ems),
Expand Down
2 changes: 1 addition & 1 deletion app/views/ems_cloud/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- url = @ems.persisted? ? ems_clouds_path(@ems) : ems_clouds_path

= react('CloudProviderForm')
= react('CloudProviderForm', :redirect => ems_clouds_path, :kind => ui_lookup(:model => 'ManageIQ::Providers::CloudManager'))

= form_for(@ems,
:url => url,
Expand Down

0 comments on commit 684f66d

Please sign in to comment.