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

refactor: [UIE-8245] - DBaaS: Replace Select component with Autocomplete #11245

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
@@ -0,0 +1,5 @@
---
"@linode/manager": Changed
---

Replace Select component with Autocomplete in DBaaS ([#11245](https://github.com/linode/manager/pull/11245))
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ import { Divider } from '@linode/ui';
import Grid from '@mui/material/Unstable_Grid2';
import React from 'react';

import Select from 'src/components/EnhancedSelect';
import { _SingleValue } from 'src/components/EnhancedSelect/components/SingleValue';
import { RegionSelect } from 'src/components/RegionSelect/RegionSelect';
import { RegionHelperText } from 'src/components/SelectRegionPanel/RegionHelperText';
import { Typography } from 'src/components/Typography';
import {
StyledLabelTooltip,
StyledTextField,
} from 'src/features/Databases/DatabaseCreate/DatabaseCreate.style';
import { EngineOption } from 'src/features/Databases/DatabaseCreate/EngineOption';
import { DatabaseEngineSelect } from 'src/features/Databases/DatabaseCreate/DatabaseEngineSelect';
import { useRestrictedGlobalGrantCheck } from 'src/hooks/useRestrictedGlobalGrantCheck';
import { getSelectedOptionFromGroupedOptions } from 'src/utilities/getSelectedOptionFromGroupedOptions';

import { getEngineOptions } from './utilities';

import type {
ClusterSize,
Expand All @@ -27,7 +22,6 @@ import type {
ReplicationCommitTypes,
} from '@linode/api-v4';
import type { FormikErrors } from 'formik';
import type { Item } from 'src/components/EnhancedSelect';
export interface DatabaseCreateValues {
allow_list: {
address: string;
Expand Down Expand Up @@ -59,13 +53,6 @@ export const DatabaseClusterData = (props: Props) => {
globalGrantType: 'add_databases',
});

const engineOptions = React.useMemo(() => {
if (!engines) {
return [];
}
return getEngineOptions(engines);
}, [engines]);

const labelToolTip = (
<StyledLabelTooltip>
<strong>Label must:</strong>
Expand Down Expand Up @@ -94,22 +81,11 @@ export const DatabaseClusterData = (props: Props) => {
<Divider spacingBottom={12} spacingTop={38} />
<Grid>
<Typography variant="h2">Select Engine and Region</Typography>
{/* TODO: use Autocomplete instead of Select */}
<Select
onChange={(selected: Item<string>) => {
onChange('engine', selected.value);
}}
value={getSelectedOptionFromGroupedOptions(
values.engine,
engineOptions
)}
components={{ Option: EngineOption, SingleValue: _SingleValue }}
disabled={isRestricted}
<DatabaseEngineSelect
engines={engines}
errorText={errors.engine}
isClearable={false}
label="Database Engine"
options={engineOptions}
placeholder="Select a Database Engine"
onChange={onChange}
value={values.engine}
/>
</Grid>
<Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const DatabaseCreate = () => {
},
],
cluster_size: -1 as ClusterSize,
engine: 'mysql' as Engine,
engine: 'mysql/8' as Engine,
label: '',
region: '',
type: '',
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A general note on error handling: surfacing the API error looks good. But I am wondering what happened to our schema validation on this field - it doesn't appear to be validating the Engine in prod, either, so this was an existing issue - optional but encouraged to address here. The engine field's behavior would ideally be consistent with that of other fields, validating before we get that API error. If not addressed in this PR, could you create a follow up to address?

Screenshot 2024-11-12 at 3 59 10β€―PM

Screenshot 2024-11-12 at 3 59 52β€―PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schema validation didn't work because the field wasn’t empty; it had a default value intended to display prices in the Plan Table, which caused a minor bug. After discussing with Amit, I fixed it to properly display the default value in the 'Select a Database Engine' field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Thanks for looking into this. Things for further consideration by you, the rest of the DBaaS folks, and UX: Is showing the priced plans in the Plan Table prior to any Engine selection a requirement? And does surfacing a default Engine selection have any implications we want to avoid?

In other sections of the app, when we require a selection (in this case, region) to be made first, we display the following in the Plan Table. I'm wondering if a 'Select an engine to view plans and prices' would be desirable for consistency here.

Screenshot 2024-11-14 at 12 44 25β€―PM
Screenshot 2024-11-14 at 12 44 36β€―PM

I think that change would probably require more code changes even further outside the scope of the Autocomplete swap, so potentially a follow up.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Box } from '@linode/ui';
import Grid from '@mui/material/Unstable_Grid2';
import React from 'react';

import { Autocomplete } from 'src/components/Autocomplete/Autocomplete';
import { getEngineOptions } from 'src/features/Databases/DatabaseCreate/utilities';
import { useRestrictedGlobalGrantCheck } from 'src/hooks/useRestrictedGlobalGrantCheck';

import type { DatabaseEngine } from '@linode/api-v4';

interface Props {
engines: DatabaseEngine[] | undefined;
errorText: string | undefined;
onChange: (filed: string, value: any) => void;
value: string;
}

export const DatabaseEngineSelect = (props: Props) => {
const { engines, errorText, onChange, value } = props;
const isRestricted = useRestrictedGlobalGrantCheck({
globalGrantType: 'add_databases',
});

const engineOptions = React.useMemo(() => {
if (!engines) {
return [];
}
return getEngineOptions(engines);
}, [engines]);

const selectedEngine = React.useMemo(() => {
return engineOptions.find((val) => val.value === value);
}, [value, engineOptions]);

return (
<Autocomplete
groupBy={(option) => {
if (option.engine.match(/mysql/i)) {
return 'MySQL';
}
if (option.engine.match(/postgresql/i)) {
return 'PostgreSQL';
}
if (option.engine.match(/mongodb/i)) {
return 'MongoDB';
}
if (option.engine.match(/redis/i)) {
return 'Redis';
}
return 'Other';
}}
onChange={(_, selected) => {
onChange('engine', selected.value);
}}
renderOption={(props, option) => {
const { key, ...rest } = props;
return (
<li {...rest} data-testid="db-engine-option" key={key}>
<Grid
alignItems="center"
container
direction="row"
justifyContent="flex-start"
spacing={2}
>
<Grid className="py0">{option.flag}</Grid>
<Grid>{option.label}</Grid>
</Grid>
</li>
);
}}
textFieldProps={{
InputProps: {
startAdornment: (
<Box sx={{ pr: 1, pt: 0.7 }}>{selectedEngine?.flag}</Box>
),
},
}}
autoHighlight
disableClearable
disabled={isRestricted}
errorText={errorText}
isOptionEqualToValue={(option, value) => option.value === value.value}
label="Database Engine"
options={engineOptions ?? []}
placeholder="Select a Database Engine"
mjac0bs marked this conversation as resolved.
Show resolved Hide resolved
value={selectedEngine}
/>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { groupBy } from 'ramda';
import React from 'react';

import MongoDBIcon from 'src/assets/icons/mongodb.svg';
Expand Down Expand Up @@ -68,48 +67,17 @@ export const engineIcons: EngineIconsProps = {
postgresql: <PostgreSQLIcon height="24" width="24" />,
redis: null,
};

export const getEngineOptions = (engines: DatabaseEngine[]) => {
const groupedEngines = groupBy<DatabaseEngine>((engineObject) => {
if (engineObject.engine.match(/mysql/i)) {
return 'MySQL';
}
if (engineObject.engine.match(/postgresql/i)) {
return 'PostgreSQL';
}
if (engineObject.engine.match(/mongodb/i)) {
return 'MongoDB';
}
if (engineObject.engine.match(/redis/i)) {
return 'Redis';
}
return 'Other';
}, engines);
return ['MySQL', 'PostgreSQL', 'MongoDB', 'Redis', 'Other'].reduce(
(accum, thisGroup) => {
if (
!groupedEngines[thisGroup] ||
groupedEngines[thisGroup].length === 0
) {
return accum;
}
return [
...accum,
{
label: thisGroup,
options: groupedEngines[thisGroup]
.map((engineObject) => ({
...engineObject,
flag: engineIcons[engineObject.engine],
label: getDatabasesDescription({
engine: engineObject.engine,
version: engineObject.version,
}),
value: `${engineObject.engine}/${engineObject.version}`,
}))
.sort((a, b) => (a.version > b.version ? -1 : 1)),
},
];
},
[]
);
return engines.map((e) => {
return {
engine: e.engine,
flag: engineIcons[e.engine],
label: getDatabasesDescription({
engine: e.engine,
version: e.version,
}),
value: `${e.engine}/${e.version}`,
};
});
};
Loading