Skip to content

Commit b93edd8

Browse files
committed
Handles on click of save, reset and cancel with flash messages
1 parent 62459f8 commit b93edd8

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

app/controllers/miq_ae_class_controller.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def find_existing_node(parents)
281281

282282
def replace_right_cell(options = {})
283283
@explorer = true
284+
# byebug
284285
replace_trees = options[:replace_trees]
285286

286287
# FIXME: is the following line needed?
@@ -1147,32 +1148,36 @@ def update_fields
11471148
case params[:button]
11481149
when "cancel"
11491150
@sb[:action] = session[:edit] = nil # clean out the saved info
1150-
add_flash(_("Edit of schema for Automate Class \"%{name}\" was cancelled by the user") % {:name => @ae_class.name})
1151+
# add_flash(_("Edit of schema for Automate Class \"%{name}\" was cancelled by the user") % {:name => @ae_class.name})
11511152
@in_a_form = false
1152-
replace_right_cell
1153-
# render :json => {:status => 200}
1153+
# replace_right_cell
1154+
message = _("Edit of schema for Automate Class \"%{name}\" was cancelled by the user") % {:name => @ae_class.name}
1155+
render :json => {:status => 200, :message => message}
11541156
when "save"
1155-
# byebug
11561157
ae_class = find_record_with_rbac(MiqAeClass, params[:id])
11571158
begin
11581159
MiqAeClass.transaction do
1159-
# byebug
11601160
set_field_vars(ae_class)
11611161
ae_class.ae_fields.destroy(MiqAeField.where(:id => @edit[:fields_to_delete]))
11621162
ae_class.ae_fields.each { |fld| fld.default_value = nil if fld.default_value == "" }
11631163
ae_class.save!
11641164
end
11651165
rescue StandardError => bang
1166-
add_flash(_("Error during 'save': %{error_message}") % {:error_message => bang.message}, :error)
1166+
# byebug
1167+
# add_flash(_("Error during 'save': %{error_message}") % {:error_message => bang.message}, :error)
11671168
session[:changed] = @changed = true
11681169
# javascript_flash
1170+
error_message = _("Error during 'save': %{error_message}") % {:error_message => bang.message}, :error
1171+
render :json => {:status => 500, :error => error_message}
11691172
else
1170-
add_flash(_("Schema for Automate Class \"%{name}\" was saved") % {:name => ae_class.name})
1173+
# add_flash(_("Schema for Automate Class \"%{name}\" was saved") % {:name => ae_class.name})
11711174
AuditEvent.success(build_saved_audit(ae_class, @edit))
11721175
@sb[:action] = session[:edit] = nil # clean out the saved info
11731176
# @in_a_form = false
1174-
replace_right_cell(:replace_trees => [:ae])
1175-
nil
1177+
# replace_right_cell(:replace_trees => [:ae])
1178+
# nil
1179+
success_message = _("Schema for Automate Class \"%{name}\" was saved") % {:name => ae_class.name}
1180+
render :json => {:status => 200, :message => success_message}
11761181
end
11771182
when "reset"
11781183
fields_set_form_vars
@@ -1181,11 +1186,14 @@ def update_fields
11811186
add_flash(_("All changes have been reset"), :warning)
11821187
@button = "reset"
11831188
@in_a_form = true
1184-
replace_right_cell
1185-
# render :json => {:status => 200}
1189+
# replace_right_cell
1190+
success_message = _("All changes have been reset")
1191+
render :json => {:status => 200, :message => success_message}
11861192
else
1193+
byebug
11871194
@changed = session[:changed] = (@edit[:new] != @edit[:current])
1188-
replace_right_cell(:replace_trees => [:ae])
1195+
# replace_right_cell(:replace_trees => [:ae])
1196+
render :json => {:status => 200}
11891197
end
11901198
end
11911199

app/javascript/components/data-tables/datastore/schema/class-fields-editor.jsx

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import createClassFieldsSchema from './modal-form.schema';
1111
import createSchemaEditSchema from './class-fields-schema';
1212
import mapper from '../../../../forms/mappers/componentMapper';
1313
import { SchemaTableComponent } from './schema-table';
14+
import miqRedirectBack from '../../../../helpers/miq-redirect-back';
15+
import miqFlash from '../../../../helpers/miq-flash';
1416

1517
export const ClassFieldsEditor = (props) => {
1618
const {
@@ -207,40 +209,40 @@ export const ClassFieldsEditor = (props) => {
207209
const onSchemaReset = () => {
208210
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=reset`, { skipErrors: [400] })
209211
.then((response) => {
210-
console.log(response);
211-
}).catch((error) => {
212-
console.error('Failed to reset schema:', error);
213-
});
212+
if (response.status === 200) {
213+
miqRedirectBack(__(response.message), 'success', '/miq_ae_class/explorer');
214+
} else {
215+
miqSparkleOff();
216+
miqFlash('error', response.error);
217+
}
218+
})
219+
.catch(miqSparkleOff);
214220
};
215221

216-
const onSchemaSave = (values) => {
222+
const onSchemaSave = () => {
217223
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=save`, { skipErrors: [400] })
218224
.then((response) => {
219-
console.log(response);
220-
}).catch((error) => {
221-
console.error('Failed to reset schema:', error);
222-
});
225+
if (response.status === 200) {
226+
miqRedirectBack(__(response.message), 'success', '/miq_ae_class/explorer');
227+
} else {
228+
miqSparkleOff();
229+
miqFlash('error', response.error);
230+
}
231+
})
232+
.catch(miqSparkleOff);
223233
};
224234

225-
// const onCancel = () => {
226-
// miqSparkleOn();
227-
// const message = __('Edit of schema field was cancelled by the user');
228-
// miqRedirectBack(message, 'warning', '/miq_ae_class/explorer');
229-
// };
230-
231-
// const onCancel = () => {
232-
// miqSparkleOn();
233-
// // miqAjaxButton(`/miq_ae_class/explorer`);
234-
// window.location.reload();
235-
// };
236-
237235
const onCancel = () => {
238236
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=cancel`, { skipErrors: [400] })
239237
.then((response) => {
240-
console.log(response);
241-
}).catch((error) => {
242-
console.error('Failed to cancel schema updates:', error);
243-
});
238+
if (response.status === 200) {
239+
miqRedirectBack(__(response.message), 'success', '/miq_ae_class/explorer');
240+
} else {
241+
miqSparkleOff();
242+
miqFlash('error', response.error);
243+
}
244+
})
245+
.catch(miqSparkleOff);
244246
};
245247

246248
return (

0 commit comments

Comments
 (0)