Skip to content

Commit df166d9

Browse files
committed
handling api calls
1 parent 219c6db commit df166d9

File tree

6 files changed

+330
-154
lines changed

6 files changed

+330
-154
lines changed

app/controllers/miq_ae_class_controller.rb

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,8 @@ def fields_form_field_changed
881881
assert_privileges('miq_ae_field_edit')
882882
return unless load_edit("aefields_edit__#{params[:id]}", "replace_cell__explorer")
883883

884+
# byebug
885+
884886
fields_get_form_vars
885887
@changed = (@edit[:new] != @edit[:current])
886888
# render :update do |page|
@@ -1143,17 +1145,20 @@ def update_fields
11431145
return unless load_edit("aefields_edit__#{params[:id]}", "replace_cell__explorer")
11441146

11451147
fields_get_form_vars
1146-
@changed = (@edit[:new] != @edit[:current])
1148+
# @changed = (@edit[:new] != @edit[:current])
11471149
case params[:button]
11481150
when "cancel"
11491151
@sb[:action] = session[:edit] = nil # clean out the saved info
11501152
add_flash(_("Edit of schema for Automate Class \"%{name}\" was cancelled by the user") % {:name => @ae_class.name})
11511153
@in_a_form = false
11521154
replace_right_cell
1155+
# render :json => {:status => 200}
11531156
when "save"
1157+
# byebug
11541158
ae_class = find_record_with_rbac(MiqAeClass, params[:id])
11551159
begin
11561160
MiqAeClass.transaction do
1161+
# byebug
11571162
set_field_vars(ae_class)
11581163
ae_class.ae_fields.destroy(MiqAeField.where(:id => @edit[:fields_to_delete]))
11591164
ae_class.ae_fields.each { |fld| fld.default_value = nil if fld.default_value == "" }
@@ -1162,22 +1167,24 @@ def update_fields
11621167
rescue StandardError => bang
11631168
add_flash(_("Error during 'save': %{error_message}") % {:error_message => bang.message}, :error)
11641169
session[:changed] = @changed = true
1165-
javascript_flash
1170+
# javascript_flash
11661171
else
11671172
add_flash(_("Schema for Automate Class \"%{name}\" was saved") % {:name => ae_class.name})
11681173
AuditEvent.success(build_saved_audit(ae_class, @edit))
11691174
@sb[:action] = session[:edit] = nil # clean out the saved info
1170-
@in_a_form = false
1175+
# @in_a_form = false
11711176
replace_right_cell(:replace_trees => [:ae])
11721177
nil
11731178
end
11741179
when "reset"
11751180
fields_set_form_vars
1176-
session[:changed] = @changed = false
1181+
# session[:changed] = @changed = false
1182+
session[:changed] = false
11771183
add_flash(_("All changes have been reset"), :warning)
11781184
@button = "reset"
11791185
@in_a_form = true
11801186
replace_right_cell
1187+
# render :json => {:status => 200}
11811188
else
11821189
@changed = session[:changed] = (@edit[:new] != @edit[:current])
11831190
replace_right_cell(:replace_trees => [:ae])
@@ -1442,18 +1449,19 @@ def create_namespace
14421449
# AJAX driven routine to select a classification entry
14431450
def field_select
14441451
assert_privileges('miq_ae_field_edit')
1445-
fields_get_form_vars
1452+
fields_get_form_vars # nothing in particular handled here
14461453
# @combo_xml = build_type_options
14471454
# @dtype_combo_xml = build_dtype_options
14481455
session[:field_data] = {}
14491456
@edit[:new_field][:substitute] = session[:field_data][:substitute] = true
1450-
@changed = (@edit[:new] != @edit[:current])
1451-
render :update do |page|
1452-
page << javascript_prologue
1453-
page.replace("class_fields_div", :partial => "class_fields")
1454-
page << javascript_for_miq_button_visibility(@changed)
1455-
page << "miqSparkle(false);"
1456-
end
1457+
# @changed = (@edit[:new] != @edit[:current])
1458+
# render :update do |page|
1459+
# page << javascript_prologue
1460+
# page.replace("class_fields_div", :partial => "class_fields")
1461+
# page << javascript_for_miq_button_visibility(@changed)
1462+
# page << "miqSparkle(false);"
1463+
# end
1464+
render :json => {:status => 200}
14571465
end
14581466

14591467
# AJAX driven routine to select a classification entry
@@ -1481,21 +1489,22 @@ def field_accept
14811489
def field_delete
14821490
assert_privileges('miq_ae_field_edit')
14831491
fields_get_form_vars
1484-
@combo_xml = build_type_options
1485-
@dtype_combo_xml = build_dtype_options
1492+
# @combo_xml = build_type_options
1493+
# @dtype_combo_xml = build_dtype_options
14861494

14871495
if params.key?(:id) && @edit[:fields_to_delete].exclude?(params[:id])
14881496
@edit[:fields_to_delete].push(params[:id])
14891497
end
14901498

