import { Egg, EggVariable, getEgg } from '@/api/admin/egg';
import updateServerStartup, { Values } from '@/api/admin/servers/updateServerStartup';
import EggSelect from '@/components/admin/servers/EggSelect';
import NestSelector from '@/components/admin/servers/NestSelector';
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, useField, useFormikContext } from 'formik';
import { ApplicationStore } from '@/state';
import { Actions, useStoreActions } from 'easy-peasy';
import Label from '@/components/elements/Label';
import { object } from 'yup';
import { Server, useServerFromRoute } from '@/api/admin/server';
import { InferModel } from '@/api/admin';
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 ]);
return (
);
}
export function ServerServiceContainer ({ egg, setEgg, nestId: _nestId }: { egg: Egg | null, setEgg: (value: Egg | null) => void, nestId: number }) {
const { isSubmitting } = useFormikContext();
const [ nestId, setNestId ] = useState(_nestId);
return (
);
}
export function ServerImageContainer () {
const { isSubmitting } = useFormikContext();
return (
);
}
export function ServerVariableContainer ({ variable, value }: { variable: EggVariable, value?: string }) {
const key = 'environment.' + variable.environmentVariable;
const [ , , { setValue, setTouched } ] = useField(key);
const { isSubmitting } = useFormikContext();
useEffect(() => {
if (value === undefined) {
return;
}
setValue(value);
setTouched(true);
}, [ value ]);
return (
{variable.name}
}>
);
}
function ServerStartupForm ({ egg, setEgg, server }: { egg: Egg | null, setEgg: (value: Egg | null) => void; server: Server }) {
const { isSubmitting, isValid, values: { environment } } = useFormikContext();
return (
);
}
export default () => {
const { data: server } = useServerFromRoute();
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes);
const [ egg, setEgg ] = useState | null>(null);
useEffect(() => {
if (!server) return;
getEgg(server.eggId)
.then(egg => setEgg(egg))
.catch(error => console.error(error));
}, [ server?.eggId ]);
if (!server) return null;
const submit = (values: Values, { setSubmitting }: FormikHelpers) => {
clearFlashes('server');
updateServerStartup(server.id, values)
// .then(s => {
// mutate(data => { ...data, ...s });
// })
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'server', error });
})
.then(() => setSubmitting(false));
};
return (
,
image: server.container.image,
eggId: server.eggId,
skipScripts: false,
}}
validationSchema={object().shape({})}
>
);
};