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,66 +1,69 @@
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 = () => {
<TitledGreyBox title={'Change Server Name'} className={'relative'}> const { isSubmitting } = useFormikContext<Values>();
<SpinnerOverlay size={'normal'} visible={isSubmitting}/>
<Form className={'mb-0'}>
<Field
id={'name'}
name={'name'}
label={'Server Name'}
type={'text'}
/>
<div className={'mt-6 text-right'}>
<button type={'submit'} className={'btn btn-sm btn-primary'}>
Save
</button>
</div>
</Form>
</TitledGreyBox>
);
const EnhancedForm = withFormik<OwnProps, Values>({ return (
displayName: 'RenameServerBoxForm', <TitledGreyBox title={'Change Server Name'} className={'relative'}>
<SpinnerOverlay size={'normal'} visible={isSubmitting}/>
mapPropsToValues: props => ({ <Form className={'mb-0'}>
name: props.server.name, <Field
}), id={'name'}
name={'name'}
validationSchema: () => object().shape({ label={'Server Name'}
name: string().required().min(1), type={'text'}
}), />
<div className={'mt-6 text-right'}>
handleSubmit: (values, { props, setSubmitting }) => { <button type={'submit'} className={'btn btn-sm btn-primary'}>
renameServer(props.server.uuid, values.name) Save
.then(() => props.setServer({ ...props.server, name: values.name })) </button>
.catch(error => { </div>
console.error(error); </Form>
}) </TitledGreyBox>
.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,52 +5,56 @@ 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'}>
<TitledGreyBox title={'SFTP Details'} className={'w-full md:flex-1 md:mr-6'}> <FlashMessageRender byKey={'settings'} className={'mb-4'}/>
<div> <div className={'md:flex'}>
<label className={'input-dark-label'}>Server Address</label> <TitledGreyBox title={'SFTP Details'} className={'w-full md:flex-1 md:mr-6'}>
<input <div>
type={'text'} <label className={'input-dark-label'}>Server Address</label>
className={'input-dark'} <input
value={`sftp://${server.sftpDetails.ip}:${server.sftpDetails.port}`} type={'text'}
readOnly={true} className={'input-dark'}
/> value={`sftp://${server.sftpDetails.ip}:${server.sftpDetails.port}`}
</div> readOnly={true}
<div className={'mt-6'}> />
<label className={'input-dark-label'}>Username</label> </div>
<input <div className={'mt-6'}>
type={'text'} <label className={'input-dark-label'}>Username</label>
className={'input-dark'} <input
value={`${user.username}.${server.id}`} type={'text'}
readOnly={true} className={'input-dark'}
/> value={`${user.username}.${server.id}`}
</div> readOnly={true}
<div className={'mt-6 flex items-center'}> />
<div className={'flex-1'}> </div>
<div className={'border-l-4 border-cyan-500 p-3'}> <div className={'mt-6 flex items-center'}>
<p className={'text-xs text-neutral-200'}> <div className={'flex-1'}>
Your SFTP password is the same as the password you use to access this panel. <div className={'border-l-4 border-cyan-500 p-3'}>
</p> <p className={'text-xs text-neutral-200'}>
Your SFTP password is the same as the password you use to access this panel.
</p>
</div>
</div>
<div className={'ml-4'}>
<a
href={`sftp://${user.username}.${server.id}@${server.sftpDetails.ip}:${server.sftpDetails.port}`}
className={'btn btn-sm btn-secondary'}
>
Launch SFTP
</a>
</div> </div>
</div> </div>
<div className={'ml-4'}> </TitledGreyBox>
<a <div className={'w-full mt-6 md:flex-1 md:mt-0'}>
href={`sftp://${user.username}.${server.id}@${server.sftpDetails.ip}:${server.sftpDetails.port}`} <RenameServerBox/>
className={'btn btn-sm btn-secondary'}
>
Launch SFTP
</a>
</div>
</div> </div>
</TitledGreyBox>
<div className={'w-full mt-6 md:flex-1 md:mt-0'}>
<RenameServerBox/>
</div> </div>
</div> </div>
); );