14911499
@edit[:new][:fields].delete_at(params[:arr_id].to_i)
1492-
@changed = (@edit[:new] != @edit[:current])
1493-
render :update do |page|
1494-
page << javascript_prologue
1495-
page.replace("class_fields_div", :partial => "class_fields")
1496-
page << javascript_for_miq_button_visibility(@changed)
1497-
page << "miqSparkle(false);"
1498-
end
1500+
# @changed = (@edit[:new] != @edit[:current])
1501+
# render :update do |page|
1502+
# page << javascript_prologue
1503+
# page.replace("class_fields_div", :partial => "class_fields")
1504+
# page << javascript_for_miq_button_visibility(@changed)
1505+
# page << "miqSparkle(false);"
1506+
# end
1507+
render :json => {:status => 200}
14991508
end
15001509

15011510
# AJAX driven routine to select a classification entry

app/javascript/components/data-tables/datastore/helper.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,3 @@ export const transformSelectOptions = (array) =>
155155
value,
156156
...extraProps,
157157
}));
158-
159-
export const handleSchemaFieldChange = (aeClassId, event) => {
160-
debugger
161-
162-
const data = {
163-
[event.target.name]: event.target.value,
164-
id: aeClassId,
165-
};
166-
167-
http.post(`/miq_ae_class/fields_form_field_changed/${aeClassId}`, data, {
168-
skipErrors: [400],
169-
}).then((response) => {
170-
debugger
171-
console.log(response);
172-
// handleSaveResponse(response.message);
173-
}).catch((error) => {
174-
console.error('Error:', error);
175-
console.error('Response:', error.response);
176-
console.log('somethign went wrogn')
177-
// miqFlash('error', __('Something went wrong'));
178-
});
179-
};

