Skip to content

Commit c01a06e

Browse files
committed
Update UI properly with icons after add/edit
1 parent 576862f commit c01a06e

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,29 @@ export const ClassFieldsEditor = (props) => {
5555
setState((state) => ({ ...state, isSchemaModified: !state.isSchemaModified }));
5656
}, [state.rows]);
5757

58-
// const editClassField = (selectedRow) => {
59-
// const rowId = selectedRow.id;
60-
// setState((state) => ({
61-
// ...state,
62-
// selectedRowId: rowId,
63-
// isModalOpen: true,
64-
// formKey: !state.formKey,
65-
// }));
66-
// };
67-
68-
// const deleteClassField = (selectedRow) => {
69-
// setState((prevState) => ({
70-
// ...prevState,
71-
// rows: prevState.rows.filter((field) => field.id !== selectedRow.id),
72-
// }));
73-
// };
74-
7558
const handleModalClose = () => {
76-
// setModalOpen(false);
7759
setState((state) => ({ ...state, isModalOpen: false }));
7860
};
7961

8062
const formatFieldValues = (field, id, data = {}) => {
8163
if (!field || typeof field !== 'object') return [];
64+
const getFieldName = () => `${field.display_name} (${field.name})`;
65+
const getIconForValue = () => {
66+
const aeMatch = aeTypeOptions.find((option) => option[1] === field.aetype);
67+
const aeTypeIcon = (aeMatch && aeMatch[2] && aeMatch[2]['data-icon']) || '';
68+
69+
const dtypeMatch = dTypeOptions.find((option) => option[1] === field.datatype);
70+
const dTypeIcon = (dtypeMatch && dtypeMatch[2] && dtypeMatch[2]['data-icon']) || '';
71+
72+
return [aeTypeIcon, dTypeIcon];
73+
};
8274

8375
const row = {
8476
id: (field.id || id).toString(),
85-
name: { text: field.name, icon: data.icons || [] },
77+
name: {
78+
text: getFieldName(),
79+
icon: getIconForValue() || [],
80+
},
8681
aetype: { text: field.aetype },
8782
datatype: { text: field.datatype },
8883
default_value: { text: field.default_value || '' },
@@ -146,8 +141,7 @@ export const ClassFieldsEditor = (props) => {
146141
const updateFieldValueInState = (fieldName, newValue) => {
147142
// Update existing field in rows
148143
setState((prevState) => {
149-
debugger
150-
const updatedRows = prevState.rows.map((row) => {
144+
const updatedRow = prevState.rows.map((row) => {
151145
if (row.id === prevState.selectedRowId) {
152146
return {
153147
...row,
@@ -159,11 +153,11 @@ export const ClassFieldsEditor = (props) => {
159153
}
160154
return row;
161155
});
156+
debugger
162157

163158
return {
164159
...prevState,
165-
rows: updatedRows,
166-
isSchemaModified: !prevState.isSchemaModified,
160+
rows: updatedRow,
167161
};
168162
});
169163
};
@@ -212,7 +206,6 @@ export const ClassFieldsEditor = (props) => {
212206
};
213207

214208
const onSchemaSave = (values) => {
215-
debugger
216209
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=save`, { skipErrors: [400] })
217210
.then((response) => {
218211
console.log(response);
@@ -237,7 +230,6 @@ export const ClassFieldsEditor = (props) => {
237230
const onCancel = () => {
238231
http.post(`/miq_ae_class/update_fields/${aeClassId}?button=cancel`, { skipErrors: [400] })
239232
.then((response) => {
240-
debugger
241233
console.log(response);
242234
}).catch((error) => {
243235
console.error('Failed to cancel schema updates:', error);

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,55 @@ import { transformSelectOptions } from '../helper';
44
const createClassFieldsSchema = (aeClassId, selectedRowId, aeTypeOptions,
55
dTypeOptions, schemaField = {}, handleSchemaFieldChange, updateFieldValueInState) => {
66
const classField = schemaField;
7+
8+
// const formatName = () => {
9+
// const fullName = classField.name.text;
10+
// const match = fullName.match(/^(.+?)\s*\(([^)]+)\)$/);
11+
// return {
12+
// display_name: match[1],
13+
// name: match[2],
14+
// };
15+
// };
16+
17+
const formatName = () => {
18+
const fullName = classField.name.text;
19+
const match = fullName.match(/^(.+?)\s*\(([^)]+)\)$/);
20+
21+
if (classField.display_name && classField.display_name.text) {
22+
return {
23+
display_name: classField.display_name.text,
24+
name: (match && match[2]) || fullName,
25+
};
26+
}
27+
return {
28+
display_name: (match && match[1]) || '',
29+
name: (match && match[2]) || fullName,
30+
};
31+
};
32+
733
const getInitialValue = (field, defaultVal = '') => {
834
if (
935
classField
1036
&& typeof classField === 'object'
1137
&& Object.keys(classField).length > 0
12-
&& classField[field]
13-
&& 'text' in classField[field]
1438
) {
15-
return classField[field].text;
39+
if (selectedRowId) {
40+
if (field === 'name' || field === 'display_name') {
41+
// debugger
42+
const formatted = formatName();
43+
return formatted[field] || defaultVal;
44+
}
45+
}
46+
47+
if (classField[field] && 'text' in classField[field]) {
48+
return classField[field].text || defaultVal;
49+
}
1650
}
51+
1752
return defaultVal;
1853
};
1954

55+
2056
const getIcons = (index) => {
2157
if (
2258
classField
@@ -35,7 +71,7 @@ const createClassFieldsSchema = (aeClassId, selectedRowId, aeTypeOptions,
3571
(item) => item[2] && item[2]['data-icon'] === icon
3672
);
3773

38-
return match ? match[1] : null;
74+
return match ? match[1] : '';
3975
};
4076

4177
return {

0 commit comments

Comments
 (0)