ui(admin): server startup fixes

- when a new egg is selected, set the variable values to the egg's default, not just the placeholder
- when a new egg is selected, change the server's startup command to the default; fixes #3397
- remove move un-needed code
This commit is contained in:
Matthew Penner 2021-09-16 16:46:41 -06:00
parent 98da1f13a1
commit 9b7cea5d04
No known key found for this signature in database
GPG Key ID: 030E4AB751DC756F

View File

@ -18,8 +18,18 @@ import { Actions, useStoreActions } from 'easy-peasy';
import Label from '@/components/elements/Label'; import Label from '@/components/elements/Label';
import { object } from 'yup'; import { object } from 'yup';
function ServerStartupLineContainer ({ egg }: { egg: Egg }) { function ServerStartupLineContainer ({ egg, server }: { egg: Egg; server: Server }) {
const { isSubmitting } = useFormikContext(); const { isSubmitting, setFieldValue } = useFormikContext();
useEffect(() => {
if (server.eggId === egg.id) {
setFieldValue('startup', server.container.startup);
return;
}
// Whenever the egg is changed, set the server's startup command to the egg's default.
setFieldValue('startup', egg.startup);
}, [ egg ]);
return ( return (
<AdminBox title={'Startup Command'} css={tw`relative w-full`}> <AdminBox title={'Startup Command'} css={tw`relative w-full`}>
@ -43,7 +53,7 @@ function ServerStartupLineContainer ({ egg }: { egg: Egg }) {
); );
} }
function ServerServiceContainer ({ server, egg, setEgg }: { server: Server, egg: Egg | null, setEgg: (value: Egg | null) => void }) { function ServerServiceContainer ({ egg, setEgg, server }: { egg: Egg | null, setEgg: (value: Egg | null) => void, server: Server }) {
const { isSubmitting } = useFormikContext(); const { isSubmitting } = useFormikContext();
const [ nestId, setNestId ] = useState<number | null>(server.nestId); const [ nestId, setNestId ] = useState<number | null>(server.nestId);
@ -116,40 +126,25 @@ function ServerVariableContainer ({ variable, defaultValue }: { variable: EggVar
); );
} }
function ServerStartupForm ({ server }: { server: Server }) { function ServerStartupForm ({ egg, setEgg, server }: { egg: Egg, setEgg: (value: Egg | null) => void; server: Server }) {
const { isSubmitting, isValid, setFieldValue } = useFormikContext(); const { isSubmitting, isValid } = useFormikContext();
const [ egg, setEgg ] = useState<Egg | null>(null);
useEffect(() => {
getEgg(server.eggId, [ 'variables' ])
.then(egg => {
if (egg.relations.variables === undefined) {
return;
}
egg.relations.variables?.forEach(v => setFieldValue('environment.' + v.envVariable, ''));
setEgg(egg);
})
.catch(error => console.error(error));
}, []);
if (egg === null) {
return (<></>);
}
return ( return (
<Form> <Form>
<div css={tw`flex flex-col`}> <div css={tw`flex flex-col`}>
<div css={tw`flex flex-row mb-6`}> <div css={tw`flex flex-row mb-6`}>
<ServerStartupLineContainer egg={egg}/> <ServerStartupLineContainer
egg={egg}
server={server}
/>
</div> </div>
<div css={tw`grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6 mb-6`}> <div css={tw`grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6 mb-6`}>
<div css={tw`flex`}> <div css={tw`flex`}>
<ServerServiceContainer <ServerServiceContainer
server={server}
egg={egg} egg={egg}
setEgg={setEgg} setEgg={setEgg}
server={server}
/> />
</div> </div>
@ -163,7 +158,7 @@ function ServerStartupForm ({ server }: { server: Server }) {
<ServerVariableContainer <ServerVariableContainer
key={i} key={i}
variable={v} variable={v}
defaultValue={server.relations?.variables.find(v2 => v.eggId === v2.eggId && v.envVariable === v2.envVariable)?.serverValue || ''} defaultValue={server.relations?.variables.find(v2 => v.eggId === v2.eggId && v.envVariable === v2.envVariable)?.serverValue || v.defaultValue}
/> />
))} ))}
</div> </div>
@ -225,7 +220,11 @@ export default function ServerStartupContainer ({ server }: { server: Server })
validationSchema={object().shape({ validationSchema={object().shape({
})} })}
> >
<ServerStartupForm server={server}/> <ServerStartupForm
egg={egg}
setEgg={setEgg}
server={server}
/>
</Formik> </Formik>
); );
} }