forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameServerBox.tsx
More file actions
76 lines (70 loc) · 2.9 KB
/
RenameServerBox.tsx
File metadata and controls
76 lines (70 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react';
import { ServerContext } from '@/state/server';
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import { Field as FormikField, Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Actions, useStoreActions } from 'easy-peasy';
import renameServer from '@/api/server/renameServer';
import Field from '@/components/elements/Field';
import { object, string } from 'yup';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http';
import { Button } from '@/components/elements/button/index';
import tw from 'twin.macro';
import Label from '@/components/elements/Label';
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
import { Textarea } from '@/components/elements/Input';
interface Values {
name: string;
description: string;
}
const RenameServerBox = () => {
const { isSubmitting } = useFormikContext<Values>();
return (
<TitledGreyBox title={'Change Server Details'} css={tw`relative`}>
<SpinnerOverlay visible={isSubmitting} />
<Form css={tw`mb-0`}>
<Field id={'name'} name={'name'} label={'Server Name'} type={'text'} />
<div css={tw`mt-6`}>
<Label>Server Description</Label>
<FormikFieldWrapper name={'description'}>
<FormikField as={Textarea} name={'description'} rows={3} />
</FormikFieldWrapper>
</div>
<div css={tw`mt-6 text-right`}>
<Button type={'submit'}>Save</Button>
</div>
</Form>
</TitledGreyBox>
);
};
export default () => {
const server = ServerContext.useStoreState((state) => state.server.data!);
const setServer = ServerContext.useStoreActions((actions) => actions.server.setServer);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers<Values>) => {
clearFlashes('settings');
renameServer(server.uuid, name, description)
.then(() => setServer({ ...server, name, description }))
.catch((error) => {
console.error(error);
addError({ key: 'settings', message: httpErrorToHuman(error) });
})
.then(() => setSubmitting(false));
};
return (
<Formik
onSubmit={submit}
initialValues={{
name: server.name,
description: server.description,
}}
validationSchema={object().shape({
name: string().required().min(1),
description: string().nullable(),
})}
>
<RenameServerBox />
</Formik>
);
};