Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Apps - Bug Fixes #4677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,9 @@ class UserDefinedFieldsPanel extends React.PureComponent<CombinedProps> {
render() {
const { userDefinedFields, classes } = this.props;

/** [true, false, true, false] */
const hasOnlyOptionalFields = userDefinedFields!
.map(eachUDF => {
return Object.keys(eachUDF).some(eachKey => eachKey === 'default');
})
.every(eachValue => eachValue === true);
const [requiredUDFs, optionalUDFs] = seperateUDFsByRequiredStatus(
userDefinedFields!
);

return (
<Paper className={classes.root}>
Expand All @@ -139,41 +136,36 @@ class UserDefinedFieldsPanel extends React.PureComponent<CombinedProps> {
</Typography>

{/* Required Fields */}
{userDefinedFields!
.filter(
(field: Linode.StackScript.UserDefinedField) =>
field.hasOwnProperty('default') !== true
)
.map((field: Linode.StackScript.UserDefinedField) => {
const error = getError(field, this.props.errors);
return this.renderField(field, error);
})}
{requiredUDFs.map((field: Linode.StackScript.UserDefinedField) => {
const error = getError(field, this.props.errors);
return this.renderField(field, error);
})}

{/* Optional Fields */}
<ShowMoreExpansion
name="Show Advanced Options"
defaultExpanded={hasOnlyOptionalFields}
>
<Typography variant="body1" className={classes.advDescription}>
These fields are additional configuration options and are not
required for creation.
</Typography>
<div
className={`${classes.optionalFieldWrapper} optionalFieldWrapper`}
{optionalUDFs.length !== 0 && (
<ShowMoreExpansion
name="Show Advanced Options"
/** expand the panel by default if there are no required UDFs */
defaultExpanded={requiredUDFs.length === 0}
>
<Grid container alignItems="center">
{userDefinedFields!
.filter(
(field: Linode.StackScript.UserDefinedField) =>
field.hasOwnProperty('default') === true
)
.map((field: Linode.StackScript.UserDefinedField) => {
const error = getError(field, this.props.errors);
return this.renderField(field, error);
})}
</Grid>
</div>
</ShowMoreExpansion>
<Typography variant="body1" className={classes.advDescription}>
These fields are additional configuration options and are not
required for creation.
</Typography>
<div
className={`${classes.optionalFieldWrapper} optionalFieldWrapper`}
>
<Grid container alignItems="center">
{optionalUDFs.map(
(field: Linode.StackScript.UserDefinedField) => {
const error = getError(field, this.props.errors);
return this.renderField(field, error);
}
)}
</Grid>
</div>
</ShowMoreExpansion>
)}
</Paper>
);
}
Expand Down Expand Up @@ -202,6 +194,29 @@ const isMultiSelect = (udf: Linode.StackScript.UserDefinedField) => {
return !!udf.manyof; // if we have a manyof prop, it's a checkbox
};

/**
* Used to separate required UDFs from non-required ones
*
* @return nested array [[...requiredUDFs], [...nonRequiredUDFs]]
*/
const seperateUDFsByRequiredStatus = (
udfs: Linode.StackScript.UserDefinedField[]
) => {
return udfs.reduce(
(accum, eachUDF) => {
/**
* if the "default" key exists, it's optional
*/
if (eachUDF.hasOwnProperty('default')) {
return [[...accum[0]], [...accum[1], eachUDF]];
} else {
return [[...accum[0], eachUDF], [...accum[1]]];
}
},
[[], []]
);
};

const styled = withStyles(styles);

export default styled(RenderGuard<CombinedProps>(UserDefinedFieldsPanel));
2 changes: 2 additions & 0 deletions src/features/linodes/LinodesCreate/LinodeCreateContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const defaultState: State = {
selectedDiskSize: undefined,
selectedLinodeID: undefined,
selectedStackScriptID: undefined,
selectedStackScriptLabel: '',
selectedStackScriptUsername: '',
selectedRegionID: undefined,
selectedTypeID: undefined,
tags: [],
Expand Down