Skip to content

Commit

Permalink
feat(database-creation-flow): add default instance type for managed (#…
Browse files Browse the repository at this point in the history
…1756)

* feat(database-creation-flow): add default instance type for managed

* fix: test
  • Loading branch information
RemiBonnet authored Nov 14, 2024
1 parent 5557486 commit e6c3105
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function PageSettingsResources(props: PageSettingsResourcesProps) {
isManaged={database.mode === DatabaseModeEnum.MANAGED}
displayInstanceTypesWarning={displayInstanceTypesWarning}
displayStorageWarning={displayStorageWarning}
isSetting
/>
</Section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ export function StepGeneralFeature() {
})

const watchModeDatabase = methods.watch('mode')
const watchTypeDatabase = methods.watch('type')

const { databaseTypeOptions, databaseVersionOptions } = generateDatabasesTypesAndVersionOptions(
databaseConfigurations,
Expand All @@ -154,13 +153,6 @@ export function StepGeneralFeature() {
const publicOptionNotAvailable =
cluster?.kubernetes === KubernetesEnum.K3_S && watchModeDatabase === DatabaseModeEnum.CONTAINER

useEffect(() => {
if (methods.formState.isDirty) {
methods.setValue('version', '')
if (watchTypeDatabase) methods.trigger('version')
}
}, [watchModeDatabase, methods.setValue])

const onSubmit = methods.handleSubmit((data) => {
const cloneData = {
...data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import StepResourcesFeature from './step-resources-feature'
jest.mock('@qovery/domains/cloud-providers/feature', () => ({
useCloudProviderDatabaseInstanceTypes: () => ({
data: [
{
name: 'db.t3.small',
},
{
name: 'db.t3.medium',
},
Expand Down Expand Up @@ -97,7 +100,7 @@ describe('PageDatabaseCreateResourcesFeature', () => {
storage: 1,
cpu: 100,
memory: 100,
instance_type: 'db.t3.medium',
instance_type: 'db.t3.small',
},
setResourcesData: mockSetResourcesData,
creationFlowUrl: '/organization/1/project/2/environment/3/services/create/database',
Expand All @@ -108,10 +111,10 @@ describe('PageDatabaseCreateResourcesFeature', () => {
)

const realSelect = screen.getByLabelText('Instance type')
await selectEvent.select(realSelect, 'db.t3.medium')
await waitFor(() => expect(screen.getAllByText('db.t3.medium')).toHaveLength(1))
await selectEvent.select(realSelect, 'db.t3.small')
await waitFor(() => expect(screen.getAllByText('db.t3.small')).toHaveLength(1))

expect(screen.getByText('db.t3.medium')).toBeInTheDocument()
expect(screen.getByText('db.t3.small')).toBeInTheDocument()

const button = screen.getByTestId('button-submit')
expect(button).toBeInTheDocument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function StepResourcesFeature() {
const { setCurrentStep, resourcesData, setResourcesData, generalData, creationFlowUrl } = useDatabaseCreateContext()
const { organizationId = '', projectId = '', environmentId = '' } = useParams()
const navigate = useNavigate()
const pathCreate = `${SERVICES_URL(organizationId, projectId, environmentId)}${SERVICES_DATABASE_CREATION_URL}`

useEffect(() => {
!generalData?.name &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function StepGeneral({
cloudProvider,
showManagedWithVpcOptions,
}: StepGeneralProps) {
const { control, formState, watch } = useFormContext<GeneralData>()
const { control, formState, watch, resetField } = useFormContext<GeneralData>()
const { organizationId = '', environmentId = '', projectId = '', slug, option } = useParams()
const navigate = useNavigate()
const [openExtraAttributes, setOpenExtraAttributes] = useState(false)
Expand Down Expand Up @@ -119,7 +119,6 @@ export function StepGeneral({
<>
<InputRadio
value={DatabaseModeEnum.CONTAINER}
className="mb-3"
name={field.name}
description="Deployed on your Kubernetes cluster. Not for production purposes, no back-ups nor snapshots."
onChange={field.onChange}
Expand All @@ -130,7 +129,6 @@ export function StepGeneral({
cloudProvider === CloudProviderEnum.AWS &&
cluster.kubernetes !== 'SELF_MANAGED' && (
<InputRadio
className="mb-3"
value={DatabaseModeEnum.MANAGED}
name={field.name}
description="Managed by your cloud provider. Back-ups and snapshots will be periodically created."
Expand Down Expand Up @@ -169,7 +167,10 @@ export function StepGeneral({
<InputSelect
label="Database type"
options={databaseTypeOptions || []}
onChange={field.onChange}
onChange={(e) => {
field.onChange(e)
resetField('version')
}}
value={field.value}
error={error?.message}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function StepResources({ onSubmit, isManaged, onBack, databaseType }: Ste

<Section className="gap-4">
<Heading>Resources configuration</Heading>
<DatabaseSettingsResources isDatabase isManaged={isManaged} databaseType={databaseType} />
<DatabaseSettingsResources isManaged={isManaged} databaseType={databaseType} />
</Section>

<div className="flex justify-between">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
type DatabaseTypeEnum,
type ManagedDatabaseInstanceTypeResponse,
} from 'qovery-typescript-axios'
import { useMemo } from 'react'
import { useEffect, useMemo } from 'react'
import { useFormContext } from 'react-hook-form'
import { useParams } from 'react-router-dom'
import { match } from 'ts-pattern'
import { useCloudProviderDatabaseInstanceTypes } from '@qovery/domains/cloud-providers/feature'
Expand All @@ -14,13 +15,16 @@ import SettingsResourcesInstanceTypes from '../../ui/settings-resources-instance
export interface SettingsResourcesInstanceTypesFeatureProps {
databaseType: DatabaseTypeEnum
displayWarning: boolean
isSetting: boolean
}

export function SettingsResourcesInstanceTypesFeature({
databaseType,
displayWarning,
isSetting,
}: SettingsResourcesInstanceTypesFeatureProps) {
const { organizationId = '', environmentId = '' } = useParams()
const { setValue } = useFormContext()

const { data: environment } = useEnvironment({ environmentId })
const { data: cluster } = useCluster({ organizationId, clusterId: environment?.cluster_id ?? '' })
Expand Down Expand Up @@ -55,6 +59,20 @@ export function SettingsResourcesInstanceTypesFeature({
[databaseInstanceTypes]
)

// Set default value when component mounts or database type changes
useEffect(() => {
if (!isSetting && databaseInstanceTypes) {
const defaultInstanceType = match(databaseType)
.with('POSTGRESQL', () => 'db.t3.small')
.with('MONGODB', () => 'db.t3.small')
.with('MYSQL', () => 'db.t3.small')
.with('REDIS', () => 'cache.t3.small')
.exhaustive()

setValue('instance_type', defaultInstanceType)
}
}, [databaseInstanceTypes, setValue])

return (
<SettingsResourcesInstanceTypes
databaseInstanceTypes={formatDatabaseInstanceTypes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import SettingsResourcesInstanceTypesFeature from '../../feature/settings-resour

export interface DatabaseSettingsResourcesProps {
database?: Database
isDatabase?: boolean
isManaged?: boolean
clusterId?: string
databaseType?: DatabaseTypeEnum
displayInstanceTypesWarning?: boolean
displayStorageWarning?: boolean
isSetting?: boolean
}

export function DatabaseSettingsResources({
Expand All @@ -25,6 +25,7 @@ export function DatabaseSettingsResources({
clusterId = '',
displayInstanceTypesWarning = false,
displayStorageWarning = false,
isSetting = false,
}: DatabaseSettingsResourcesProps) {
const { control } = useFormContext()
const { organizationId = '', environmentId } = useParams()
Expand Down Expand Up @@ -142,6 +143,7 @@ export function DatabaseSettingsResources({
<SettingsResourcesInstanceTypesFeature
databaseType={databaseType}
displayWarning={displayInstanceTypesWarning}
isSetting={isSetting}
/>
)}
<Controller
Expand Down

0 comments on commit e6c3105

Please sign in to comment.