Skip to content

Commit e2000d9

Browse files
jeffibmjeffbonson
authored andcommitted
Order Service Form conversion
1 parent 8adaffa commit e2000d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3736
-34
lines changed

app/controllers/catalog_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ def svc_catalog_provision
743743
ra, st, svc_catalog_provision_finish_submit_endpoint
744744
)
745745
@in_a_form = true
746+
@dialog_locals = options[:dialog_locals]
746747
replace_right_cell(:action => "dialog_provision", :dialog_locals => options[:dialog_locals])
747748
else
748749
# if catalog item has no dialog and provision button was pressed from list view

app/helpers/catalog_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module CatalogHelper
33
include RequestInfoHelper
44
include Mixins::AutomationMixin
55
include OrchestrationTemplateHelper
6+
include OrderServiceHelper
67

78
def miq_catalog_resource(resources)
89
headers = ["", _("Name"), _("Description"), _("Action Order"), _("Provision Order"), _("Action Start"), _("Action Stop"), _("Delay (mins) Start"), _("Delay (mins) Stop")]

app/helpers/miq_request_helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,24 @@ def row_data(label, value)
99
def request_task_configuration_script_ids(miq_request)
1010
miq_request.miq_request_tasks.map { |task| task.options&.dig(:configuration_script_id) }.compact
1111
end
12+
13+
def select_box_options(options)
14+
options.map { |item| { label: item, value: item } }
15+
end
16+
17+
def dialog_field_values(dialog)
18+
transformed_data = dialog.transform_keys { |key| key.sub('Array::', '') }
19+
converted_data = transformed_data.transform_values do |value|
20+
if value.to_s.include?("\u001F")
21+
select_box_options(value.split("\u001F"))
22+
elsif value.to_s.include?("::")
23+
model, id = value.split("::")
24+
record = model.constantize.find_by(id: id)
25+
record ? [{label: record.description, value: record.id}] : value
26+
else
27+
value
28+
end
29+
end
30+
return converted_data
31+
end
1232
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module OrderServiceHelper
2+
def order_service_data(dialog)
3+
{
4+
:dialogId => dialog[:dialog_id],
5+
:params => {
6+
:resourceActionId => dialog[:resource_action_id],
7+
:targetId => dialog[:target_id],
8+
:targetType => dialog[:target_type],
9+
:realTargetType => dialog[:real_target_type],
10+
},
11+
:urls => {
12+
:apiSubmitEndpoint => dialog[:api_submit_endpoint],
13+
:apiAction => dialog[:api_action],
14+
:cancelEndPoint => dialog[:cancel_endpoint],
15+
:finishSubmitEndpoint => dialog[:finish_submit_endpoint],
16+
:openUrl => dialog[:open_url],
17+
}
18+
}
19+
end
20+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createContext } from 'react';
2+
3+
const OrderServiceContext = createContext();
4+
export default OrderServiceContext;
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
import { componentTypes } from '@@ddf';
2+
import { REFERENCE_TYPES } from './order-service-constants';
3+
4+
const sharedSchema = (field) => ({
5+
id: field.id,
6+
name: field.name,
7+
label: field.label,
8+
hideField: !field.visible,
9+
isRequired: field.required,
10+
isDisabled: field.read_only,
11+
description: field.description,
12+
});
13+
14+
/** Function to build a text box. */
15+
export const buildTextBox = ({
16+
field, validate, orderServiceConfig,
17+
}) => {
18+
let component = {};
19+
20+
if (field.options.protected) {
21+
component = {
22+
component: 'password-field',
23+
validate,
24+
initialValue: field.default_value || '',
25+
...sharedSchema(field),
26+
};
27+
} else {
28+
component = {
29+
component: componentTypes.TEXT_FIELD,
30+
validate,
31+
initialValue: field.default_value || '',
32+
...sharedSchema(field),
33+
resolveProps: (props, { input }) => {
34+
orderServiceConfig.updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
35+
},
36+
};
37+
}
38+
return component;
39+
};
40+
41+
/** Function to build a text area */
42+
export const buildTextAreaBox = ({
43+
field, validate, updateFormReference,
44+
}) => ({
45+
component: componentTypes.TEXTAREA,
46+
...sharedSchema(field),
47+
initialValue: field.default_value,
48+
validate,
49+
resolveProps: (props, { input }) => {
50+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
51+
},
52+
});
53+
54+
/** Function to build a check box. */
55+
export const buildCheckBox = ({
56+
field, validate, updateFormReference,
57+
}) => ({
58+
component: componentTypes.CHECKBOX,
59+
...sharedSchema(field),
60+
initialValue: field.default_value,
61+
validate,
62+
resolveProps: (props, { input }) => {
63+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
64+
},
65+
});
66+
67+
/** Function to build a tag control field. */
68+
export const buildTagControl = ({
69+
field, validate, updateFormReference,
70+
}) => {
71+
const options = [];
72+
field.values.forEach((value) => {
73+
if (!value.id) {
74+
value.id = '-1';
75+
}
76+
options.push({ value: value.id, label: value.description });
77+
});
78+
return {
79+
component: componentTypes.SELECT,
80+
...sharedSchema(field),
81+
initialValue: field.default_value,
82+
validate,
83+
options,
84+
resolveProps: (props, { input }) => {
85+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
86+
},
87+
};
88+
};
89+
90+
/** Function to build a date control field */
91+
export const buildDateControl = ({
92+
field, validate, updateFormReference,
93+
}) => ({
94+
component: componentTypes.DATE_PICKER,
95+
...sharedSchema(field),
96+
initialValue: field.default_value,
97+
validate,
98+
variant: 'date-time',
99+
resolveProps: (props, { input }) => {
100+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
101+
},
102+
});
103+
104+
/** Function to build a time control field */
105+
export const buildTimeControl = ({
106+
field, validate, updateFormReference,
107+
}, dateTime) => ([{
108+
component: componentTypes.DATE_PICKER,
109+
...sharedSchema(field),
110+
initialValue: dateTime.toISOString(),
111+
validate,
112+
variant: 'date-time',
113+
resolveProps: (props, { input }) => {
114+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
115+
},
116+
},
117+
{
118+
component: componentTypes.TIME_PICKER,
119+
id: `${field.id}-time`,
120+
name: `${field.name}-time`,
121+
isRequired: field.required,
122+
isDisabled: field.read_only,
123+
initialValue: dateTime,
124+
validate,
125+
twelveHoursFormat: true,
126+
pattern: '(0?[1-9]|1[0-2]):[0-5][0-9]',
127+
resolveProps: (props, { input }) => {
128+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
129+
},
130+
}]);
131+
132+
/** Function to build radio buttons fields */
133+
export const buildRadioButtons = ({
134+
field, validate, updateFormReference,
135+
}) => {
136+
const options = [];
137+
field.values.forEach((value) => {
138+
options.push({ value: value[0], label: value[1] });
139+
});
140+
return {
141+
component: componentTypes.RADIO,
142+
...sharedSchema(field),
143+
initialValue: field.default_value,
144+
validate,
145+
options,
146+
resolveProps: (props, { input }) => {
147+
updateFormReference({ type: REFERENCE_TYPES.dialogFields, payload: { fieldName: input.name, value: input.value } });
148+
},
149+
};
150+
};
151+
152+
/** Function to build a refresh button near to drop down. */
153+
export const buildRefreshButton = (field, tabIndex, { updateFormReference }) => ({
154+
component: 'refresh-button',
155+
name: `refresh_${field.name}`,
156+
data: {
157+
showRefreshButton: !!(field.dynamic && field.show_refresh_button),
158+
fieldName: field.name,
159+
tabIndex,
160+
disabled: false,
161+
updateRefreshInProgress: (status) => updateFormReference({ type: REFERENCE_TYPES.refreshInProgress, payload: status }),
162+
},
163+
});
164+
165+
const buildOptions = (field) => {
166+
let options = [];
167+
let placeholder = __('<Choose>');
168+
let start;
169+
170+
field.values.forEach((value) => {
171+
if (value[0] === null) {
172+
value[0] = null;
173+
// eslint-disable-next-line prefer-destructuring
174+
placeholder = value[1];
175+
}
176+
options.push({ value: value[0] !== null ? String(value[0]) : null, label: value[1] });
177+
});
178+
if (options[0].value === null) {
179+
start = options.shift();
180+
}
181+
options = options.sort((option1, option2) => {
182+
if (field.options.sort_by === 'description') {
183+
if (field.options.sort_order === 'ascending') {
184+
return option1.label.localeCompare(option2.label);
185+
}
186+
return option2.label.localeCompare(option1.label);
187+
}
188+
if (field.options.sort_order === 'ascending') {
189+
return option1.value.localeCompare(option2.value);
190+
}
191+
return option2.value.localeCompare(option1.value);
192+
});
193+
if (start) {
194+
options.unshift(start);
195+
}
196+
return { options, placeholder };
197+
};
198+
199+
/** Function to build a drop down select box. */
200+
export const buildDropDownList = ({
201+
field, validate, orderServiceConfig,
202+
}) => {
203+
const { options, placeholder } = buildOptions(field);
204+
205+
if (field.options && field.options.force_multi_value) {
206+
return {
207+
component: componentTypes.SELECT,
208+
...sharedSchema(field),
209+
id: field.id,
210+
labelText: field.label,
211+
validate,
212+
options,
213+
placeholder,
214+
simpleValue: true,
215+
// isSearchable: true, // Unable to use isSearchable for isMulti items, due to a browser console error.
216+
isMulti: true,
217+
onChange: (value) => {
218+
orderServiceConfig.fieldOnChange(value, field);
219+
},
220+
};
221+
}
222+
223+
return {
224+
component: componentTypes.SELECT,
225+
...sharedSchema(field),
226+
labelText: field.label,
227+
initialValue: field.default_value,
228+
validate,
229+
options,
230+
placeholder,
231+
isSearchable: true,
232+
simpleValue: true,
233+
onChange: (value) => {
234+
console.log(value, field)
235+
orderServiceConfig.fieldOnChange(value, field);
236+
},
237+
};
238+
};

0 commit comments

Comments
 (0)