Skip to content

Commit

Permalink
Cloud Apps - Bug Fixes (#4677)
Browse files Browse the repository at this point in the history
* bug fixes

* Copy fixes
  • Loading branch information
martinmckenna committed Mar 20, 2019
1 parent d68c380 commit 9f0269d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
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

0 comments on commit 9f0269d

Please sign in to comment.