Show error message when attempting to change a server's name

This commit is contained in:
Dane Everitt 2020-03-28 16:47:32 -07:00
parent 1f92a7de33
commit 7e0ac2c311
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 91 additions and 84 deletions

View file

@ -1,24 +1,23 @@
import React from 'react'; import React from 'react';
import { ServerContext } from '@/state/server'; import { ServerContext } from '@/state/server';
import TitledGreyBox from '@/components/elements/TitledGreyBox'; import TitledGreyBox from '@/components/elements/TitledGreyBox';
import { Form, FormikProps, withFormik } from 'formik'; import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Server } from '@/api/server/getServer'; import { Actions, useStoreActions } from 'easy-peasy';
import { ActionCreator } from 'easy-peasy';
import renameServer from '@/api/server/renameServer'; import renameServer from '@/api/server/renameServer';
import Field from '@/components/elements/Field'; import Field from '@/components/elements/Field';
import { object, string } from 'yup'; import { object, string } from 'yup';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { ApplicationStore } from '@/state';
interface OwnProps { import { httpErrorToHuman } from '@/api/http';
server: Server;
setServer: ActionCreator<Server>;
}
interface Values { interface Values {
name: string; name: string;
} }
const RenameServerBox = ({ isSubmitting, ...props }: OwnProps & FormikProps<Values>) => ( const RenameServerBox = () => {
const { isSubmitting } = useFormikContext<Values>();
return (
<TitledGreyBox title={'Change Server Name'} className={'relative'}> <TitledGreyBox title={'Change Server Name'} className={'relative'}>
<SpinnerOverlay size={'normal'} visible={isSubmitting}/> <SpinnerOverlay size={'normal'} visible={isSubmitting}/>
<Form className={'mb-0'}> <Form className={'mb-0'}>
@ -36,31 +35,35 @@ const RenameServerBox = ({ isSubmitting, ...props }: OwnProps & FormikProps<Valu
</Form> </Form>
</TitledGreyBox> </TitledGreyBox>
); );
};
const EnhancedForm = withFormik<OwnProps, Values>({
displayName: 'RenameServerBoxForm',
mapPropsToValues: props => ({
name: props.server.name,
}),
validationSchema: () => object().shape({
name: string().required().min(1),
}),
handleSubmit: (values, { props, setSubmitting }) => {
renameServer(props.server.uuid, values.name)
.then(() => props.setServer({ ...props.server, name: values.name }))
.catch(error => {
console.error(error);
})
.then(() => setSubmitting(false));
},
})(RenameServerBox);
export default () => { export default () => {
const server = ServerContext.useStoreState(state => state.server.data!); const server = ServerContext.useStoreState(state => state.server.data!);
const setServer = ServerContext.useStoreActions(actions => actions.server.setServer); const setServer = ServerContext.useStoreActions(actions => actions.server.setServer);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
return <EnhancedForm server={server} setServer={setServer}/>; const submit = ({ name }: Values, { setSubmitting }: FormikHelpers<Values>) => {
clearFlashes('settings');
renameServer(server.uuid, name)
.then(() => setServer({ ...server, name }))
.catch(error => {
console.error(error);
addError({ key: 'settings', message: httpErrorToHuman(error) });
})
.then(() => setSubmitting(false));
};
return (
<Formik
onSubmit={submit}
initialValues={{
name: server.name,
}}
validationSchema={object().shape({
name: string().required().min(1),
})}
>
<RenameServerBox/>
</Formik>
);
}; };

View file

@ -5,13 +5,16 @@ import { useStoreState } from 'easy-peasy';
import { ApplicationStore } from '@/state'; import { ApplicationStore } from '@/state';
import { UserData } from '@/state/user'; import { UserData } from '@/state/user';
import RenameServerBox from '@/components/server/settings/RenameServerBox'; import RenameServerBox from '@/components/server/settings/RenameServerBox';
import FlashMessageRender from '@/components/FlashMessageRender';
export default () => { export default () => {
const user = useStoreState<ApplicationStore, UserData>(state => state.user.data!); const user = useStoreState<ApplicationStore, UserData>(state => state.user.data!);
const server = ServerContext.useStoreState(state => state.server.data!); const server = ServerContext.useStoreState(state => state.server.data!);
return ( return (
<div className={'my-10 mb-6 md:flex'}> <div className={'my-10 mb-6'}>
<FlashMessageRender byKey={'settings'} className={'mb-4'}/>
<div className={'md:flex'}>
<TitledGreyBox title={'SFTP Details'} className={'w-full md:flex-1 md:mr-6'}> <TitledGreyBox title={'SFTP Details'} className={'w-full md:flex-1 md:mr-6'}>
<div> <div>
<label className={'input-dark-label'}>Server Address</label> <label className={'input-dark-label'}>Server Address</label>
@ -53,5 +56,6 @@ export default () => {
<RenameServerBox/> <RenameServerBox/>
</div> </div>
</div> </div>
</div>
); );
}; };