admin(ui): implement DatabaseEditContainer.tsx
This commit is contained in:
parent
0759ecb1e1
commit
d323662ad5
4 changed files with 166 additions and 8 deletions
|
@ -8,6 +8,13 @@ import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
|||
import Spinner from '@/components/elements/Spinner';
|
||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||
import { ApplicationStore } from '@/state';
|
||||
import { number, object, string } from 'yup';
|
||||
import AdminBox from '@/components/admin/AdminBox';
|
||||
import Button from '@/components/elements/Button';
|
||||
import Field from '@/components/elements/Field';
|
||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||
import { Form, Formik, FormikHelpers } from 'formik';
|
||||
import updateDatabase from '@/api/admin/databases/updateDatabase';
|
||||
|
||||
interface ctx {
|
||||
database: Database | undefined;
|
||||
|
@ -22,6 +29,125 @@ export const Context = createContextStore<ctx>({
|
|||
}),
|
||||
});
|
||||
|
||||
interface Values {
|
||||
name: string;
|
||||
host: string;
|
||||
port: number;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
const EditInformationContainer = () => {
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
||||
const database = Context.useStoreState(state => state.database);
|
||||
const setDatabase = Context.useStoreActions(actions => actions.setDatabase);
|
||||
|
||||
if (database === undefined) {
|
||||
return (
|
||||
<></>
|
||||
);
|
||||
}
|
||||
|
||||
const submit = ({ name, host, port, username, password }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
||||
clearFlashes('database');
|
||||
|
||||
updateDatabase(database.id, name, host, port, username, password || undefined)
|
||||
.then(() => setDatabase({ ...database, name, host, port, username }))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'database', error });
|
||||
})
|
||||
.then(() => setSubmitting(false));
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
onSubmit={submit}
|
||||
initialValues={{
|
||||
name: database.name,
|
||||
host: database.host,
|
||||
port: database.port,
|
||||
username: database.username,
|
||||
password: '',
|
||||
}}
|
||||
validationSchema={object().shape({
|
||||
name: string().required().max(191),
|
||||
host: string().max(255),
|
||||
port: number().min(2).max(65534),
|
||||
username: string().min(1).max(32),
|
||||
password: string(),
|
||||
})}
|
||||
>
|
||||
{
|
||||
({ isSubmitting, isValid }) => (
|
||||
<React.Fragment>
|
||||
<AdminBox title={'Edit Database'} 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`w-full flex flex-row mt-6`}>
|
||||
<div css={tw`w-full flex flex-col mr-4`}>
|
||||
<Field
|
||||
id={'host'}
|
||||
name={'host'}
|
||||
label={'Host'}
|
||||
type={'text'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div css={tw`w-full flex flex-col ml-4`}>
|
||||
<Field
|
||||
id={'port'}
|
||||
name={'port'}
|
||||
label={'Port'}
|
||||
type={'text'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div css={tw`w-full flex flex-row mt-6`}>
|
||||
<div css={tw`w-full flex flex-col mr-4`}>
|
||||
<Field
|
||||
id={'username'}
|
||||
name={'username'}
|
||||
label={'Username'}
|
||||
type={'text'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div css={tw`w-full flex flex-col ml-4`}>
|
||||
<Field
|
||||
id={'password'}
|
||||
name={'password'}
|
||||
label={'Password'}
|
||||
type={'password'}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div css={tw`mt-6 text-right`}>
|
||||
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
||||
Save
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
</AdminBox>
|
||||
</React.Fragment>
|
||||
)
|
||||
}
|
||||
</Formik>
|
||||
);
|
||||
};
|
||||
|
||||
const DatabaseEditContainer = () => {
|
||||
const match = useRouteMatch<{ id?: string }>();
|
||||
|
||||
|
@ -65,6 +191,8 @@ const DatabaseEditContainer = () => {
|
|||
</div>
|
||||
|
||||
<FlashMessageRender byKey={'database'} css={tw`mb-4`}/>
|
||||
|
||||
<EditInformationContainer/>
|
||||
</AdminContentBlock>
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue