2021-10-03 21:23:06 +01:00
|
|
|
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
|
|
|
|
import React, { useEffect } from 'react';
|
2021-10-01 18:07:48 +01:00
|
|
|
import tw from 'twin.macro';
|
2021-10-03 21:23:06 +01:00
|
|
|
import { array, boolean, object, string } from 'yup';
|
2021-10-01 18:07:48 +01:00
|
|
|
import { Egg, EggVariable } from '@/api/admin/eggs/getEgg';
|
2021-10-03 21:23:06 +01:00
|
|
|
import updateEggVariables from '@/api/admin/eggs/updateEggVariables';
|
2021-05-20 23:00:46 +01:00
|
|
|
import AdminBox from '@/components/admin/AdminBox';
|
2021-10-03 21:23:06 +01:00
|
|
|
import Button from '@/components/elements/Button';
|
2021-10-01 18:07:48 +01:00
|
|
|
import Checkbox from '@/components/elements/Checkbox';
|
|
|
|
import Field, { FieldRow, TextareaField } from '@/components/elements/Field';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2021-10-03 21:23:06 +01:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
2021-10-01 18:07:48 +01:00
|
|
|
|
2021-10-03 21:23:06 +01:00
|
|
|
function EggVariableForm ({ variable: { name }, i }: { variable: EggVariable, i: number }) {
|
2021-10-01 18:07:48 +01:00
|
|
|
const { isSubmitting } = useFormikContext();
|
2021-05-20 23:00:46 +01:00
|
|
|
|
|
|
|
return (
|
2021-10-01 18:07:48 +01:00
|
|
|
<AdminBox css={tw`relative w-full`} title={<p css={tw`text-sm uppercase`}>{name}</p>}>
|
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
|
|
|
|
|
|
|
<Field
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].name`}
|
|
|
|
name={`[${i}].name`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'Name'}
|
|
|
|
type={'text'}
|
|
|
|
css={tw`mb-6`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<TextareaField
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].description`}
|
|
|
|
name={`[${i}].description`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'Description'}
|
|
|
|
rows={3}
|
|
|
|
css={tw`mb-4`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FieldRow>
|
|
|
|
<Field
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].envVariable`}
|
|
|
|
name={`[${i}].envVariable`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'Environment Variable'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Field
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].defaultValue`}
|
|
|
|
name={`[${i}].defaultValue`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'Default Value'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</FieldRow>
|
|
|
|
|
|
|
|
<div css={tw`flex flex-row mb-6`}>
|
|
|
|
<Checkbox
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].userViewable`}
|
|
|
|
name={`[${i}].userViewable`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'User Viewable'}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Checkbox
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].userEditable`}
|
|
|
|
name={`[${i}].userEditable`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'User Editable'}
|
|
|
|
css={tw`ml-auto`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Field
|
2021-10-03 21:23:06 +01:00
|
|
|
id={`[${i}].rules`}
|
|
|
|
name={`[${i}].rules`}
|
2021-10-01 18:07:48 +01:00
|
|
|
label={'Validation Rules'}
|
|
|
|
type={'text'}
|
|
|
|
css={tw`mb-2`}
|
|
|
|
/>
|
2021-05-20 23:00:46 +01:00
|
|
|
</AdminBox>
|
|
|
|
);
|
2021-10-01 18:07:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function EggVariablesContainer ({ egg }: { egg: Egg }) {
|
2021-10-03 21:23:06 +01:00
|
|
|
const { clearAndAddHttpError } = useFlash();
|
|
|
|
|
|
|
|
const submit = (values: EggVariable[], { setSubmitting }: FormikHelpers<EggVariable[]>) => {
|
|
|
|
updateEggVariables(egg.id, values)
|
|
|
|
.then(variables => console.log(variables))
|
|
|
|
.catch(error => clearAndAddHttpError({ key: 'egg', error }))
|
|
|
|
.then(() => setSubmitting(false));
|
2021-10-01 18:07:48 +01:00
|
|
|
};
|
|
|
|
|
2021-10-03 21:23:06 +01:00
|
|
|
useEffect(() => {
|
|
|
|
console.log(egg.relations?.variables || []);
|
|
|
|
}, []);
|
|
|
|
|
2021-10-01 18:07:48 +01:00
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
2021-10-03 21:23:06 +01:00
|
|
|
initialValues={egg.relations?.variables || []}
|
|
|
|
validationSchema={array().of(
|
|
|
|
object().shape({
|
|
|
|
name: string().required().min(1).max(191),
|
|
|
|
description: string(),
|
|
|
|
envVariable: string().required().min(1).max(191),
|
|
|
|
defaultValue: string(),
|
|
|
|
userViewable: boolean().required(),
|
|
|
|
userEditable: boolean().required(),
|
|
|
|
rules: string().required(),
|
|
|
|
}),
|
|
|
|
)}
|
2021-10-01 18:07:48 +01:00
|
|
|
>
|
2021-10-03 21:23:06 +01:00
|
|
|
{({ isSubmitting, isValid }) => (
|
|
|
|
<Form>
|
|
|
|
<div css={tw`flex flex-col mb-16`}>
|
|
|
|
<div css={tw`grid grid-cols-1 lg:grid-cols-2 gap-x-8 gap-y-6`}>
|
|
|
|
{egg.relations?.variables?.map((v, i) => <EggVariableForm key={v.id} i={i} variable={v}/>)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div css={tw`bg-neutral-700 rounded shadow-md py-2 pr-6 mt-6`}>
|
|
|
|
<div css={tw`flex flex-row`}>
|
|
|
|
<Button type="submit" size="small" css={tw`ml-auto`} disabled={isSubmitting || !isValid}>
|
|
|
|
Save Changes
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)}
|
2021-10-01 18:07:48 +01:00
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
}
|