diff --git a/resources/scripts/api/admin/egg.ts b/resources/scripts/api/admin/egg.ts index 8f9f0284b..69b28c4b8 100644 --- a/resources/scripts/api/admin/egg.ts +++ b/resources/scripts/api/admin/egg.ts @@ -56,7 +56,7 @@ export interface EggVariable extends Model { * A standard API response with the minimum viable details for the frontend * to correctly render a egg. */ -type LoadedEgg = WithRelationships; +export type LoadedEgg = WithRelationships; /** * Gets a single egg from the database and returns it. diff --git a/resources/scripts/components/admin/servers/EggSelect.tsx b/resources/scripts/components/admin/servers/EggSelect.tsx index c6b788274..12ab72496 100644 --- a/resources/scripts/components/admin/servers/EggSelect.tsx +++ b/resources/scripts/components/admin/servers/EggSelect.tsx @@ -3,7 +3,7 @@ import type { ChangeEvent } from 'react'; import { useEffect, useState } from 'react'; import type { WithRelationships } from '@/api/admin'; -import type { Egg } from '@/api/admin/egg'; +import type {Egg, LoadedEgg} from '@/api/admin/egg'; import { searchEggs } from '@/api/admin/egg'; import Label from '@/components/elements/Label'; import Select from '@/components/elements/Select'; @@ -11,16 +11,16 @@ import Select from '@/components/elements/Select'; interface Props { nestId?: number; selectedEggId?: number; - onEggSelect: (egg: Egg | null) => void; + onEggSelect: (egg: WithRelationships | undefined) => void; } export default ({ nestId, selectedEggId, onEggSelect }: Props) => { const [, , { setValue, setTouched }] = useField>('environment'); - const [eggs, setEggs] = useState[] | null>(null); + const [eggs, setEggs] = useState[] | undefined>(undefined); - const selectEgg = (egg: Egg | null) => { - if (egg === null) { - onEggSelect(null); + const selectEgg = (egg: WithRelationships | undefined) => { + if (egg === undefined) { + onEggSelect(undefined); return; } @@ -40,26 +40,29 @@ export default ({ nestId, selectedEggId, onEggSelect }: Props) => { useEffect(() => { if (!nestId) { - setEggs(null); + setEggs(undefined); return; } searchEggs(nestId, {}) - .then(eggs => { - setEggs(eggs); - selectEgg(eggs[0] || null); + .then(_eggs => { + setEggs(_eggs); + + // If the currently selected egg is in the selected nest, use it instead of picking the first egg on the nest. + const egg = _eggs.find(egg => egg.id === selectedEggId) ?? _eggs[0]; + selectEgg(egg); }) .catch(error => console.error(error)); }, [nestId]); - const onSelectChange = (e: ChangeEvent) => { - selectEgg(eggs?.find(egg => egg.id.toString() === e.currentTarget.value) || null); + const onSelectChange = (event: ChangeEvent) => { + selectEgg(eggs?.find(egg => egg.id.toString() === event.currentTarget.value) ?? undefined); }; return ( <> - {!eggs ? ( ) : ( diff --git a/resources/scripts/components/admin/servers/NewServerContainer.tsx b/resources/scripts/components/admin/servers/NewServerContainer.tsx index 58432b333..828305c49 100644 --- a/resources/scripts/components/admin/servers/NewServerContainer.tsx +++ b/resources/scripts/components/admin/servers/NewServerContainer.tsx @@ -30,6 +30,7 @@ import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; +import {WithRelationships} from "@/api/admin"; function InternalForm() { const { @@ -39,12 +40,12 @@ function InternalForm() { values: { environment }, } = useFormikContext(); - const [egg, setEgg] = useState(null); - const [node, setNode] = useState(null); - const [allocations, setAllocations] = useState(null); + const [egg, setEgg] = useState | undefined>(undefined); + const [node, setNode] = useState(undefined); + const [allocations, setAllocations] = useState(undefined); useEffect(() => { - if (egg === null) { + if (egg === undefined) { return; } @@ -54,7 +55,7 @@ function InternalForm() { }, [egg]); useEffect(() => { - if (node === null) { + if (node === undefined) { return; } @@ -77,20 +78,20 @@ function InternalForm() { - + -
- -
+
+ +