import Label from '@/components/elements/Label'; import Select from '@/components/elements/Select'; import { useField } from 'formik'; import React, { useEffect, useState } from 'react'; import { Egg, searchEggs } from '@/api/admin/egg'; import { WithRelationships } from '@/api/admin'; interface Props { nestId?: number; selectedEggId?: number; onEggSelect: (egg: Egg | null) => void; } export default ({ nestId, selectedEggId, onEggSelect }: Props) => { const [ , , { setValue, setTouched } ] = useField>('environment'); const [ eggs, setEggs ] = useState[] | null>(null); useEffect(() => { if (!nestId) return setEggs(null); searchEggs(nestId, {}).then(eggs => { setEggs(eggs); onEggSelect(eggs[0] || null); }).catch(error => console.error(error)); }, [ nestId ]); const onSelectChange = (e: React.ChangeEvent) => { if (!eggs) return; const match = eggs.find(egg => String(egg.id) === e.currentTarget.value); if (!match) return onEggSelect(null); // Ensure that only new egg variables are present in the record storing all // of the possible variables. This ensures the fields are controlled, rather // than uncontrolled when a user begins typing in them. setValue(match.relationships.variables.reduce((obj, value) => ({ ...obj, [value.environmentVariable]: undefined, }), {})); setTouched(true); onEggSelect(match); }; return ( <> ); };