2019-12-10 06:03:10 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
2020-03-28 23:47:32 +00:00
|
|
|
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
2019-12-10 06:03:10 +00:00
|
|
|
import renameServer from '@/api/server/renameServer';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2020-03-28 23:47:32 +00:00
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2022-06-20 16:38:23 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2020-07-04 22:58:14 +00:00
|
|
|
import tw from 'twin.macro';
|
2019-12-10 06:03:10 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2020-03-28 23:47:32 +00:00
|
|
|
const RenameServerBox = () => {
|
|
|
|
const { isSubmitting } = useFormikContext<Values>();
|
2019-12-10 06:03:10 +00:00
|
|
|
|
2020-03-28 23:47:32 +00:00
|
|
|
return (
|
2020-07-04 22:58:14 +00:00
|
|
|
<TitledGreyBox title={'Change Server Name'} css={tw`relative`}>
|
2020-07-04 19:39:55 +00:00
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
2020-07-04 22:58:14 +00:00
|
|
|
<Form css={tw`mb-0`}>
|
2020-03-28 23:47:32 +00:00
|
|
|
<Field
|
|
|
|
id={'name'}
|
|
|
|
name={'name'}
|
|
|
|
label={'Server Name'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
2020-07-04 22:58:14 +00:00
|
|
|
<div css={tw`mt-6 text-right`}>
|
|
|
|
<Button type={'submit'}>
|
2020-03-28 23:47:32 +00:00
|
|
|
Save
|
2020-07-04 22:58:14 +00:00
|
|
|
</Button>
|
2020-03-28 23:47:32 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</TitledGreyBox>
|
|
|
|
);
|
|
|
|
};
|
2019-12-10 06:03:10 +00:00
|
|
|
|
2020-03-28 23:47:32 +00:00
|
|
|
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);
|
2019-12-10 06:03:10 +00:00
|
|
|
|
2020-03-28 23:47:32 +00:00
|
|
|
const submit = ({ name }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('settings');
|
|
|
|
renameServer(server.uuid, name)
|
|
|
|
.then(() => setServer({ ...server, name }))
|
2019-12-10 06:03:10 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-03-28 23:47:32 +00:00
|
|
|
addError({ key: 'settings', message: httpErrorToHuman(error) });
|
2019-12-10 06:03:10 +00:00
|
|
|
})
|
|
|
|
.then(() => setSubmitting(false));
|
2020-03-28 23:47:32 +00:00
|
|
|
};
|
2019-12-10 06:03:10 +00:00
|
|
|
|
2020-03-28 23:47:32 +00:00
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
name: server.name,
|
|
|
|
}}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
name: string().required().min(1),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<RenameServerBox/>
|
|
|
|
</Formik>
|
|
|
|
);
|
2019-12-10 06:03:10 +00:00
|
|
|
};
|