import { Form, Formik, FormikHelpers, useFormikContext } from 'formik'; import React, { useState } from 'react'; import tw from 'twin.macro'; import createEggVariable, { CreateEggVariable } from '@/api/admin/eggs/createEggVariable'; import { useEggFromRoute } from '@/api/admin/egg'; import { EggVariableForm, validationSchema } from '@/components/admin/nests/eggs/EggVariablesContainer'; import Modal from '@/components/elements/Modal'; import FlashMessageRender from '@/components/FlashMessageRender'; import Button from '@/components/elements/Button'; import useFlash from '@/plugins/useFlash'; export default function NewVariableButton () { const { setValues } = useFormikContext(); const [ visible, setVisible ] = useState(false); const { clearFlashes, clearAndAddHttpError } = useFlash(); const { data: egg, mutate } = useEggFromRoute(); if (!egg) { return null; } const submit = (values: CreateEggVariable, { setSubmitting }: FormikHelpers) => { clearFlashes('variable:create'); createEggVariable(egg.id, values) .then(async (variable) => { setValues([ ...egg.relationships.variables, variable ]); await mutate(egg => ({ ...egg!, relationships: { ...egg!.relationships, variables: [ ...egg!.relationships.variables, variable ] } })); setVisible(false); }) .catch(error => { clearAndAddHttpError({ key: 'variable:create', error }); setSubmitting(false); }); }; return ( <> {({ isSubmitting, isValid, resetForm }) => ( { resetForm(); setVisible(false); }} >

New Variable

)}
); }