2021-01-08 22:12:43 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import tw from 'twin.macro';
|
2021-01-09 17:26:30 +00:00
|
|
|
import { useHistory, useRouteMatch } from 'react-router-dom';
|
2021-01-08 22:12:43 +00:00
|
|
|
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
|
|
|
|
import { Database } from '@/api/admin/databases/getDatabases';
|
|
|
|
import getDatabase from '@/api/admin/databases/getDatabase';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { ApplicationStore } from '@/state';
|
2021-01-09 16:58:55 +00:00
|
|
|
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';
|
2021-01-09 17:26:30 +00:00
|
|
|
import DatabaseDeleteButton from '@/components/admin/databases/DatabaseDeleteButton';
|
2021-01-08 22:12:43 +00:00
|
|
|
|
|
|
|
interface ctx {
|
|
|
|
database: Database | undefined;
|
|
|
|
setDatabase: Action<ctx, Database | undefined>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Context = createContextStore<ctx>({
|
|
|
|
database: undefined,
|
|
|
|
|
|
|
|
setDatabase: action((state, payload) => {
|
|
|
|
state.database = payload;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2021-03-06 23:57:13 +00:00
|
|
|
export interface Values {
|
2021-01-09 16:58:55 +00:00
|
|
|
name: string;
|
|
|
|
host: string;
|
|
|
|
port: number;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:57:13 +00:00
|
|
|
export interface Params {
|
|
|
|
title: string;
|
|
|
|
initialValues?: Values;
|
|
|
|
children?: React.ReactNode;
|
2021-01-09 17:26:30 +00:00
|
|
|
|
2021-03-06 23:57:13 +00:00
|
|
|
onSubmit: (values: Values, helpers: FormikHelpers<Values>) => void;
|
|
|
|
}
|
2021-01-09 17:26:30 +00:00
|
|
|
|
2021-03-06 23:57:13 +00:00
|
|
|
export const InformationContainer = ({ title, initialValues, children, onSubmit }: Params) => {
|
|
|
|
const submit = (values: Values, helpers: FormikHelpers<Values>) => {
|
|
|
|
onSubmit(values, helpers);
|
|
|
|
};
|
2021-01-09 16:58:55 +00:00
|
|
|
|
2021-03-06 23:57:13 +00:00
|
|
|
if (!initialValues) {
|
|
|
|
initialValues = {
|
|
|
|
name: '',
|
|
|
|
host: '',
|
|
|
|
port: 3306,
|
|
|
|
username: '',
|
|
|
|
password: '',
|
|
|
|
};
|
2021-01-09 16:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
2021-03-06 23:57:13 +00:00
|
|
|
initialValues={initialValues}
|
2021-01-09 16:58:55 +00:00
|
|
|
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>
|
2021-03-06 23:57:13 +00:00
|
|
|
<AdminBox title={title} css={tw`relative`}>
|
2021-01-09 16:58:55 +00:00
|
|
|
<SpinnerOverlay visible={isSubmitting}/>
|
|
|
|
|
|
|
|
<Form css={tw`mb-0`}>
|
|
|
|
<div>
|
|
|
|
<Field
|
|
|
|
id={'name'}
|
|
|
|
name={'name'}
|
|
|
|
label={'Name'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:40:30 +00:00
|
|
|
<div css={tw`md:w-full md:flex md:flex-row mt-6`}>
|
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:mr-4 mt-6 md:mt-0`}>
|
2021-01-09 16:58:55 +00:00
|
|
|
<Field
|
|
|
|
id={'host'}
|
|
|
|
name={'host'}
|
|
|
|
label={'Host'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:40:30 +00:00
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:ml-4 mt-6 md:mt-0`}>
|
2021-01-09 16:58:55 +00:00
|
|
|
<Field
|
|
|
|
id={'port'}
|
|
|
|
name={'port'}
|
|
|
|
label={'Port'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:40:30 +00:00
|
|
|
<div css={tw`md:w-full md:flex md:flex-row mt-6`}>
|
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:mr-4 mt-6 md:mt-0`}>
|
2021-01-09 16:58:55 +00:00
|
|
|
<Field
|
|
|
|
id={'username'}
|
|
|
|
name={'username'}
|
|
|
|
label={'Username'}
|
|
|
|
type={'text'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:40:30 +00:00
|
|
|
<div css={tw`md:w-full md:flex md:flex-col md:ml-4 mt-6 md:mt-0`}>
|
2021-01-09 16:58:55 +00:00
|
|
|
<Field
|
|
|
|
id={'password'}
|
|
|
|
name={'password'}
|
|
|
|
label={'Password'}
|
|
|
|
type={'password'}
|
2021-02-11 17:21:49 +00:00
|
|
|
placeholder={'••••••••'}
|
2021-01-09 16:58:55 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2021-01-09 17:26:30 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
2021-03-06 23:57:13 +00:00
|
|
|
{children}
|
2021-01-09 17:26:30 +00:00
|
|
|
<div css={tw`flex ml-auto`}>
|
|
|
|
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</div>
|
2021-01-09 16:58:55 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</AdminBox>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-03-06 23:57:13 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-01-08 22:12:43 +00:00
|
|
|
const DatabaseEditContainer = () => {
|
|
|
|
const match = useRouteMatch<{ id?: string }>();
|
|
|
|
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
|
|
|
|
|
|
|
const database = Context.useStoreState(state => state.database);
|
|
|
|
const setDatabase = Context.useStoreActions(actions => actions.setDatabase);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
clearFlashes('database');
|
|
|
|
|
|
|
|
getDatabase(Number(match.params?.id))
|
|
|
|
.then(database => setDatabase(database))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'database', error });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (loading || database === undefined) {
|
|
|
|
return (
|
|
|
|
<AdminContentBlock>
|
|
|
|
<FlashMessageRender byKey={'database'} css={tw`mb-4`}/>
|
|
|
|
|
|
|
|
<div css={tw`w-full flex flex-col items-center justify-center`} style={{ height: '24rem' }}>
|
|
|
|
<Spinner size={'base'}/>
|
|
|
|
</div>
|
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AdminContentBlock title={'Database - ' + database.name}>
|
|
|
|
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
2021-01-10 18:34:14 +00:00
|
|
|
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
2021-01-08 22:12:43 +00:00
|
|
|
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>{database.name}</h2>
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>{database.getAddress()}</p>
|
2021-01-08 22:12:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<FlashMessageRender byKey={'database'} css={tw`mb-4`}/>
|
2021-01-09 16:58:55 +00:00
|
|
|
|
|
|
|
<EditInformationContainer/>
|
2021-01-08 22:12:43 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|
2021-01-06 22:50:21 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
return (
|
2021-01-08 22:12:43 +00:00
|
|
|
<Context.Provider>
|
|
|
|
<DatabaseEditContainer/>
|
|
|
|
</Context.Provider>
|
2021-01-06 22:50:21 +00:00
|
|
|
);
|
|
|
|
};
|