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

211 lines
7.8 KiB
TypeScript
Raw Normal View History

import deleteEggVariable from '@/api/admin/eggs/deleteEggVariable';
2021-10-23 19:13:25 +00:00
import { NoItems } from '@/components/admin/AdminTable';
import ConfirmationModal from '@/components/elements/ConfirmationModal';
2021-10-03 20:23:06 +00:00
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import React, { useState } from 'react';
2021-10-01 17:07:48 +00:00
import tw from 'twin.macro';
2021-10-03 20:23:06 +00:00
import { array, boolean, object, string } from 'yup';
2021-11-04 20:32:42 +00:00
import { EggVariable, useEggFromRoute } from '@/api/admin/egg';
2021-10-03 20:23:06 +00:00
import updateEggVariables from '@/api/admin/eggs/updateEggVariables';
2021-10-03 21:51:35 +00:00
import NewVariableButton from '@/components/admin/nests/eggs/NewVariableButton';
import AdminBox from '@/components/admin/AdminBox';
2021-10-03 20:23:06 +00:00
import Button from '@/components/elements/Button';
2021-10-01 17:07:48 +00:00
import Checkbox from '@/components/elements/Checkbox';
import Field, { FieldRow, TextareaField } from '@/components/elements/Field';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
2021-10-03 20:23:06 +00:00
import useFlash from '@/plugins/useFlash';
import { TrashIcon } from '@heroicons/react/outline';
2021-10-01 17:07:48 +00:00
2021-10-03 21:51:35 +00:00
export const validationSchema = object().shape({
name: string().required().min(1).max(191),
description: string(),
2021-11-04 20:32:42 +00:00
environmentVariable: string().required().min(1).max(191),
2021-10-03 21:51:35 +00:00
defaultValue: string(),
2021-11-04 20:32:42 +00:00
isUserViewable: boolean().required(),
isUserEditable: boolean().required(),
2021-10-03 21:51:35 +00:00
rules: string().required(),
});
2021-10-03 21:51:35 +00:00
export function EggVariableForm ({ prefix }: { prefix: string }) {
return (
2021-10-03 21:51:35 +00:00
<>
2021-10-01 17:07:48 +00:00
<Field
2021-10-03 21:51:35 +00:00
id={`${prefix}name`}
name={`${prefix}name`}
2021-10-01 17:07:48 +00:00
label={'Name'}
type={'text'}
css={tw`mb-6`}
/>
<TextareaField
2021-10-03 21:51:35 +00:00
id={`${prefix}description`}
name={`${prefix}description`}
2021-10-01 17:07:48 +00:00
label={'Description'}
rows={3}
css={tw`mb-4`}
/>
<FieldRow>
<Field
2021-11-04 20:32:42 +00:00
id={`${prefix}environmentVariable`}
name={`${prefix}environmentVariable`}
2021-10-01 17:07:48 +00:00
label={'Environment Variable'}
type={'text'}
/>
<Field
2021-10-03 21:51:35 +00:00
id={`${prefix}defaultValue`}
name={`${prefix}defaultValue`}
2021-10-01 17:07:48 +00:00
label={'Default Value'}
type={'text'}
/>
</FieldRow>
<div css={tw`flex flex-row mb-6`}>
<Checkbox
2021-11-04 20:32:42 +00:00
id={`${prefix}isUserViewable`}
name={`${prefix}isUserViewable`}
2021-10-01 17:07:48 +00:00
label={'User Viewable'}
/>
<Checkbox
2021-11-04 20:32:42 +00:00
id={`${prefix}isUserEditable`}
name={`${prefix}isUserEditable`}
2021-10-01 17:07:48 +00:00
label={'User Editable'}
css={tw`ml-auto`}
/>
</div>
<Field
2021-10-03 21:51:35 +00:00
id={`${prefix}rules`}
name={`${prefix}rules`}
2021-10-01 17:07:48 +00:00
label={'Validation Rules'}
type={'text'}
css={tw`mb-2`}
/>
2021-10-03 21:51:35 +00:00
</>
);
}
function EggVariableDeleteButton ({ onClick }: { onClick: (success: () => void) => void }) {
const [ visible, setVisible ] = useState(false);
const [ loading, setLoading ] = useState(false);
const onDelete = () => {
setLoading(true);
onClick(() => {
2021-11-04 20:32:42 +00:00
//setLoading(false);
});
};
return (
<>
<ConfirmationModal
visible={visible}
title={'Delete variable?'}
buttonText={'Yes, delete variable'}
onConfirmed={onDelete}
showSpinnerOverlay={loading}
onModalDismissed={() => setVisible(false)}
>
Are you sure you want to delete this variable? Deleting this variable will delete it from every server
using this egg.
</ConfirmationModal>
<button
type={'button'}
css={tw`ml-auto text-neutral-500 hover:text-neutral-300`}
onClick={() => setVisible(true)}
>
<TrashIcon css={tw`h-5 w-5`}/>
</button>
</>
);
}
function EggVariableBox ({ onDeleteClick, variable, prefix }: { onDeleteClick: (success: () => void) => void, variable: EggVariable, prefix: string }) {
2021-10-03 21:51:35 +00:00
const { isSubmitting } = useFormikContext();
return (
<AdminBox
css={tw`relative w-full`}
title={<p css={tw`text-sm uppercase`}>{variable.name}</p>}
button={<EggVariableDeleteButton onClick={onDeleteClick}/>}
>
2021-10-03 21:51:35 +00:00
<SpinnerOverlay visible={isSubmitting}/>
<EggVariableForm prefix={prefix}/>
</AdminBox>
);
2021-10-01 17:07:48 +00:00
}
2021-11-04 20:32:42 +00:00
export default function EggVariablesContainer () {
2021-10-03 20:23:06 +00:00
const { clearAndAddHttpError } = useFlash();
2021-11-04 20:32:42 +00:00
const { data: egg, mutate } = useEggFromRoute();
if (!egg) {
return null;
}
2021-10-03 21:51:35 +00:00
2021-10-03 20:23:06 +00:00
const submit = (values: EggVariable[], { setSubmitting }: FormikHelpers<EggVariable[]>) => {
updateEggVariables(egg.id, values)
2021-11-04 20:32:42 +00:00
.then(async () => await mutate())
2021-10-03 20:23:06 +00:00
.catch(error => clearAndAddHttpError({ key: 'egg', error }))
.then(() => setSubmitting(false));
2021-10-01 17:07:48 +00:00
};
return (
<Formik
onSubmit={submit}
2021-11-04 20:32:42 +00:00
initialValues={egg.relationships.variables}
2021-10-03 21:51:35 +00:00
validationSchema={array().of(validationSchema)}
2021-10-01 17:07:48 +00:00
>
2021-10-03 20:23:06 +00:00
{({ isSubmitting, isValid }) => (
<Form>
<div css={tw`flex flex-col mb-16`}>
2021-11-04 20:32:42 +00:00
{egg.relationships.variables?.length === 0 ?
2021-10-23 19:13:25 +00:00
<NoItems css={tw`bg-neutral-700 rounded-md shadow-md`}/>
:
2021-11-04 20:32:42 +00:00
<div css={tw`grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-x-8 gap-y-6`}>
{egg.relationships.variables.map((v, i) => (
2021-10-23 19:13:25 +00:00
<EggVariableBox
key={i}
prefix={`[${i}].`}
variable={v}
onDeleteClick={(success) => {
deleteEggVariable(egg.id, v.id)
.then(async () => {
await mutate(egg => ({
...egg!,
2021-11-04 20:32:42 +00:00
relationships: {
...egg!.relationships,
variables: egg!.relationships.variables!.filter(v2 => v.id === v2.id),
2021-10-23 19:13:25 +00:00
},
}));
success();
})
.catch(error => clearAndAddHttpError({ key: 'egg', error }));
}}
/>
))}
</div>
}
2021-10-03 20:23:06 +00:00
2021-10-03 21:51:35 +00:00
<div css={tw`bg-neutral-700 rounded shadow-md py-2 px-4 mt-6`}>
2021-10-03 20:23:06 +00:00
<div css={tw`flex flex-row`}>
2021-11-04 20:32:42 +00:00
<NewVariableButton/>
2021-10-03 21:51:35 +00:00
<Button type={'submit'} size={'small'} css={tw`ml-auto`} disabled={isSubmitting || !isValid}>
2021-10-03 20:23:06 +00:00
Save Changes
</Button>
</div>
</div>
</div>
</Form>
)}
2021-10-01 17:07:48 +00:00
</Formik>
);
}