misc_pterodactyl-panel/resources/scripts/components/admin/roles/RoleEditContainer.tsx

188 lines
6.7 KiB
TypeScript
Raw Normal View History

2021-08-02 16:08:52 +00:00
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
2021-08-02 18:29:29 +00:00
import { Form, Formik, FormikHelpers } from 'formik';
import React, { useEffect, useState } from 'react';
2021-08-02 18:29:29 +00:00
import { useHistory } from 'react-router';
import { useRouteMatch } from 'react-router-dom';
2021-08-02 16:08:52 +00:00
import tw from 'twin.macro';
import { object, string } from 'yup';
import { Role } from '@/api/admin/roles/getRoles';
import getRole from '@/api/admin/roles/getRole';
2021-08-02 16:08:52 +00:00
import updateRole from '@/api/admin/roles/updateRole';
2021-08-02 18:29:29 +00:00
import FlashMessageRender from '@/components/FlashMessageRender';
2021-08-02 16:08:52 +00:00
import AdminBox from '@/components/admin/AdminBox';
2021-08-02 18:29:29 +00:00
import AdminContentBlock from '@/components/admin/AdminContentBlock';
import RoleDeleteButton from '@/components/admin/roles/RoleDeleteButton';
2021-08-02 16:08:52 +00:00
import Button from '@/components/elements/Button';
2021-08-02 18:29:29 +00:00
import Field from '@/components/elements/Field';
import Spinner from '@/components/elements/Spinner';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { ApplicationStore } from '@/state';
interface ctx {
role: Role | undefined;
setRole: Action<ctx, Role | undefined>;
}
export const Context = createContextStore<ctx>({
role: undefined,
setRole: action((state, payload) => {
state.role = payload;
}),
});
2021-08-02 16:08:52 +00:00
interface Values {
name: string;
description: string;
}
const EditInformationContainer = () => {
2021-08-02 18:29:29 +00:00
const history = useHistory();
2021-08-02 16:08:52 +00:00
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const role = Context.useStoreState(state => state.role);
const setRole = Context.useStoreActions(actions => actions.setRole);
if (role === undefined) {
return (
<></>
);
}
const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers<Values>) => {
clearFlashes('role');
updateRole(role.id, name, description)
.then(() => setRole({ ...role, name, description }))
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'role', error });
})
.then(() => setSubmitting(false));
};
return (
<Formik
onSubmit={submit}
initialValues={{
name: role.name,
description: role.description || '',
}}
validationSchema={object().shape({
name: string().required().min(1),
description: string().max(255, ''),
})}
>
{
({ isSubmitting, isValid }) => (
<React.Fragment>
<AdminBox title={'Edit Role'} css={tw`relative`}>
<SpinnerOverlay visible={isSubmitting}/>
<Form css={tw`mb-0`}>
<div>
<Field
id={'name'}
name={'name'}
label={'Name'}
type={'text'}
/>
</div>
<div css={tw`mt-6`}>
<Field
id={'description'}
name={'description'}
label={'description'}
type={'text'}
/>
</div>
<div css={tw`w-full flex flex-row items-center mt-6`}>
2021-08-02 18:29:29 +00:00
<div css={tw`flex`}>
<RoleDeleteButton
roleId={role.id}
onDeleted={() => history.push('/admin/roles')}
/>
</div>
2021-08-02 16:08:52 +00:00
<div css={tw`flex ml-auto`}>
<Button type={'submit'} disabled={isSubmitting || !isValid}>
Save Changes
2021-08-02 16:08:52 +00:00
</Button>
</div>
</div>
</Form>
</AdminBox>
</React.Fragment>
)
}
</Formik>
);
};
const RoleEditContainer = () => {
const match = useRouteMatch<{ id?: string }>();
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const [ loading, setLoading ] = useState(true);
const role = Context.useStoreState(state => state.role);
const setRole = Context.useStoreActions(actions => actions.setRole);
useEffect(() => {
clearFlashes('role');
getRole(Number(match.params?.id))
.then(role => setRole(role))
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'role', error });
})
.then(() => setLoading(false));
}, []);
if (loading || role === undefined) {
return (
<AdminContentBlock>
<FlashMessageRender byKey={'role'} css={tw`mb-4`}/>
<div css={tw`w-full flex flex-col items-center justify-center`} style={{ height: '24rem' }}>
<Spinner size={'base'}/>
</div>
</AdminContentBlock>
);
}
return (
<AdminContentBlock title={'Role - ' + role.name}>
<div css={tw`w-full flex flex-row items-center mb-8`}>
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>{role.name}</h2>
{
(role.description || '').length < 1 ?
<p css={tw`text-base text-neutral-400`}>
<span css={tw`italic`}>No description</span>
</p>
:
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>{role.description}</p>
}
</div>
</div>
<FlashMessageRender byKey={'role'} css={tw`mb-4`}/>
2021-08-02 16:08:52 +00:00
<EditInformationContainer/>
</AdminContentBlock>
);
};
export default () => {
return (
<Context.Provider>
<RoleEditContainer/>
</Context.Provider>
);
};