-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert automation simulation form conversion to react
Basics for resolve form conversion
- Loading branch information
Showing
5 changed files
with
228 additions
and
117 deletions.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
app/javascript/components/automate-simulation/automate-simulation.schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { componentTypes, validatorTypes } from '@@ddf'; | ||
|
||
const createSchema = (resolve, max_name_length, url, ae_ansible_custom_button, form_action, ae_custom_button, attribute_values_pairs, maxlength) => { | ||
const fields = [ | ||
{ | ||
component: componentTypes.SELECT, | ||
id: 'instance_name', | ||
name: 'instance_name', | ||
label: __('System/Process'), | ||
initialValue: resolve.instance_names.sort((b, a) => a.toLowerCase().localeCompare(b.toLowerCase())), | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
isSearchable: true, | ||
simpleValue: true, | ||
options: resolve.instance_names.map(name => ({ label: name, value: name })), | ||
onChange: (value) => miqSelectPickerEvent(value, url), | ||
url: url, | ||
}, | ||
{ | ||
id: 'simulationParameters', | ||
component: componentTypes.PLAIN_TEXT, | ||
name: 'simulationParameters', | ||
label: __('Simulation Parameters'), | ||
style: { fontSize: '16px' } | ||
}, | ||
{ | ||
component: componentTypes.CHECKBOX, | ||
id: 'readonly', | ||
name: 'readonly', | ||
label: __('Execute Methods'), | ||
initialValue: resolve.new.readonly !== true, | ||
title : "Simulation parameters" | ||
}, | ||
{ | ||
id: 'AttributeValuePairs', | ||
component: componentTypes.PLAIN_TEXT, | ||
name: 'AttributeValuePairs', | ||
label: __('Attribute/Value Pairs'), | ||
style: { fontSize: '16px' } | ||
}, | ||
]; | ||
|
||
if (!ae_ansible_custom_button) { | ||
fields.splice(1, 0, { | ||
component: componentTypes.TEXT_FIELD, | ||
id: 'object_message', | ||
name: 'object_message', | ||
label: __('Message'), | ||
maxLength: max_name_length, | ||
initialValue: resolve.new.object_message, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
isRequired: true | ||
}), | ||
fields.splice(2, 0, { | ||
component: componentTypes.TEXT_FIELD, | ||
id: 'object_request', | ||
name: 'object_request', | ||
label: __('Request'), | ||
initialValue: resolve.new.object_request, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
}); | ||
} | ||
|
||
if (!document.getElementById('description') && document.getElementById('object_message')) { | ||
document.getElementById('object_message').focus(); | ||
} | ||
|
||
if (form_action != "miq_action") { | ||
if (ae_custom_button) { | ||
fields.splice(3, 0, { | ||
id: 'objectAttribute_1', | ||
component: componentTypes.PLAIN_TEXT, | ||
name: 'objectAttribute_1', | ||
label: __('Object Attribute 1'), | ||
style: { fontSize: '16px' } | ||
}), | ||
fields.splice(4, 0, { | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'object_attribute_1', | ||
label: __('Type'), | ||
isReadOnly: true, | ||
content: `${_("Type")}: ${ui_lookup({ model: resolve.target_class })}`, | ||
}) | ||
} else { | ||
const targetClassesOptions = [ | ||
{ label: "<none>", value: null }, | ||
...Object.entries(resolve.target_classes).map(([key, value]) => ({ label: key, value: value })) | ||
]; | ||
fields.splice(3, 0, { | ||
id: 'objectAttribute', | ||
component: componentTypes.PLAIN_TEXT, | ||
name: 'objectAttribute', | ||
label: __('Object Attribute'), | ||
style: { fontSize: '16px' } | ||
}), | ||
fields.splice(4, 0, { | ||
component: componentTypes.SELECT, | ||
id: 'target_class', | ||
name: 'target_class', | ||
label: __('Type'), | ||
initialValue: resolve.new.target_class, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
isSearchable: true, | ||
simpleValue: true, | ||
options: targetClassesOptions, | ||
onChange: (value) => miqSelectPickerEvent(value, url), | ||
url: url, | ||
}) | ||
} | ||
} | ||
|
||
attribute_values_pairs.forEach((_, i) => { | ||
const f = `attribute_${i + 1}`; | ||
const v = `value_${i + 1}`; | ||
const labelKey = `attributeValuePairLabel_${i + 1}`; | ||
|
||
fields.push( | ||
{ | ||
component: componentTypes.PLAIN_TEXT, | ||
id: labelKey, | ||
name: labelKey, | ||
label: `${i + 1}`, | ||
style: {fontWeight: 'bold'} | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
id: f, | ||
name: f, | ||
maxLength: maxlength, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
fieldprops: { | ||
className: 'form-control col-md-4', | ||
'data-miq_observe': JSON.stringify({ interval: '.5', url: url }) | ||
} | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
id: v, | ||
name: v, | ||
maxLength: maxlength, | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
fieldprops: { | ||
className: 'form-control col-md-4', | ||
'data-miq_observe': JSON.stringify({ interval: '.5', url: url }) | ||
} | ||
} | ||
); | ||
}) | ||
|
||
return { | ||
title: 'Object Details', | ||
fields: fields, | ||
}; | ||
}; | ||
|
||
export default createSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import MiqFormRenderer from '@@ddf'; | ||
import PropTypes from 'prop-types'; | ||
import createSchema from './automate-simulation.schema'; | ||
import miqRedirectBack from '../../../../app/javascript/helpers/miq-redirect-back'; | ||
|
||
const AutomateSimulation = ({ resolve, title, max_name_length, url, ae_ansible_custom_button, form_action, ae_custom_button, attribute_values_pairs, maxlength }) => { | ||
|
||
const handleSubmit = (values) => { | ||
fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify(values), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
}) | ||
.then(response => { | ||
const message = __('Automation Simulation options submitted'); | ||
const url = '/miq_ae_tools/resolve' | ||
miqRedirectBack(message, 'success', url); | ||
}) | ||
.catch(error => { | ||
miqRedirectBack(error, 'error', url); | ||
}); | ||
}; | ||
|
||
return( | ||
<MiqFormRenderer | ||
schema={createSchema(resolve, max_name_length, url, ae_ansible_custom_button, form_action, ae_custom_button, attribute_values_pairs, maxlength)} | ||
onSubmit={handleSubmit} | ||
/> | ||
); | ||
}; | ||
|
||
AutomateSimulation.propTypes = { | ||
resolve: PropTypes.object.isRequired, | ||
title: PropTypes.string.isRequired, | ||
max_name_length: PropTypes.number.isRequired, | ||
url: PropTypes.string.isRequired, | ||
ae_ansible_custom_button: PropTypes.bool.isRequired, | ||
form_action: PropTypes.string.isRequired, | ||
ae_custom_button: PropTypes.bool.isRequired, | ||
attribute_values_pairs: PropTypes.array.isRequired, | ||
maxlength: PropTypes.number.isRequired, | ||
}; | ||
|
||
AutomateSimulation.defaultProps = { | ||
recordId: undefined, | ||
title: undefined, | ||
max_name_length: undefined, | ||
url: undefined, | ||
ae_ansible_custom_button: undefined, | ||
form_action: undefined, | ||
ae_custom_button: undefined, | ||
attribute_values_pairs: undefined, | ||
maxlength: undefined, | ||
}; | ||
export default AutomateSimulation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters