Skip to content

Commit bf9c859

Browse files
committed
sets onchange event only during edit
1 parent df166d9 commit bf9c859

File tree

2 files changed

+100
-131
lines changed

2 files changed

+100
-131
lines changed

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

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -144,39 +144,8 @@ export const ClassFieldsEditor = (props) => {
144144
};
145145

146146
// 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-
178147
const updateFieldValueInState = (fieldName, newValue) => {
179-
debugger
148+
// Update existing field in rows
180149
setState((prevState) => {
181150
debugger
182151
const updatedRows = prevState.rows.map((row) => {
@@ -200,41 +169,6 @@ export const ClassFieldsEditor = (props) => {
200169
});
201170
};
202171

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-
238172
const handleSchemaFieldChange = useCallback(
239173
debounce((aeClassId, val, fieldName) => {
240174
let fname;
@@ -258,7 +192,6 @@ export const ClassFieldsEditor = (props) => {
258192
skipErrors: [400],
259193
}).then((response) => {
260194
console.log(response);
261-
// updateFieldValueInState(fieldName, val);
262195
}).catch((error) => {
263196
console.error('Error:', error);
264197
console.error('Response:', error.response);
@@ -337,6 +270,7 @@ export const ClassFieldsEditor = (props) => {
337270
key={state.formKey}
338271
schema={createClassFieldsSchema(
339272
aeClassId,
273+
state.selectedRowId,
340274
aeTypeOptions,
341275
dTypeOptions,
342276
state.rows[state.selectedRowId],

app/javascript/components/data-tables/datastore/schema/modal-form.schema.js

Lines changed: 98 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { componentTypes, validatorTypes } from '@@ddf';
22
import { transformSelectOptions } from '../helper';
33

4-
const createClassFieldsSchema = (aeClassId, aeTypeOptions, dTypeOptions, schemaField = {}, handleSchemaFieldChange, updateFieldValueInState) => {
4+
const createClassFieldsSchema = (aeClassId, selectedRowId, aeTypeOptions,
5+
dTypeOptions, schemaField = {}, handleSchemaFieldChange, updateFieldValueInState) => {
56
const classField = schemaField;
67
const getInitialValue = (field, defaultVal = '') => {
78
if (
@@ -47,11 +48,13 @@ const createClassFieldsSchema = (aeClassId, aeTypeOptions, dTypeOptions, schemaF
4748
isRequired: true,
4849
validate: [{ type: validatorTypes.REQUIRED }],
4950
initialValue: getInitialValue('name'),
50-
onChange: (e) => {
51-
const val = e.target.value;
52-
updateFieldValueInState('name', val);
53-
handleSchemaFieldChange(aeClassId, val, 'name');
54-
},
51+
...(selectedRowId && {
52+
onChange: (e) => {
53+
const val = e.target.value;
54+
updateFieldValueInState('name', val);
55+
handleSchemaFieldChange(aeClassId, val, 'name');
56+
},
57+
}),
5558
},
5659
{
5760
component: componentTypes.SELECT,
@@ -64,7 +67,12 @@ const createClassFieldsSchema = (aeClassId, aeTypeOptions, dTypeOptions, schemaF
6467
options: transformSelectOptions(aeTypeOptions),
6568
includeEmpty: true,
6669
initialValue: getType(aeTypeOptions, getIcons(0)),
67-
onChange: (val) => handleSchemaFieldChange(aeClassId, val, 'aetype'),
70+
...(selectedRowId && {
71+
onChange: (val) => {
72+
updateFieldValueInState('aetype', val);
73+
handleSchemaFieldChange(aeClassId, val, 'aetype');
74+
},
75+
}),
6876
},
6977
{
7078
component: componentTypes.SELECT,
@@ -75,139 +83,166 @@ const createClassFieldsSchema = (aeClassId, aeTypeOptions, dTypeOptions, schemaF
7583
options: transformSelectOptions(dTypeOptions),
7684
includeEmpty: true,
7785
initialValue: getType(dTypeOptions, getIcons(1)),
78-
onChange: (val) => handleSchemaFieldChange(aeClassId, val, 'datatype'),
86+
...(selectedRowId && {
87+
onChange: (val) => {
88+
updateFieldValueInState('datatype', val);
89+
handleSchemaFieldChange(aeClassId, val, 'datatype');
90+
},
91+
}),
7992
},
8093
{
8194
component: componentTypes.TEXT_FIELD,
8295
name: 'default_value',
8396
id: 'default_value',
8497
label: __('Default Value'),
8598
initialValue: getInitialValue('default_value'),
86-
onChange: (e) => {
87-
const val = e.target.value;
88-
updateFieldValueInState('default_value', val);
89-
handleSchemaFieldChange(aeClassId, val, 'default_value');
90-
},
99+
...(selectedRowId && {
100+
onChange: (e) => {
101+
const val = e.target.value;
102+
updateFieldValueInState('default_value', val);
103+
handleSchemaFieldChange(aeClassId, val, 'default_value');
104+
},
105+
}),
91106
},
92107
{
93108
component: componentTypes.TEXT_FIELD,
94109
name: 'display_name',
95110
id: 'display_name',
96111
label: __('Display Name'),
97112
initialValue: getInitialValue('display_name'),
98-
onChange: (e) => {
99-
const val = e.target.value;
100-
updateFieldValueInState('display_name', val);
101-
handleSchemaFieldChange(aeClassId, val, 'display_name');
102-
},
113+
...(selectedRowId && {
114+
onChange: (e) => {
115+
const val = e.target.value;
116+
updateFieldValueInState('display_name', val);
117+
handleSchemaFieldChange(aeClassId, val, 'display_name');
118+
},
119+
}),
103120
},
104121
{
105122
component: componentTypes.TEXT_FIELD,
106123
name: 'description',
107124
id: 'description',
108125
label: __('Description'),
109126
initialValue: getInitialValue('description'),
110-
onChange: (e) => {
111-
const val = e.target.value;
112-
updateFieldValueInState('description', val);
113-
handleSchemaFieldChange(aeClassId, val, 'description');
114-
},
127+
...(selectedRowId && {
128+
onChange: (e) => {
129+
const val = e.target.value;
130+
updateFieldValueInState('description', val);
131+
handleSchemaFieldChange(aeClassId, val, 'description');
132+
},
133+
}),
115134
},
116135
{
117136
component: componentTypes.CHECKBOX,
118137
name: 'substitute',
119138
id: 'substitute',
120139
label: __('Sub'),
121140
initialValue: getInitialValue('substitute', true),
122-
onChange: (e) => {
123-
const val = e.target.value;
124-
updateFieldValueInState('substitute', val);
125-
handleSchemaFieldChange(aeClassId, val, 'substitute');
126-
},
141+
...(selectedRowId && {
142+
onChange: (e) => {
143+
const val = e.target.value;
144+
updateFieldValueInState('substitute', val);
145+
handleSchemaFieldChange(aeClassId, val, 'substitute');
146+
},
147+
}),
127148
},
128149
{
129150
component: componentTypes.TEXT_FIELD,
130151
name: 'collect',
131152
id: 'collect',
132153
label: __('Collect'),
133154
initialValue: getInitialValue('collect'),
134-
onChange: (e) => {
135-
const val = e.target.value;
136-
updateFieldValueInState('collect', val);
137-
handleSchemaFieldChange(aeClassId, val, 'collect');
138-
},
155+
...(selectedRowId && {
156+
onChange: (e) => {
157+
const val = e.target.value;
158+
updateFieldValueInState('collect', val);
159+
handleSchemaFieldChange(aeClassId, val, 'collect');
160+
},
161+
}),
139162
},
140163
{
141164
component: componentTypes.TEXT_FIELD,
142165
name: 'message',
143166
id: 'message',
144167
label: __('Message'),
145168
initialValue: getInitialValue('message', 'create'),
146-
onChange: (e) => {
147-
const val = e.target.value;
148-
updateFieldValueInState('message', val);
149-
handleSchemaFieldChange(aeClassId, val, 'message');
150-
},
169+
...(selectedRowId && {
170+
onChange: (e) => {
171+
const val = e.target.value;
172+
updateFieldValueInState('message', val);
173+
handleSchemaFieldChange(aeClassId, val, 'message');
174+
},
175+
}),
151176
},
152177
{
153178
component: componentTypes.TEXT_FIELD,
154179
name: 'on_entry',
155180
id: 'on_entry',
156181
label: __('On Entry'),
157182
initialValue: getInitialValue('on_entry'),
158-
onChange: (e) => {
159-
const val = e.target.value;
160-
updateFieldValueInState('on_entry', val);
161-
handleSchemaFieldChange(aeClassId, val, 'on_entry');
162-
},
183+
...(selectedRowId && {
184+
onChange: (e) => {
185+
const val = e.target.value;
186+
updateFieldValueInState('on_entry', val);
187+
handleSchemaFieldChange(aeClassId, val, 'on_entry');
188+
},
189+
}),
163190
},
164191
{
165192
component: componentTypes.TEXT_FIELD,
166193
name: 'on_exit',
167194
id: 'on_exit',
168195
label: __('On Exit'),
169196
initialValue: getInitialValue('on_exit'),
170-
onChange: (e) => {
171-
const val = e.target.value;
172-
updateFieldValueInState('on_exit', val);
173-
handleSchemaFieldChange(aeClassId, val, 'on_exit');
174-
},
197+
...(selectedRowId && {
198+
onChange: (e) => {
199+
const val = e.target.value;
200+
updateFieldValueInState('on_exit', val);
201+
handleSchemaFieldChange(aeClassId, val, 'on_exit');
202+
},
203+
}),
175204
},
176205
{
177206
component: componentTypes.TEXT_FIELD,
178207
name: 'on_error',
179208
id: 'on_error',
180209
label: __('On Error'),
181210
initialValue: getInitialValue('on_error'),
182-
onChange: (e) => {
183-
const val = e.target.value;
184-
updateFieldValueInState('on_error', val);
185-
handleSchemaFieldChange(aeClassId, val, 'on_error');
186-
},
211+
...(selectedRowId && {
212+
onChange: (e) => {
213+
const val = e.target.value;
214+
updateFieldValueInState('on_error', val);
215+
handleSchemaFieldChange(aeClassId, val, 'on_error');
216+
},
217+
}),
187218
},
188219
{
189220
component: componentTypes.TEXT_FIELD,
190221
name: 'max_retries',
191222
id: 'max_retries',
192223
label: __('Max Retries'),
193224
initialValue: getInitialValue('max_retries'),
194-
onChange: (e) => {
195-
const val = e.target.value;
196-
updateFieldValueInState('max_retries', val);
197-
handleSchemaFieldChange(aeClassId, val, 'max_retries');
198-
},
225+
...(selectedRowId && {
226+
onChange: (e) => {
227+
const val = e.target.value;
228+
updateFieldValueInState('max_retries', val);
229+
handleSchemaFieldChange(aeClassId, val, 'max_retries');
230+
},
231+
}),
199232
},
200233
{
201234
component: componentTypes.TEXT_FIELD,
202235
name: 'max_time',
203236
id: 'max_time',
204237
label: __('Max Time'),
205238
initialValue: getInitialValue('max_time'),
206-
onChange: (e) => {
207-
const val = e.target.value;
208-
updateFieldValueInState('max_time', val);
209-
handleSchemaFieldChange(aeClassId, val, 'max_time');
210-
},
239+
...(selectedRowId && {
240+
onChange: (e) => {
241+
const val = e.target.value;
242+
updateFieldValueInState('max_time', val);
243+
handleSchemaFieldChange(aeClassId, val, 'max_time');
244+
},
245+
}),
211246
},
212247
],
213248
};

0 commit comments

Comments
 (0)