From 684f66d73738f2111e960466239738a68898e464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Hal=C3=A1sz?= Date: Tue, 25 Feb 2020 14:36:01 +0100 Subject: [PATCH] Implement redirects for submit/cancel on the cloud provider form --- app/helpers/ems_cloud_helper.rb | 4 ++ .../cloud-provider-form/index.jsx | 37 ++++++++++++++----- app/views/ems_cloud/edit.html.haml | 2 +- app/views/ems_cloud/new.html.haml | 2 +- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/app/helpers/ems_cloud_helper.rb b/app/helpers/ems_cloud_helper.rb index 7b175c6ba5c0..60c6eb53f915 100644 --- a/app/helpers/ems_cloud_helper.rb +++ b/app/helpers/ems_cloud_helper.rb @@ -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 diff --git a/app/javascript/forms/provider-forms/cloud-provider-form/index.jsx b/app/javascript/forms/provider-forms/cloud-provider-form/index.jsx index 1a58dfed8233..a447b484d7ea 100644 --- a/app/javascript/forms/provider-forms/cloud-provider-form/index.jsx +++ b/app/javascript/forms/provider-forms/cloud-provider-form/index.jsx @@ -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: [ @@ -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 = {}) => { @@ -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, @@ -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 ( @@ -129,7 +139,14 @@ const CloudProviderForm = ({ providerId, ...props }) => { /> ) } - + ); diff --git a/app/views/ems_cloud/edit.html.haml b/app/views/ems_cloud/edit.html.haml index a718cfbd85bf..2f5258fae005 100644 --- a/app/views/ems_cloud/edit.html.haml +++ b/app/views/ems_cloud/edit.html.haml @@ -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), diff --git a/app/views/ems_cloud/new.html.haml b/app/views/ems_cloud/new.html.haml index 58d5e303dd71..cad026bff0bb 100644 --- a/app/views/ems_cloud/new.html.haml +++ b/app/views/ems_cloud/new.html.haml @@ -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,