ui(admin): implement NewDatabaseContainer.tsx

This commit is contained in:
Matthew Penner 2021-03-06 16:57:13 -07:00
parent e9546c70bd
commit 08546e6076
3 changed files with 99 additions and 39 deletions

View File

@ -30,7 +30,7 @@ export const Context = createContextStore<ctx>({
}), }),
}); });
interface Values { export interface Values {
name: string; name: string;
host: string; host: string;
port: number; port: number;
@ -38,42 +38,33 @@ interface Values {
password: string; password: string;
} }
const EditInformationContainer = () => { export interface Params {
const history = useHistory(); title: string;
initialValues?: Values;
children?: React.ReactNode;
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); onSubmit: (values: Values, helpers: FormikHelpers<Values>) => void;
}
const database = Context.useStoreState(state => state.database); export const InformationContainer = ({ title, initialValues, children, onSubmit }: Params) => {
const setDatabase = Context.useStoreActions(actions => actions.setDatabase); const submit = (values: Values, helpers: FormikHelpers<Values>) => {
onSubmit(values, helpers);
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));
}; };
if (!initialValues) {
initialValues = {
name: '',
host: '',
port: 3306,
username: '',
password: '',
};
}
return ( return (
<Formik <Formik
onSubmit={submit} onSubmit={submit}
initialValues={{ initialValues={initialValues}
name: database.name,
host: database.host,
port: database.port,
username: database.username,
password: '',
}}
validationSchema={object().shape({ validationSchema={object().shape({
name: string().required().max(191), name: string().required().max(191),
host: string().max(255), host: string().max(255),
@ -85,7 +76,7 @@ const EditInformationContainer = () => {
{ {
({ isSubmitting, isValid }) => ( ({ isSubmitting, isValid }) => (
<React.Fragment> <React.Fragment>
<AdminBox title={'Edit Database'} css={tw`relative`}> <AdminBox title={title} css={tw`relative`}>
<SpinnerOverlay visible={isSubmitting}/> <SpinnerOverlay visible={isSubmitting}/>
<Form css={tw`mb-0`}> <Form css={tw`mb-0`}>
@ -140,13 +131,7 @@ const EditInformationContainer = () => {
</div> </div>
<div css={tw`w-full flex flex-row items-center mt-6`}> <div css={tw`w-full flex flex-row items-center mt-6`}>
<div css={tw`flex`}> {children}
<DatabaseDeleteButton
databaseId={database.id}
onDeleted={() => history.push('/admin/databases')}
/>
</div>
<div css={tw`flex ml-auto`}> <div css={tw`flex ml-auto`}>
<Button type={'submit'} disabled={isSubmitting || !isValid}> <Button type={'submit'} disabled={isSubmitting || !isValid}>
Save Save
@ -162,6 +147,54 @@ const EditInformationContainer = () => {
); );
}; };
const EditInformationContainer = () => {
const history = useHistory();
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 (
<InformationContainer
title={'Edit Database'}
initialValues={{
name: database.name,
host: database.host,
port: database.port,
username: database.username,
password: '',
}}
onSubmit={submit}
>
<div css={tw`flex`}>
<DatabaseDeleteButton
databaseId={database.id}
onDeleted={() => history.push('/admin/databases')}
/>
</div>
</InformationContainer>
);
};
const DatabaseEditContainer = () => { const DatabaseEditContainer = () => {
const match = useRouteMatch<{ id?: string }>(); const match = useRouteMatch<{ id?: string }>();

View File

@ -1,8 +1,31 @@
import React from 'react'; import React from 'react';
import { useHistory } from 'react-router-dom';
import tw from 'twin.macro'; import tw from 'twin.macro';
import AdminContentBlock from '@/components/admin/AdminContentBlock'; import AdminContentBlock from '@/components/admin/AdminContentBlock';
import createDatabase from '@/api/admin/databases/createDatabase';
import { ApplicationStore } from '@/state';
import { Actions, useStoreActions } from 'easy-peasy';
import { FormikHelpers } from 'formik';
import FlashMessageRender from '@/components/FlashMessageRender';
import { InformationContainer, Values } from '@/components/admin/databases/DatabaseEditContainer';
export default () => { export default () => {
const history = useHistory();
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const submit = ({ name, host, port, username, password }: Values, { setSubmitting }: FormikHelpers<Values>) => {
clearFlashes('database:create');
createDatabase(name, host, port, username, password)
.then(database => history.push(`/admin/databases/${database.id}`))
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'database:create', error });
})
.then(() => setSubmitting(false));
};
return ( return (
<AdminContentBlock title={'New Database'}> <AdminContentBlock title={'New Database'}>
<div css={tw`w-full flex flex-row items-center mb-8`}> <div css={tw`w-full flex flex-row items-center mb-8`}>
@ -11,6 +34,10 @@ export default () => {
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>Add a new database host to the panel.</p> <p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>Add a new database host to the panel.</p>
</div> </div>
</div> </div>
<FlashMessageRender byKey={'database:create'} css={tw`mb-4`}/>
<InformationContainer title={'Create Database'} onSubmit={submit}/>
</AdminContentBlock> </AdminContentBlock>
); );
}; };

View File

@ -35,7 +35,7 @@ export default () => {
<AdminBox title={'Configuration'} icon={faCode} css={tw`mb-4`}> <AdminBox title={'Configuration'} icon={faCode} css={tw`mb-4`}>
<div css={tw`relative`}> <div css={tw`relative`}>
<div css={tw`absolute top-0 right-0`}> <div css={tw`absolute top-0 right-0`}>
<CopyOnClick text={configuration} hideTextInToast={true}> <CopyOnClick text={configuration} hideTextInToast>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" css={tw`h-5 w-5 text-neutral-500 hover:text-neutral-400 cursor-pointer mt-1 mr-1`}> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" css={tw`h-5 w-5 text-neutral-500 hover:text-neutral-400 cursor-pointer mt-1 mr-1`}>
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3" /> <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3" />
</svg> </svg>