import updateEgg from '@/api/admin/eggs/updateEgg'; import EggDeleteButton from '@/components/admin/nests/eggs/EggDeleteButton'; import Button from '@/components/elements/Button'; import Editor from '@/components/elements/Editor'; import Field, { TextareaField } from '@/components/elements/Field'; import Input from '@/components/elements/Input'; import Label from '@/components/elements/Label'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import useFlash from '@/plugins/useFlash'; import { jsonLanguage } from '@codemirror/lang-json'; import { faDocker } from '@fortawesome/free-brands-svg-icons'; import { faEgg, faFireAlt, faMicrochip, faTerminal } from '@fortawesome/free-solid-svg-icons'; import React, { forwardRef, useImperativeHandle, useRef } from 'react'; import AdminBox from '@/components/admin/AdminBox'; import { Egg } from '@/api/admin/eggs/getEgg'; import { useHistory } from 'react-router-dom'; import tw from 'twin.macro'; import { object } from 'yup'; import { Form, Formik, FormikHelpers, useFormikContext } from 'formik'; function EggInformationContainer () { const { isSubmitting } = useFormikContext(); return ( ); } function EggDetailsContainer ({ egg }: { egg: Egg }) { return (
); } function EggStartupContainer ({ className }: { className?: string }) { const { isSubmitting } = useFormikContext(); return ( ); } function EggImageContainer () { const { isSubmitting } = useFormikContext(); return ( ); } function EggLifecycleContainer () { const { isSubmitting } = useFormikContext(); return ( ); } interface EggProcessContainerProps { className?: string; egg: Egg; } interface EggProcessContainerRef { getStartupConfiguration: () => Promise; getFilesConfiguration: () => Promise; } const EggProcessContainer = forwardRef( function EggProcessContainer ({ className, egg }, ref) { const { isSubmitting } = useFormikContext(); let fetchStartupConfiguration: (() => Promise) | null = null; let fetchFilesConfiguration: (() => Promise) | null = null; useImperativeHandle(ref, () => ({ getStartupConfiguration: async () => { if (fetchStartupConfiguration === null) { return new Promise(resolve => resolve(null)); } return await fetchStartupConfiguration(); }, getFilesConfiguration: async () => { if (fetchFilesConfiguration === null) { return new Promise(resolve => resolve(null)); } return await fetchFilesConfiguration(); }, })); return (
{ fetchStartupConfiguration = value; }} />
{ fetchFilesConfiguration = value; }} />
); } ); interface Values { name: string; description: string; startup: string; dockerImages: string; stopCommand: string; configStartup: string; configFiles: string; } export default function EggSettingsContainer ({ egg }: { egg: Egg }) { const history = useHistory(); const { clearFlashes, clearAndAddHttpError } = useFlash(); const ref = useRef(); const submit = async (values: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('egg'); values.configStartup = await ref.current?.getStartupConfiguration() || ''; values.configFiles = await ref.current?.getFilesConfiguration() || ''; updateEgg(egg.id, { ...values, dockerImages: values.dockerImages.split('\n') }) .catch(error => { console.error(error); clearAndAddHttpError({ key: 'egg', error }); }) .then(() => setSubmitting(false)); }; return ( {({ isSubmitting, isValid }) => (
history.push('/admin/nests')} />
)}
); }