import getEgg, { Egg, EggVariable } from '@/api/admin/eggs/getEgg';
import { Server } from '@/api/admin/servers/getServers';
import updateServerStartup, { Values } from '@/api/admin/servers/updateServerStartup';
import EggSelect from '@/components/admin/servers/EggSelect';
import NestSelect from '@/components/admin/servers/NestSelect';
import { Context, ServerIncludes } from '@/components/admin/servers/ServerRouter';
import FormikSwitch from '@/components/elements/FormikSwitch';
import React, { useEffect, useState } from 'react';
import Button from '@/components/elements/Button';
import Input from '@/components/elements/Input';
import AdminBox from '@/components/admin/AdminBox';
import tw from 'twin.macro';
import Field from '@/components/elements/Field';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { ApplicationStore } from '@/state';
import { Actions, useStoreActions } from 'easy-peasy';
import Label from '@/components/elements/Label';
import { object } from 'yup';
function ServerStartupLineContainer ({ egg, server }: { egg: Egg | null; server: Server }) {
const { isSubmitting, setFieldValue } = useFormikContext();
useEffect(() => {
if (egg === null) {
return;
}
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 (
);
}
function ServerServiceContainer ({ egg, setEgg, server }: { egg: Egg | null, setEgg: (value: Egg | null) => void, server: Server }) {
const { isSubmitting } = useFormikContext();
const [ nestId, setNestId ] = useState(server.nestId);
return (
);
}
function ServerImageContainer () {
const { isSubmitting } = useFormikContext();
return (
);
}
function ServerVariableContainer ({ variable, defaultValue }: { variable: EggVariable, defaultValue: string }) {
const key = 'environment.' + variable.envVariable;
const { isSubmitting, setFieldValue } = useFormikContext();
useEffect(() => {
setFieldValue(key, defaultValue);
}, [ variable, defaultValue ]);
return (
{variable.name}
}>
);
}
function ServerStartupForm ({ egg, setEgg, server }: { egg: Egg | null, setEgg: (value: Egg | null) => void; server: Server }) {
const { isSubmitting, isValid } = useFormikContext();
return (
);
}
export default function ServerStartupContainer ({ server }: { server: Server }) {
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes);
const [ egg, setEgg ] = useState(null);
const setServer = Context.useStoreActions(actions => actions.setServer);
useEffect(() => {
getEgg(server.eggId, [ 'variables' ])
.then(egg => setEgg(egg))
.catch(error => console.error(error));
}, []);
const submit = (values: Values, { setSubmitting }: FormikHelpers) => {
clearFlashes('server');
updateServerStartup(server.id, values, ServerIncludes)
.then(s => {
setServer({ ...server, ...s });
})
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'server', error });
})
.then(() => setSubmitting(false));
};
return (
[ v.envVariable, '' ]) || []),
image: server.container.image,
eggId: server.eggId,
skipScripts: false,
}}
validationSchema={object().shape({
})}
>
);
}