-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the new instance selector to the service form
- Loading branch information
Showing
5 changed files
with
226 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/modules/service-form/sections/06-instance/instance-selector.new.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useState } from 'react'; | ||
import { useController, useFormContext } from 'react-hook-form'; | ||
|
||
import { useInstance, useInstances, useRegions } from 'src/api/hooks/catalog'; | ||
import { CatalogInstance, CatalogRegion, InstanceCategory } from 'src/api/model'; | ||
import { useIsInstanceAvailable, useIsRegionAvailable } from 'src/application/instance-region-availability'; | ||
import { useGetInstanceBadges } from 'src/components/instance-selector/instance-badges'; | ||
import { InstanceCategoryTabs } from 'src/components/instance-selector/instance-category-tabs'; | ||
import { InstanceSelector as InstanceSelectorComponent } from 'src/components/instance-selector/instance-selector'; | ||
import { hasProperty } from 'src/utils/object'; | ||
|
||
import { ServiceForm } from '../../service-form.types'; | ||
import { useWatchServiceForm } from '../../use-service-form'; | ||
|
||
import { InstanceAlerts } from './instance-alerts'; | ||
|
||
export function InstanceSelectorNew() { | ||
const instances = useInstances(); | ||
const regions = useRegions(); | ||
|
||
const serviceType = useWatchServiceForm('serviceType'); | ||
const hasVolumes = useWatchServiceForm('volumes').filter((volume) => volume.name !== '').length > 0; | ||
const previousInstance = useInstance(useWatchServiceForm('meta.previousInstance')); | ||
|
||
const selectedRegions = useWatchServiceForm('regions').map( | ||
(identifier) => regions.find(hasProperty('identifier', identifier))!, | ||
); | ||
|
||
const { getValues, setValue, trigger } = useFormContext<ServiceForm>(); | ||
|
||
const { field } = useController<ServiceForm, 'instance'>({ name: 'instance' }); | ||
const instance = useInstance(field.value); | ||
|
||
const [category, setCategory] = useState<InstanceCategory>(instance?.category ?? 'standard'); | ||
|
||
const isInstanceAvailable = useIsInstanceAvailable({ serviceType, hasVolumes, previousInstance }); | ||
const isRegionAvailable = useIsRegionAvailable({ instance }); | ||
|
||
const handleInstanceSelected = (instance: CatalogInstance | null) => { | ||
if (!instance || !isInstanceAvailable(instance)) { | ||
field.onChange(null); | ||
return; | ||
} | ||
|
||
field.onChange(instance.identifier); | ||
|
||
if (instance.category === 'eco') { | ||
setValue('scaling.max', getValues('scaling.min')); | ||
void trigger('scaling'); | ||
} | ||
|
||
if (instance.identifier === 'free') { | ||
setValue('scaling.min', 1); | ||
setValue('scaling.max', 1); | ||
void trigger('scaling'); | ||
} | ||
}; | ||
|
||
const handleRegionsSelected = (regions: CatalogRegion[]) => { | ||
setValue( | ||
'regions', | ||
regions.map((region) => region.identifier), | ||
{ shouldValidate: true }, | ||
); | ||
}; | ||
|
||
const getBadges = useGetInstanceBadges({ | ||
previousInstance: useInstance(useFormContext<ServiceForm>().watch('meta.previousInstance')), | ||
}); | ||
|
||
return ( | ||
<> | ||
<InstanceCategoryTabs | ||
category={category} | ||
setCategory={setCategory} | ||
instances={instances.filter(hasProperty('regionCategory', 'koyeb')).filter(isInstanceAvailable)} | ||
setInstance={handleInstanceSelected} | ||
/> | ||
|
||
<InstanceAlerts selectedCategory={category} /> | ||
|
||
<InstanceSelectorComponent | ||
instances={instances | ||
.filter(hasProperty('regionCategory', 'koyeb')) | ||
.filter(hasProperty('category', category)) | ||
.filter(isInstanceAvailable)} | ||
selectedInstance={instances.find(hasProperty('identifier', field.value)) ?? null} | ||
onInstanceSelected={handleInstanceSelected} | ||
regions={regions.filter(isRegionAvailable)} | ||
selectedRegions={selectedRegions} | ||
onRegionsSelected={handleRegionsSelected} | ||
getBadges={getBadges} | ||
/> | ||
</> | ||
); | ||
} |
85 changes: 85 additions & 0 deletions
85
src/modules/service-form/sections/06-instance/instance-selector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { useState } from 'react'; | ||
import { useController, useFormContext } from 'react-hook-form'; | ||
|
||
import { useInstance, useInstances, useRegion } from 'src/api/hooks/catalog'; | ||
import { CatalogInstance, InstanceCategory } from 'src/api/model'; | ||
import { | ||
useInstanceAvailabilities, | ||
useRegionAvailabilities, | ||
} from 'src/application/instance-region-availability'; | ||
import { InstanceSelector as InstanceSelectorComponent } from 'src/components/instance-selector'; | ||
import { hasProperty } from 'src/utils/object'; | ||
|
||
import { ServiceForm } from '../../service-form.types'; | ||
import { useWatchServiceForm } from '../../use-service-form'; | ||
|
||
import { InstanceAlerts } from './instance-alerts'; | ||
|
||
export function InstanceSelector() { | ||
const instances = useInstances(); | ||
|
||
const serviceType = useWatchServiceForm('serviceType'); | ||
const hasVolumes = useWatchServiceForm('volumes').filter((volume) => volume.name !== '').length > 0; | ||
const previousInstance = useInstance(useWatchServiceForm('meta.previousInstance')); | ||
|
||
const regions = useWatchServiceForm('regions'); | ||
const firstRegion = useRegion(regions[0]); | ||
|
||
const instanceAvailabilities = useInstanceAvailabilities({ serviceType, hasVolumes, previousInstance }); | ||
const regionAvailabilities = useRegionAvailabilities(); | ||
|
||
const { getValues, setValue, trigger } = useFormContext<ServiceForm>(); | ||
|
||
const { field } = useController<ServiceForm, 'instance'>({ name: 'instance' }); | ||
const instance = useInstance(field.value); | ||
|
||
const [category, setCategory] = useState<InstanceCategory>(instance?.category ?? 'standard'); | ||
|
||
const handleInstanceSelected = (instance: CatalogInstance | null) => { | ||
const [isAvailable] = instance ? (instanceAvailabilities[instance.identifier] ?? [false]) : [false]; | ||
|
||
if (!instance || !isAvailable) { | ||
field.onChange(null); | ||
return; | ||
} | ||
|
||
field.onChange(instance.identifier); | ||
|
||
if (instance.category === 'eco') { | ||
setValue('scaling.max', getValues('scaling.min')); | ||
void trigger('scaling'); | ||
} | ||
|
||
if (instance.identifier === 'free') { | ||
setValue('scaling.min', 1); | ||
setValue('scaling.max', 1); | ||
void trigger('scaling'); | ||
} | ||
|
||
let availableRegions = getValues('regions') | ||
.filter((region) => regionAvailabilities[region]?.[0]) | ||
.filter((region) => instance.regions?.includes(region)); | ||
|
||
if (availableRegions.length === 0) { | ||
availableRegions = [instance?.regions?.[0] ?? 'fra']; | ||
} | ||
|
||
setValue('regions', availableRegions, { shouldValidate: true }); | ||
}; | ||
|
||
return ( | ||
<> | ||
<InstanceAlerts selectedCategory={category} /> | ||
|
||
<InstanceSelectorComponent | ||
instances={instances.filter(hasProperty('regionCategory', firstRegion?.category ?? 'koyeb'))} | ||
checkAvailability={(instance) => instanceAvailabilities[instance] ?? [false, 'instanceNotFound']} | ||
selectedInstance={instances.find(hasProperty('identifier', field.value)) ?? null} | ||
previousInstance={previousInstance} | ||
onInstanceSelected={handleInstanceSelected} | ||
onCategoryChanged={setCategory} | ||
className="w-full" | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters