misc_pterodactyl-panel/resources/scripts/components/admin/nests/eggs/NewVariableButton.tsx

94 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-11-04 20:32:42 +00:00
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
2021-10-03 21:51:35 +00:00
import React, { useState } from 'react';
import tw from 'twin.macro';
2021-11-04 20:32:42 +00:00
import createEggVariable, { CreateEggVariable } from '@/api/admin/eggs/createEggVariable';
import { useEggFromRoute } from '@/api/admin/egg';
2021-10-03 21:51:35 +00:00
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';
2021-11-04 20:32:42 +00:00
export default function NewVariableButton () {
const { setValues } = useFormikContext();
2021-10-03 21:51:35 +00:00
const [ visible, setVisible ] = useState(false);
const { clearFlashes, clearAndAddHttpError } = useFlash();
2021-11-04 20:32:42 +00:00
const { data: egg, mutate } = useEggFromRoute();
2021-10-03 21:51:35 +00:00
2021-11-04 20:32:42 +00:00
if (!egg) {
return null;
}
const submit = (values: CreateEggVariable, { setSubmitting }: FormikHelpers<CreateEggVariable>) => {
2021-10-03 21:51:35 +00:00
clearFlashes('variable:create');
2021-11-04 20:32:42 +00:00
createEggVariable(egg.id, values)
.then(async (variable) => {
setValues([ ...egg.relationships.variables, variable ]);
await mutate(egg => ({ ...egg!, relationships: { ...egg!.relationships, variables: [ ...egg!.relationships.variables, variable ] } }));
2021-10-03 21:51:35 +00:00
setVisible(false);
})
.catch(error => {
clearAndAddHttpError({ key: 'variable:create', error });
setSubmitting(false);
});
};
return (
<>
<Formik
onSubmit={submit}
initialValues={{
name: '',
description: '',
2021-11-04 20:32:42 +00:00
environmentVariable: '',
2021-10-03 21:51:35 +00:00
defaultValue: '',
2021-11-04 20:32:42 +00:00
isUserViewable: false,
isUserEditable: false,
2021-10-03 21:51:35 +00:00
rules: '',
}}
validationSchema={validationSchema}
>
2021-11-04 20:32:42 +00:00
{({ isSubmitting, isValid, resetForm }) => (
2021-10-03 21:51:35 +00:00
<Modal
visible={visible}
dismissable={!isSubmitting}
showSpinnerOverlay={isSubmitting}
onDismissed={() => {
resetForm();
setVisible(false);
}}
>
<FlashMessageRender byKey={'variable:create'} css={tw`mb-6`}/>
<h2 css={tw`mb-6 text-2xl text-neutral-100`}>New Variable</h2>
<Form css={tw`m-0`}>
<EggVariableForm prefix={''}/>
<div css={tw`flex flex-wrap justify-end mt-6`}>
<Button
type={'button'}
isSecondary
css={tw`w-full sm:w-auto sm:mr-2`}
onClick={() => setVisible(false)}
>
Cancel
</Button>
2021-11-04 20:32:42 +00:00
<Button css={tw`w-full mt-4 sm:w-auto sm:mt-0`} type={'submit'} disabled={isSubmitting || !isValid}>
2021-10-03 21:51:35 +00:00
Create Variable
</Button>
</div>
</Form>
</Modal>
)}
</Formik>
<Button type={'button'} color={'green'} onClick={() => setVisible(true)}>
2021-10-23 19:13:25 +00:00
New Variable
2021-10-03 21:51:35 +00:00
</Button>
</>
);
}