Skip to content

Commit 9761b10

Browse files
Merge pull request #9188 from liu-samuel/provision-dialog-conversion
Provision Dialog Form conversion from HAML to React
2 parents f3dc555 + 5947d4f commit 9761b10

File tree

11 files changed

+644
-42
lines changed

11 files changed

+644
-42
lines changed

app/controllers/miq_ae_customization_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ def handle_bottom_cell(presenter)
318318
}
319319
presenter.update(:form_buttons_div, render_proc[:partial => "layouts/x_edit_buttons", :locals => locals])
320320
presenter.remove_paging.show(:form_buttons_div)
321+
if @hide_bottom_bar
322+
presenter.hide(:form_buttons_div)
323+
end
321324
end
322325
presenter.show(:paging_div)
323326
else

app/controllers/miq_ae_customization_controller/old_dialogs.rb

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,6 @@ def old_dialogs_get_form_vars
1111
@edit[:new][:content] = @edit[:new][:content] + "..." if !params[:name] && !params[:description] && !params[:dialog_type] && !params[:content_data]
1212
end
1313

14-
# Set form variables for edit
15-
def old_dialogs_set_form_vars
16-
@edit = {}
17-
@edit[:dialog] = @dialog
18-
19-
@edit[:new] = {}
20-
@edit[:current] = {}
21-
@edit[:key] = "dialog_edit__#{@dialog.id || "new"}"
22-
23-
@edit[:new][:name] = @dialog.name
24-
@edit[:new][:description] = @dialog.description
25-
@edit[:new][:dialog_type] = if @dialog.dialog_type
26-
@dialog.dialog_type
27-
else
28-
# if new customization dialogs, check if add button was pressed form folder level, to auto select image type
29-
x_node == "root" ? @dialog.dialog_type : x_node.split('_')[1]
30-
end
31-
32-
@edit[:new][:content] = @dialog.content.to_yaml
33-
@edit[:current] = copy_hash(@edit[:new])
34-
session[:edit] = @edit
35-
end
36-
3714
def old_dialogs_set_record_vars(dialog)
3815
dialog.name = @edit[:new][:name]
3916
dialog.description = @edit[:new][:description]
@@ -161,15 +138,30 @@ def old_dialogs_list
161138
def old_dialogs_new
162139
assert_privileges("old_dialogs_new")
163140
@dialog = MiqDialog.new
164-
old_dialogs_set_form_vars
141+
@dialog.dialog_type = x_node == "root" ? @dialog.dialog_type : x_node.split('_')[1]
142+
@hide_bottom_bar = true
165143
@in_a_form = true
166144
replace_right_cell(:nodetype => "odg-")
167145
end
168146

169147
def old_dialogs_copy
170148
assert_privileges("old_dialogs_copy")
171-
@_params[:typ] = "copy"
172-
old_dialogs_edit
149+
@hide_bottom_bar = true
150+
dialog = MiqDialog.find(params[:id])
151+
@dialog = MiqDialog.new
152+
@dialog.name = "Copy of " + dialog.name
153+
@dialog.description = dialog.description
154+
@dialog.dialog_type = dialog.dialog_type
155+
@dialog.content = YAML.dump(dialog.content)
156+
session[:changed] = true
157+
if @dialog.default == true
158+
add_flash(_("Default Dialog \"%{name}\" can not be edited") % {:name => @dialog.name}, :error)
159+
get_node_info
160+
replace_right_cell(:nodetype => x_node)
161+
return
162+
end
163+
@in_a_form = true
164+
replace_right_cell(:nodetype => "odg-#{params[:id]}")
173165
end
174166

175167
def old_dialogs_edit
@@ -180,39 +172,112 @@ def old_dialogs_edit
180172
obj = find_checked_items
181173
@_params[:id] = obj[0]
182174
end
183-
184-
if params[:typ] == "copy"
185-
dialog = MiqDialog.find(params[:id])
186-
@dialog = MiqDialog.new
187-
@dialog.name = "Copy of " + dialog.name
188-
@dialog.description = dialog.description
189-
@dialog.dialog_type = dialog.dialog_type
190-
@dialog.content = dialog.content
191-
session[:changed] = true
192-
else
193-
@dialog = @record = identify_record(params[:id], MiqDialog) if params[:id]
194-
session[:changed] = false
195-
end
175+
@hide_bottom_bar = true
176+
@dialog = @record = identify_record(params[:id], MiqDialog) if params[:id]
177+
session[:changed] = false
196178
if @dialog.default == true
197179
add_flash(_("Default Dialog \"%{name}\" can not be edited") % {:name => @dialog.name}, :error)
198180
get_node_info
199181
replace_right_cell(:nodetype => x_node)
200182
return
201183
end
202-
old_dialogs_set_form_vars
203184
@in_a_form = true
204185
replace_right_cell(:nodetype => "odg-#{params[:id]}")
205186
end
206187