app/javascript/components/data-tables/datastore/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const Datastore = ({
104104
case 'class_fields':
105105
return (
106106
<ClassFieldsEditor
107-
// aeClassId={aeClassId}
107+
aeClassId={aeClassId}
108108
initialData={initialData}
109109
aeTypeOptions={aeTypeOptions}
110110
dTypeOptions={dTypeOptions}

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

Lines changed: 169 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, {
2+
useState, useEffect, useMemo, useCallback,
3+
} from 'react';
24
import PropTypes from 'prop-types';
35
import { Modal } from 'carbon-components-react';
46
import MiqFormRenderer from '@@ddf';
7+
import debounce from 'lodash/debounce';
58
import { schemaHeaders, createEditableRows } from '../helper';
69
import createClassFieldsSchema from './modal-form.schema';
710
// import miqRedirectBack from '../../helpers/miq-redirect-back';
@@ -11,7 +14,7 @@ import { SchemaTableComponent } from './schema-table';
1114

1215
export const ClassFieldsEditor = (props) => {
1316
const {
14-
initialData, aeTypeOptions, dTypeOptions,
17+
aeClassId, initialData, aeTypeOptions, dTypeOptions,
1518
} = props;
1619

1720
const componentMapper = {
@@ -140,8 +143,150 @@ export const ClassFieldsEditor = (props) => {
140143
}
141144
};
142145

146+
// const handleSchemaFieldChange = (aeClassId, val, fieldName) => {
147+
// let fname;
148+
149+
// if (state.selectedRowId) {
150+
// if (fieldName === 'datatype' || fieldName === 'aetype') {
151+
// fname = `fields_${fieldName}${state.selectedRowId}`;
152+
// } else {
153+
// fname = `fields_${fieldName}_${state.selectedRowId}`;
154+
// }
155+
// } else {
156+
// fname = `field_${fieldName}`;
157+
// }
158+
159+
// const data = {
160+
// [fname]: val,
161+
// id: aeClassId,
162+
// };
163+
164+
// http.post(`/miq_ae_class/fields_form_field_changed/${aeClassId}`, data, {
165+
// skipErrors: [400],
166+
// }).then((response) => {
167+
// debugger
168+
// console.log(response);
169+
// // return val;
170+
// }).catch((error) => {
171+
// console.error('Error:', error);
172+
// console.error('Response:', error.response);
173+
// console.log('somethign went wrogn')
174+
// // miqFlash('error', __('Something went wrong'));
175+
// });
176+
// };
177+
178+
const updateFieldValueInState = (fieldName, newValue) => {
179+
debugger
180+
setState((prevState) => {
181+
debugger
182+
const updatedRows = prevState.rows.map((row) => {
183+
if (row.id === prevState.selectedRowId) {
184+
return {
185+
...row,
186+
[fieldName]: {
187+
...row[fieldName],
188+
text: newValue,
189+
},
190+
};
191+
}
192+
return row;
193+
});
194+
195+
return {
196+
...prevState,
197+
rows: updatedRows,
198+
isSchemaModified: !prevState.isSchemaModified,
199+
};
200+
});
201+
};
202+
203+
204+
// const handleSchemaFieldChange = useMemo(() => (
205+
// debounce((aeClassId, val, fieldName) => {
206+
// // updateFieldValueInState(fieldName, val);
207+
// let fname;
208+
209+
// if (state.selectedRowId) {
210+
// if (fieldName === 'datatype' || fieldName === 'aetype') {
211+
// fname = `fields_${fieldName}${state.selectedRowId}`;
212+
// } else {
213+
// fname = `fields_${fieldName}_${state.selectedRowId}`;
214+
// }
215+
// } else {
216+
// fname = `field_${fieldName}`;
217+
// }
218+
219+
// const data = {
220+
// [fname]: val,
221+
// id: aeClassId,
222+
// };
223+
224+
// http.post(`/miq_ae_class/fields_form_field_changed/${aeClassId}`, data, {
225+
// skipErrors: [400],
226+
// }).then((response) => {
227+
// console.log(response);
228+
// // updateFieldValueInState(fieldName, val);
229+
// }).catch((error) => {
230+
// console.error('Error:', error);
231+
// console.error('Response:', error.response);
232+
// console.log('Something went wrong');
233+
// });
234+
// }, 500)
235+
// ), [aeClassId, state.selectedRowId]);
236+
237+
238+
const handleSchemaFieldChange = useCallback(
239+
debounce((aeClassId, val, fieldName) => {
240+
let fname;
241+
242+
if (state.selectedRowId) {
243+
if (fieldName === 'datatype' || fieldName === 'aetype') {
244+
fname = `fields_${fieldName}${state.selectedRowId}`;
245+
} else {
246+
fname = `fields_${fieldName}_${state.selectedRowId}`;
247+
}
248+
} else {
249+
fname = `field_${fieldName}`;
250+
}
251+
252+
const data = {
253+
[fname]: val,
254+
id: aeClassId,
255+
};
256+
257+
http.post(`/miq_ae_class/fields_form_field_changed/${aeClassId}`, data, {
258+
skipErrors: [400],
259+
}).then((response) => {
260+
console.log(response);
261+
// updateFieldValueInState(fieldName, val);
262+
}).catch((error) => {
263+
console.error('Error:', error);
264+
console.error('Response:', error.response);
265+
console.log('Something went wrong');
266+
});
267+
}, 500),
268+
[aeClassId, state.selectedRowId]
269+
);
270+
271+
272+
273+
const onSchemaReset = () => {
274+
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=reset`, { skipErrors: [400] })
275+
.then((response) => {
276+
console.log(response);
277+
}).catch((error) => {
278+
console.error('Failed to reset schema:', error);
279+
});
280+
};
281+
143282
const onSchemaSave = (values) => {
144283
debugger
284+
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=save`, { skipErrors: [400] })
285+
.then((response) => {
286+
console.log(response);
287+
}).catch((error) => {
288+
console.error('Failed to reset schema:', error);
289+
});
145290
};
146291

147292
// const onCancel = () => {
@@ -151,13 +296,23 @@ export const ClassFieldsEditor = (props) => {
151296
// miqRedirectBack(message, 'warning', '/miq_ae_class/explorer');
152297
// };
153298

299+
// const onCancel = () => {
300+
// miqSparkleOn();
301+
// // miqAjaxButton(`/miq_ae_class/explorer`);
302+
// window.location.reload();
303+
// };
304+
154305
const onCancel = () => {
155-
miqSparkleOn();
156-
// miqAjaxButton(`/miq_ae_class/explorer`);
157-
window.location.reload();
158-
};
306+
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=cancel`, { skipErrors: [400] })
307+
.then((response) => {
308+
debugger
309+
console.log(response);
310+
}).catch((error) => {
311+
console.error('Failed to cancel schema updates:', error);
312+
});
313+
}
159314

160-
debugger
315+
// debugger
161316

162317
return (
163318
<>
@@ -168,6 +323,7 @@ export const ClassFieldsEditor = (props) => {
168323
onSubmit={onSchemaSave}
169324
onCancel={onCancel}
170325
canReset
326+
onReset={onSchemaReset}
171327
buttonsLabels={{ submitLabel: __('Save') }}
172328
/>
173329

@@ -180,14 +336,16 @@ export const ClassFieldsEditor = (props) => {
180336
<MiqFormRenderer
181337
key={state.formKey}
182338
schema={createClassFieldsSchema(
339+
aeClassId,
183340
aeTypeOptions,
184341
dTypeOptions,
185-
state.selectedRowId,
186-
state.rows[state.selectedRowId]
342+
state.rows[state.selectedRowId],
343+
handleSchemaFieldChange,
344+
updateFieldValueInState,
187345
)}
188346
onSubmit={onModalSubmit}
189347
onCancel={handleModalClose}
190-
canReset
348+
// canReset
191349
buttonsLabels={{ submitLabel: __('Acc/savw') }}
192350
/>
193351
</Modal>
@@ -196,7 +354,7 @@ export const ClassFieldsEditor = (props) => {
196354
};
197355

198356
ClassFieldsEditor.propTypes = {
199-
// aeClassId: PropTypes.number.isRequired,
357+
aeClassId: PropTypes.number.isRequired,
200358
initialData: PropTypes.arrayOf(PropTypes.any).isRequired,
201359
aeTypeOptions: PropTypes.arrayOf(PropTypes.any),
202360
dTypeOptions: PropTypes.arrayOf(PropTypes.any),

0 commit comments

Comments
 (0)