Skip to content

Commit 33c6798

Browse files
committed
fixes issues with displaying values and saving of service dialog edit form
1 parent 11f6b48 commit 33c6798

File tree

2 files changed

+97
-38
lines changed

2 files changed

+97
-38
lines changed

app/javascript/components/service-dialog-form/data.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ export const tagControlCategories = async() => {
5858
};
5959

6060
// data has formfields and list (as of now); no dialog related general info - this is needed
61-
export const saveServiceDialog = (data) => {
61+
export const saveServiceDialog = (data, onSuccess) => {
6262
const payload = formattedCatalogPayload(data);
6363

6464
API.post('/api/service_dialogs', payload, {
6565
skipErrors: [400],
6666
}).then(() => {
6767
// Redirect to the service dialogs explorer page after successful save
68-
window.location.href = '/miq_ae_customization/explorer';
68+
if (typeof onSuccess === 'function') {
69+
onSuccess();
70+
} else {
71+
window.location.href = '/miq_ae_customization/explorer';
72+
}
6973
}).catch((error) => {
7074
console.error('Error saving dialog:', error);
7175
});

app/javascript/components/service-dialog-form/index.jsx

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -502,50 +502,100 @@ const ServiceDialogForm = ({ dialogData, dialogAction }) => {
502502
</div>
503503
);
504504

505+
// Helper function to navigate back to the explorer page
506+
const navigateToExplorer = () => {
507+
window.location.href = '/miq_ae_customization/explorer';
508+
};
509+
510+
// Helper function to clean up field data for submission
511+
const cleanupField = (field) => {
512+
// Create a clean copy without React-specific or UI-specific properties
513+
const cleanField = { ...field };
514+
515+
// Remove properties that shouldn't be sent to the backend
516+
delete cleanField.componentId;
517+
delete cleanField.fieldsToRefresh;
518+
delete cleanField.categories;
519+
delete cleanField.selectedCategory;
520+
delete cleanField.subCategories;
521+
522+
return {
523+
name: cleanField.name || `field_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`,
524+
description: cleanField.description || '',
525+
data_type: cleanField.dataType || 'string',
526+
notes: cleanField.notes || '',
527+
notes_display: cleanField.notesDisplay || '',
528+
display: cleanField.display || 'edit',
529+
display_method: cleanField.displayMethod || null,
530+
display_method_options: cleanField.displayMethodOptions || {},
531+
required: cleanField.required || false,
532+
required_method: cleanField.requiredMethod || null,
533+
required_method_options: cleanField.requiredMethodOptions || {},
534+
default_value: cleanField.value || '',
535+
values: cleanField.values || null,
536+
values_method: cleanField.valuesMethod || null,
537+
values_method_options: cleanField.valuesMethodOptions || {},
538+
options: {
539+
protected: cleanField.protected || false,
540+
show_past_days: cleanField.showPastDates || false,
541+
sort_by: cleanField.sortBy || 'description',
542+
sort_order: cleanField.sortOrder || 'ascending',
543+
force_multi_value: cleanField.multiselect || false
544+
},
545+
label: cleanField.label || 'Field Label',
546+
position: cleanField.position || 0,
547+
validator_type: cleanField.validatorType || '',
548+
validator_rule: cleanField.validatorRule || null,
549+
reconfigurable: cleanField.reconfigurable || false,
550+
dynamic: cleanField.dynamic || false,
551+
show_refresh_button: cleanField.showRefresh || false,
552+
load_values_on_init: cleanField.loadOnInit || false,
553+
read_only: cleanField.readOnly || false,
554+
auto_refresh: cleanField.autoRefresh || false,
555+
trigger_auto_refresh: cleanField.triggerAutoRefresh || false,
556+
visible: cleanField.visible !== undefined ? cleanField.visible : true,
557+
type: cleanField.type || 'DialogFieldTextBox'
558+
};
559+
};
560+
505561
const handleSubmit = (e) => {
506562
e.preventDefault();
507563

508-
// If dialogAction contains an id and action is 'edit', we're updating an existing dialog
509-
if (dialogAction && dialogAction.id && dialogAction.action === 'edit') {
510-
// Update existing dialog
511-
API.put(`/api/service_dialogs/${dialogAction.id}`, {
512-
action: 'edit',
513-
resource: {
514-
label: data.label,
515-
description: data.description,
516-
dialog_tabs: data.formFields
517-
.filter(tab => tab.tabId !== 'new') // Filter out the "Create new tab" option
518-
.map((tab, index) => ({
519-
label: tab.name,
520-
position: index,
521-
dialog_groups: tab.sections.map((section, sectionIndex) => ({
522-
label: section.title,
523-
description: section.description || '',
524-
position: sectionIndex,
525-
dialog_fields: section.fields.map((field, fieldIndex) => {
526-
// Make sure we have all required properties for each field
527-
const fieldData = {
528-
...field,
529-
position: fieldIndex,
530-
name: field.name || `field_${Date.now()}_${fieldIndex}`,
531-
label: field.label || 'Field Label',
532-
type: field.type || 'DialogFieldTextBox',
533-
data_type: field.dataType || 'string'
534-
};
535-
536-
return fieldData;
537-
})
538-
}))
564+
// Prepare the dialog data in the format expected by the backend
565+
const dialogFormData = {
566+
label: data.label,
567+
description: data.description,
568+
dialog_tabs: data.formFields
569+
.filter((tab) => tab.tabId !== 'new') // Filter out the "Create new tab" option
570+
.map((tab, index) => ({
571+
label: tab.name,
572+
position: index,
573+
dialog_groups: tab.sections.map((section, sectionIndex) => ({
574+
label: section.title,
575+
description: section.description || '',
576+
position: sectionIndex,
577+
dialog_fields: section.fields.map((field, fieldIndex) => cleanupField({
578+
...field,
579+
position: fieldIndex
539580
}))
540-
}
581+
}))
582+
}))
583+
};
584+
585+
// If dialogData contains an id and dialogAction.action is 'edit', we're updating an existing dialog
586+
if (dialogData && dialogData.id && dialogAction && dialogAction.action === 'edit') {
587+
// Update existing dialog using the API endpoint
588+
API.post(`/api/service_dialogs/${dialogData.id}`, {
589+
action: 'edit',
590+
resource: dialogFormData
541591
}).then(() => {
542-
window.location.href = '/miq_ae_customization/explorer';
543-
}).catch(error => {
592+
navigateToExplorer();
593+
}).catch((error) => {
544594
console.error('Error updating dialog:', error);
545595
});
546596
} else {
547597
// Create new dialog
548-
saveServiceDialog(data);
598+
saveServiceDialog(data, navigateToExplorer);
549599
}
550600
};
551601

@@ -638,7 +688,12 @@ const ServiceDialogForm = ({ dialogData, dialogAction }) => {
638688
>
639689
{ __('Submit')}
640690
</Button>
641-
<Button variant="contained" type="button" onClick={() => console.log('this is on cancel')} kind="secondary">
691+
<Button
692+
variant="contained"
693+
type="button"
694+
onClick={navigateToExplorer}
695+
kind="secondary"
696+
>
642697
{ __('Cancel')}
643698
</Button>
644699
</div>

0 commit comments

Comments
 (0)