188+
def old_dialogs_edit_get
189+
assert_privileges("old_dialogs_edit")
190+
unless params[:id]
191+
obj = find_checked_items
192+
@_params[:id] = obj[0]
193+
end
194+
@hide_bottom_bar = true
195+
196+
dialog = MiqDialog.find(params[:id])
197+
if dialog.default == true
198+
add_flash(_("Default Dialog \"%{name}\" can not be edited") % {:name => dialog.name}, :error)
199+
end
200+
render :json => {
201+
:name => dialog.name,
202+
:description => dialog.description,
203+
:content => YAML.dump(dialog.content),
204+
:dialog_type => dialog.dialog_type
205+
}
206+
end
207+
207208
def old_dialogs_update
208209
assert_privileges(params[:id].present? ? 'old_dialogs_edit' : 'old_dialogs_new')
209210
id = params[:id] ? params[:id] : "new"
210211
return unless load_edit("dialog_edit__#{id}", "replace_cell__explorer")
211212
old_dialogs_update_create
212213
end
213214

215+
def provision_dialogs_update
216+
assert_privileges(params[:id].present? ? 'old_dialogs_edit' : 'old_dialogs_new')
217+
@hide_bottom_bar = true
218+
id = params[:id] ? params[:id] : "new"
219+
provision_dialogs_update_create
220+
end
221+
214222
private
215223

