1
- import React , { useState , useEffect } from 'react' ;
1
+ import React , {
2
+ useState , useEffect , useMemo , useCallback ,
3
+ } from 'react' ;
2
4
import PropTypes from 'prop-types' ;
3
5
import { Modal } from 'carbon-components-react' ;
4
6
import MiqFormRenderer from '@@ddf' ;
7
+ import debounce from 'lodash/debounce' ;
5
8
import { schemaHeaders , createEditableRows } from '../helper' ;
6
9
import createClassFieldsSchema from './modal-form.schema' ;
7
10
// import miqRedirectBack from '../../helpers/miq-redirect-back';
@@ -11,7 +14,7 @@ import { SchemaTableComponent } from './schema-table';
11
14
12
15
export const ClassFieldsEditor = ( props ) => {
13
16
const {
14
- initialData, aeTypeOptions, dTypeOptions,
17
+ aeClassId , initialData, aeTypeOptions, dTypeOptions,
15
18
} = props ;
16
19
17
20
const componentMapper = {
@@ -140,8 +143,150 @@ export const ClassFieldsEditor = (props) => {
140
143
}
141
144
} ;
142
145
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
+
143
282
const onSchemaSave = ( values ) => {
144
283
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
+ } ) ;
145
290
} ;
146
291
147
292
// const onCancel = () => {
@@ -151,13 +296,23 @@ export const ClassFieldsEditor = (props) => {
151
296
// miqRedirectBack(message, 'warning', '/miq_ae_class/explorer');
152
297
// };
153
298
299
+ // const onCancel = () => {
300
+ // miqSparkleOn();
301
+ // // miqAjaxButton(`/miq_ae_class/explorer`);
302
+ // window.location.reload();
303
+ // };
304
+
154
305
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
+ }
159
314
160
- debugger
315
+ // debugger
161
316
162
317
return (
163
318
< >
@@ -168,6 +323,7 @@ export const ClassFieldsEditor = (props) => {
168
323
onSubmit = { onSchemaSave }
169
324
onCancel = { onCancel }
170
325
canReset
326
+ onReset = { onSchemaReset }
171
327
buttonsLabels = { { submitLabel : __ ( 'Save' ) } }
172
328
/>
173
329
@@ -180,14 +336,16 @@ export const ClassFieldsEditor = (props) => {
180
336
< MiqFormRenderer
181
337
key = { state . formKey }
182
338
schema = { createClassFieldsSchema (
339
+ aeClassId ,
183
340
aeTypeOptions ,
184
341
dTypeOptions ,
185
- state . selectedRowId ,
186
- state . rows [ state . selectedRowId ]
342
+ state . rows [ state . selectedRowId ] ,
343
+ handleSchemaFieldChange ,
344
+ updateFieldValueInState ,
187
345
) }
188
346
onSubmit = { onModalSubmit }
189
347
onCancel = { handleModalClose }
190
- canReset
348
+ // canReset
191
349
buttonsLabels = { { submitLabel : __ ( 'Acc/savw' ) } }
192
350
/>
193
351
</ Modal >
@@ -196,7 +354,7 @@ export const ClassFieldsEditor = (props) => {
196
354
} ;
197
355
198
356
ClassFieldsEditor . propTypes = {
199
- // aeClassId: PropTypes.number.isRequired,
357
+ aeClassId : PropTypes . number . isRequired ,
200
358
initialData : PropTypes . arrayOf ( PropTypes . any ) . isRequired ,
201
359
aeTypeOptions : PropTypes . arrayOf ( PropTypes . any ) ,
202
360
dTypeOptions : PropTypes . arrayOf ( PropTypes . any ) ,
0 commit comments