224+
def provision_dialogs_update_create
225+
case params[:button]
226+
when "add", "save"
227+
dialog = params[:id].blank? ? MiqDialog.new : MiqDialog.find(params[:id]) # Get new or existing record
228+
if params[:name].blank?
229+
add_flash(_("Name is required"), :error)
230+
end
231+
if params[:dialog_type].blank?
232+
add_flash(_("Dialog Type must be selected"), :error)
233+
end
234+
unless @flash_array
235+
begin
236+
YAML.parse(params[:content_data])
237+
rescue YAML::SyntaxError => ex
238+
add_flash(_("Syntax error in YAML file: %{error_message}") % {:error_message => ex.message}, :error)
239+
end
240+
end
241+
if @flash_array
242+
javascript_flash
243+
return
244+
end
245+
dialog.name = params[:name]
246+
dialog.description = params[:description]
247+
dialog.dialog_type = params[:dialog_type]
248+
dialog.content = YAML.safe_load(params[:content_data])
249+
begin
250+
dialog.save!
251+
rescue StandardError
252+
dialog.errors.each do |error|
253+
add_flash("#{error.attribute.to_s.capitalize} #{error.message}", :error)
254+
end
255+
@changed = true
256+
javascript_flash
257+
else
258+
edit_hash = {}
259+
edit_hash[:new] = {:name => params[:name], :description => params[:description], :dialog_type => params[:dialog_type], :content => params[:content_data]}
260+
261+
if params[:old_data]
262+
edit_hash[:current] = {:name => params[:old_data][:name], :description => params[:old_data][:description], :dialog_type => params[:old_data][:dialog_type], :content => params[:old_data][:content]}
263+
else
264+
edit_hash[:current] = {:name => nil, :description => nil, :dialog_type => nil, :content => nil}
265+
end
266+
AuditEvent.success(build_saved_audit(dialog, edit_hash))
267+
@edit = session[:edit] = nil # clean out the saved info
268+
# if editing from list view then change active_node to be same as updated image_type folder node
269+
if x_node.split('-')[0] == "xx"
270+
self.x_node = "xx-MiqDialog_#{dialog.dialog_type}"
271+
elsif params[:button] == "add"
272+
d = MiqDialog.find_by(:name => dialog.name, :dialog_type => dialog.dialog_type)
273+
self.x_node = "odg-#{d.id}"
274+
end
275+
get_node_info
276+
replace_right_cell(:nodetype => x_node, :replace_trees => [:old_dialogs])
277+
end
278+
end
279+
end
280+
216281
def old_dialogs_update_create
217282
old_dialogs_get_form_vars
218283
case params[:button]
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import React, { useState, useEffect } from 'react';
2+
import { FormSpy } from '@data-driven-forms/react-form-renderer';
3+
import { Button } from 'carbon-components-react';
4+
import MiqFormRenderer, { useFormApi } from '@@ddf';
5+
import PropTypes from 'prop-types';
6+
import createSchema from './miq-ae-customization-form.schema';
7+
import miqRedirectBack from '../../helpers/miq-redirect-back';
8+
9+
const MiqAeCustomization = ({ dialogRecord, dialogTypes }) => {
10+
const [data, setData] = useState({
11+
isLoading: true,
12+
initialValues: undefined,
13+
});
14+
15+
const isEdit = !!(dialogRecord && dialogRecord.id);
16+
17+
useEffect(() => {
18+
if (isEdit) {
19+
http.get(`/miq_ae_customization/old_dialogs_edit_get/${dialogRecord.id}/`).then((recordValues) => {
20+
if (recordValues) {
21+
setData({ ...data, isLoading: false, initialValues: recordValues });
22+
}
23+
});
24+
} else {
25+
const initialValues = {
26+
name: dialogRecord && dialogRecord.name,
27+
description: dialogRecord && dialogRecord.description,
28+
content: (dialogRecord && dialogRecord.content) || '---\n',
29+
dialog_type: dialogRecord && dialogRecord.dialog_type,
30+
};
31+
setData({ ...data, isLoading: false, initialValues });
32+
}
33+
}, [dialogRecord]);
34+
35+
const onSubmit = (values) => {
36+
miqSparkleOn();
37+
38+
const params = {
39+
action: isEdit ? 'edit' : 'create',
40+
name: values.name,
41+
description: values.description,
42+
dialog_type: values.dialog_type,
43+
content_data: values.content,
44+
old_data: data.initialValues,
45+
button: dialogRecord.id ? 'save' : 'add',
46+
};
47+
48+
const request = isEdit
49+
? http.post(`/miq_ae_customization/provision_dialogs_update/${dialogRecord.id}`, params)
50+
: http.post(`/miq_ae_customization/provision_dialogs_update/`, params);
51+
52+
request
53+
.then(() => {
54+
const confirmation = isEdit ? __(`Dialog "${values.name}" was saved`) : __(`Dialog "${values.name}" was added`);
55+
miqRedirectBack(sprintf(confirmation, values.name), 'success', '/miq_ae_customization/explorer');
56+
})
57+
.catch(miqSparkleOff);
58+
};
59+
60+
const onCancel = () => {
61+
const confirmation = dialogRecord.id ? __(`Edit of Dialog "${dialogRecord.name}" cancelled by the user`)
62+
: __(`Add of new Dialog was cancelled by the user`);
63+
const message = sprintf(
64+
confirmation
65+
);
66+
miqRedirectBack(message, 'warning', '/miq_ae_customization/explorer');
67+
};
68+
69+
return (!data.isLoading
70+
? (
71+
<div className="dialog-provision-form">
72+
<MiqFormRenderer
73+
schema={createSchema(dialogTypes)}
74+
initialValues={data.initialValues}
75+
onSubmit={onSubmit}
76+
onCancel={onCancel}
77+
canReset={!!dialogRecord.id}
78+
validate={() => {}}
79+
FormTemplate={(props) => <FormTemplate {...props} recId={dialogRecord.id} />}
80+
/>
81+
</div>
82+
) : null
83+
);
84+
};
85+
86+
const FormTemplate = ({
87+
formFields, recId,
88+
}) => {
89+
const {
90+
handleSubmit, onReset, onCancel, getState,
91+
} = useFormApi();
92+
const { valid, pristine } = getState();
93+
const submitLabel = !!recId ? __('Save') : __('Add');
94+
return (
95+
<form onSubmit={handleSubmit}>
96+
{formFields}
97+
<FormSpy>
98+
{() => (
99+
<div className="custom-button-wrapper">
100+
{ !recId
101+
? (
102+
<Button
103+
disabled={!valid}
104+
kind="primary"
105+
className="btnRight"
106+
type="submit"
107+
variant="contained"
108+
>
109+
{submitLabel}
110+
</Button>
111+
) : (
112+
<Button
113+
disabled={!valid || pristine}
114+
kind="primary"
115+
className="btnRight"
116+
type="submit"
117+
variant="contained"
118+
>
119+
{submitLabel}
120+
</Button>
121+
)}
122+
{!!recId
123+
? (
124+
<Button
125+
disabled={pristine}
126+
kind="secondary"
127+
className="btnRight"
128+
variant="contained"
129+
onClick={onReset}
130+
type="button"
131+
>
132+
{ __('Reset')}
133+
</Button>
134+
) : null}
135+
136+
<Button variant="contained" type="button" onClick={onCancel} kind="secondary">
137+
{ __('Cancel')}
138+
</Button>
139+
</div>
140+
)}
141+
</FormSpy>
142+
</form>
143+
);
144+
};
145+
146+
MiqAeCustomization.propTypes = {
147+
dialogRecord: PropTypes.shape({
148+
id: PropTypes.number,
149+
name: PropTypes.string,
150+
description: PropTypes.string,
151+
content: PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.array]),
152+
dialog_type: PropTypes.string,
153+
}),
154+
dialogTypes: PropTypes.arrayOf(
155+
PropTypes.arrayOf(PropTypes.string.isRequired).isRequired
156+
).isRequired,
157+
};
158+
159+
MiqAeCustomization.defaultProps = {
160+
dialogRecord: undefined,
161+
};
162+
163+
FormTemplate.propTypes = {
164+
formFields: PropTypes.arrayOf(
165+
PropTypes.shape({ id: PropTypes.number }),
166+
PropTypes.shape({ name: PropTypes.string }),
167+
PropTypes.shape({ description: PropTypes.string }),
168+
PropTypes.shape({ content: PropTypes.string }),
169+
PropTypes.shape({ dialog_type: PropTypes.string }),
170+
),
171+
recId: PropTypes.number,
172+
};
173+
174+
FormTemplate.defaultProps = {
175+
formFields: undefined,
176+
recId: undefined,
177+
};
178+
179+
export default MiqAeCustomization;

0 commit comments

Comments
 (0)