Make database rows use context better
This commit is contained in:
parent
0ebf842757
commit
708c15eba8
|
@ -1,15 +1,14 @@
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { ServerDatabase } from '@/api/server/getServerDatabases';
|
|
||||||
import Modal from '@/components/elements/Modal';
|
import Modal from '@/components/elements/Modal';
|
||||||
import { Form, Formik, FormikHelpers } from 'formik';
|
import { Form, Formik, FormikHelpers } from 'formik';
|
||||||
import Field from '@/components/elements/Field';
|
import Field from '@/components/elements/Field';
|
||||||
import { object, string } from 'yup';
|
import { object, string } from 'yup';
|
||||||
import createServerDatabase from '@/api/server/createServerDatabase';
|
import createServerDatabase from '@/api/server/createServerDatabase';
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import { Actions, useStoreActions } from 'easy-peasy';
|
|
||||||
import { ApplicationStore } from '@/state';
|
|
||||||
import { httpErrorToHuman } from '@/api/http';
|
import { httpErrorToHuman } from '@/api/http';
|
||||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||||
|
import useFlash from '@/plugins/useFlash';
|
||||||
|
import useServer from '@/plugins/useServer';
|
||||||
|
|
||||||
interface Values {
|
interface Values {
|
||||||
databaseName: string;
|
databaseName: string;
|
||||||
|
@ -27,28 +26,25 @@ const schema = object().shape({
|
||||||
.matches(/^([1-9]{1,3}|%)(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?$/, 'A valid connection address must be provided.'),
|
.matches(/^([1-9]{1,3}|%)(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?$/, 'A valid connection address must be provided.'),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default ({ onCreated }: { onCreated: (database: ServerDatabase) => void }) => {
|
export default () => {
|
||||||
|
const { uuid } = useServer();
|
||||||
|
const { addError, clearFlashes } = useFlash();
|
||||||
const [ visible, setVisible ] = useState(false);
|
const [ visible, setVisible ] = useState(false);
|
||||||
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
||||||
const server = ServerContext.useStoreState(state => state.server.data!);
|
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
|
||||||
|
|
||||||
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
||||||
clearFlashes();
|
clearFlashes('database:create');
|
||||||
createServerDatabase(server.uuid, { ...values })
|
createServerDatabase(uuid, { ...values })
|
||||||
.then(database => {
|
.then(database => {
|
||||||
onCreated(database);
|
appendDatabase(database);
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
addFlash({
|
addError({ key: 'database:create', message: httpErrorToHuman(error) });
|
||||||
key: 'create-database-modal',
|
setSubmitting(false);
|
||||||
type: 'error',
|
});
|
||||||
title: 'Error',
|
|
||||||
message: httpErrorToHuman(error),
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then(() => setSubmitting(false));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -69,7 +65,7 @@ export default ({ onCreated }: { onCreated: (database: ServerDatabase) => void }
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<FlashMessageRender byKey={'create-database-modal'} className={'mb-6'}/>
|
<FlashMessageRender byKey={'database:create'} className={'mb-6'}/>
|
||||||
<h3 className={'mb-6'}>Create new database</h3>
|
<h3 className={'mb-6'}>Create new database</h3>
|
||||||
<Form className={'m-0'}>
|
<Form className={'m-0'}>
|
||||||
<Field
|
<Field
|
||||||
|
@ -105,7 +101,7 @@ export default ({ onCreated }: { onCreated: (database: ServerDatabase) => void }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
</Formik>
|
</Formik>
|
||||||
<button className={'btn btn-primary btn-lg'} onClick={() => setVisible(true)}>
|
<button className={'btn btn-primary btn-sm'} onClick={() => setVisible(true)}>
|
||||||
New Database
|
New Database
|
||||||
</button>
|
</button>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
|
|
|
@ -9,31 +9,28 @@ import { Form, Formik, FormikHelpers } from 'formik';
|
||||||
import Field from '@/components/elements/Field';
|
import Field from '@/components/elements/Field';
|
||||||
import { object, string } from 'yup';
|
import { object, string } from 'yup';
|
||||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||||
import { Actions, useStoreActions } from 'easy-peasy';
|
|
||||||
import { ApplicationStore } from '@/state';
|
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import deleteServerDatabase from '@/api/server/deleteServerDatabase';
|
import deleteServerDatabase from '@/api/server/deleteServerDatabase';
|
||||||
import { httpErrorToHuman } from '@/api/http';
|
import { httpErrorToHuman } from '@/api/http';
|
||||||
import RotatePasswordButton from '@/components/server/databases/RotatePasswordButton';
|
import RotatePasswordButton from '@/components/server/databases/RotatePasswordButton';
|
||||||
import Can from '@/components/elements/Can';
|
import Can from '@/components/elements/Can';
|
||||||
|
import { ServerDatabase } from '@/api/server/getServerDatabases';
|
||||||
|
import useServer from '@/plugins/useServer';
|
||||||
|
import useFlash from '@/plugins/useFlash';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
databaseId: string | number;
|
database: ServerDatabase;
|
||||||
className?: string;
|
className?: string;
|
||||||
onDelete: () => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ({ databaseId, className, onDelete }: Props) => {
|
export default ({ database, className }: Props) => {
|
||||||
|
const { uuid } = useServer();
|
||||||
|
const { addError, clearFlashes } = useFlash();
|
||||||
const [ visible, setVisible ] = useState(false);
|
const [ visible, setVisible ] = useState(false);
|
||||||
const database = ServerContext.useStoreState(state => state.databases.items.find(item => item.id === databaseId));
|
|
||||||
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
|
|
||||||
const [ connectionVisible, setConnectionVisible ] = useState(false);
|
const [ connectionVisible, setConnectionVisible ] = useState(false);
|
||||||
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
||||||
const server = ServerContext.useStoreState(state => state.server.data!);
|
|
||||||
|
|
||||||
if (!database) {
|
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
|
||||||
return null;
|
const removeDatabase = ServerContext.useStoreActions(actions => actions.databases.removeDatabase);
|
||||||
}
|
|
||||||
|
|
||||||
const schema = object().shape({
|
const schema = object().shape({
|
||||||
confirm: string()
|
confirm: string()
|
||||||
|
@ -43,20 +40,15 @@ export default ({ databaseId, className, onDelete }: Props) => {
|
||||||
|
|
||||||
const submit = (values: { confirm: string }, { setSubmitting }: FormikHelpers<{ confirm: string }>) => {
|
const submit = (values: { confirm: string }, { setSubmitting }: FormikHelpers<{ confirm: string }>) => {
|
||||||
clearFlashes();
|
clearFlashes();
|
||||||
deleteServerDatabase(server.uuid, database.id)
|
deleteServerDatabase(uuid, database.id)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
setTimeout(() => onDelete(), 150);
|
setTimeout(() => removeDatabase(database.id), 150);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
setSubmitting(false);
|
setSubmitting(false);
|
||||||
addFlash({
|
addError({ key: 'database:delete', message: httpErrorToHuman(error) });
|
||||||
key: 'delete-database-modal',
|
|
||||||
type: 'error',
|
|
||||||
title: 'Error',
|
|
||||||
message: httpErrorToHuman(error),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -78,7 +70,7 @@ export default ({ databaseId, className, onDelete }: Props) => {
|
||||||
resetForm();
|
resetForm();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<FlashMessageRender byKey={'delete-database-modal'} className={'mb-6'}/>
|
<FlashMessageRender byKey={'database:delete'} className={'mb-6'}/>
|
||||||
<h3 className={'mb-6'}>Confirm database deletion</h3>
|
<h3 className={'mb-6'}>Confirm database deletion</h3>
|
||||||
<p className={'text-sm'}>
|
<p className={'text-sm'}>
|
||||||
Deleting a database is a permanent action, it cannot be undone. This will permanetly
|
Deleting a database is a permanent action, it cannot be undone. This will permanetly
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import getServerDatabases from '@/api/server/getServerDatabases';
|
import getServerDatabases from '@/api/server/getServerDatabases';
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import { Actions, useStoreActions } from 'easy-peasy';
|
|
||||||
import { ApplicationStore } from '@/state';
|
|
||||||
import { httpErrorToHuman } from '@/api/http';
|
import { httpErrorToHuman } from '@/api/http';
|
||||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||||
import DatabaseRow from '@/components/server/databases/DatabaseRow';
|
import DatabaseRow from '@/components/server/databases/DatabaseRow';
|
||||||
|
@ -10,51 +8,51 @@ import Spinner from '@/components/elements/Spinner';
|
||||||
import { CSSTransition } from 'react-transition-group';
|
import { CSSTransition } from 'react-transition-group';
|
||||||
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
|
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
|
||||||
import Can from '@/components/elements/Can';
|
import Can from '@/components/elements/Can';
|
||||||
|
import useFlash from '@/plugins/useFlash';
|
||||||
|
import useServer from '@/plugins/useServer';
|
||||||
|
import ListRefreshIndicator from '@/components/elements/ListRefreshIndicator';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
|
const { uuid, featureLimits } = useServer();
|
||||||
|
const { addError, clearFlashes } = useFlash();
|
||||||
const [ loading, setLoading ] = useState(true);
|
const [ loading, setLoading ] = useState(true);
|
||||||
const server = ServerContext.useStoreState(state => state.server.data!);
|
|
||||||
const databases = ServerContext.useStoreState(state => state.databases.items);
|
const databases = ServerContext.useStoreState(state => state.databases.data);
|
||||||
const { setDatabases, appendDatabase, removeDatabase } = ServerContext.useStoreActions(state => state.databases);
|
const setDatabases = ServerContext.useStoreActions(state => state.databases.setDatabases);
|
||||||
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLoading(!databases.length);
|
setLoading(!databases.length);
|
||||||
clearFlashes('databases');
|
clearFlashes('databases');
|
||||||
|
|
||||||
getServerDatabases(server.uuid)
|
getServerDatabases(uuid)
|
||||||
.then(databases => {
|
.then(databases => setDatabases(databases))
|
||||||
setDatabases(databases);
|
.catch(error => {
|
||||||
setLoading(false);
|
console.error(error);
|
||||||
|
addError({ key: 'databases', message: httpErrorToHuman(error) });
|
||||||
})
|
})
|
||||||
.catch(error => addFlash({
|
.then(() => setLoading(false));
|
||||||
key: 'databases',
|
|
||||||
title: 'Error',
|
|
||||||
message: httpErrorToHuman(error),
|
|
||||||
type: 'error',
|
|
||||||
}));
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={'my-10 mb-6'}>
|
<div className={'my-10 mb-6'}>
|
||||||
<FlashMessageRender byKey={'databases'}/>
|
<FlashMessageRender byKey={'databases'} className={'mb-4'}/>
|
||||||
{loading ?
|
{(!databases.length && loading) ?
|
||||||
<Spinner size={'large'} centered={true}/>
|
<Spinner size={'large'} centered={true}/>
|
||||||
:
|
:
|
||||||
<CSSTransition classNames={'fade'} timeout={250}>
|
<CSSTransition classNames={'fade'} timeout={250}>
|
||||||
<>
|
<>
|
||||||
|
<ListRefreshIndicator visible={loading}/>
|
||||||
{databases.length > 0 ?
|
{databases.length > 0 ?
|
||||||
databases.map((database, index) => (
|
databases.map((database, index) => (
|
||||||
<DatabaseRow
|
<DatabaseRow
|
||||||
key={database.id}
|
key={database.id}
|
||||||
databaseId={database.id}
|
database={database}
|
||||||
onDelete={() => removeDatabase(database)}
|
|
||||||
className={index > 0 ? 'mt-1' : undefined}
|
className={index > 0 ? 'mt-1' : undefined}
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
:
|
:
|
||||||
<p className={'text-center text-sm text-neutral-400'}>
|
<p className={'text-center text-sm text-neutral-400'}>
|
||||||
{server.featureLimits.databases > 0 ?
|
{featureLimits.databases > 0 ?
|
||||||
`It looks like you have no databases.`
|
`It looks like you have no databases.`
|
||||||
:
|
:
|
||||||
`Databases cannot be created for this server.`
|
`Databases cannot be created for this server.`
|
||||||
|
@ -62,9 +60,9 @@ export default () => {
|
||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
<Can action={'database.create'}>
|
<Can action={'database.create'}>
|
||||||
{server.featureLimits.databases > 0 &&
|
{featureLimits.databases > 0 &&
|
||||||
<div className={'mt-6 flex justify-end'}>
|
<div className={'mt-6 flex justify-end'}>
|
||||||
<CreateDatabaseButton onCreated={appendDatabase}/>
|
<CreateDatabaseButton/>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</Can>
|
</Can>
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import { action, Action } from 'easy-peasy';
|
||||||
|
import { ServerDatabase } from '@/api/server/getServerDatabases';
|
||||||
|
|
||||||
|
export interface ServerDatabaseStore {
|
||||||
|
data: ServerDatabase[];
|
||||||
|
setDatabases: Action<ServerDatabaseStore, ServerDatabase[]>;
|
||||||
|
appendDatabase: Action<ServerDatabaseStore, ServerDatabase>;
|
||||||
|
removeDatabase: Action<ServerDatabaseStore, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const databases: ServerDatabaseStore = {
|
||||||
|
data: [],
|
||||||
|
|
||||||
|
setDatabases: action((state, payload) => {
|
||||||
|
state.data = payload;
|
||||||
|
}),
|
||||||
|
|
||||||
|
appendDatabase: action((state, payload) => {
|
||||||
|
if (state.data.find(database => database.id === payload.id)) {
|
||||||
|
state.data = state.data.map(database => database.id === payload.id ? payload : database);
|
||||||
|
} else {
|
||||||
|
state.data = [ ...state.data, payload ];
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
removeDatabase: action((state, payload) => {
|
||||||
|
state.data = [ ...state.data.filter(database => database.id !== payload) ];
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
export default databases;
|
|
@ -1,12 +1,12 @@
|
||||||
import getServer, { Server } from '@/api/server/getServer';
|
import getServer, { Server } from '@/api/server/getServer';
|
||||||
import { action, Action, createContextStore, thunk, Thunk } from 'easy-peasy';
|
import { action, Action, createContextStore, thunk, Thunk } from 'easy-peasy';
|
||||||
import socket, { SocketStore } from './socket';
|
import socket, { SocketStore } from './socket';
|
||||||
import { ServerDatabase } from '@/api/server/getServerDatabases';
|
|
||||||
import files, { ServerFileStore } from '@/state/server/files';
|
import files, { ServerFileStore } from '@/state/server/files';
|
||||||
import subusers, { ServerSubuserStore } from '@/state/server/subusers';
|
import subusers, { ServerSubuserStore } from '@/state/server/subusers';
|
||||||
import { composeWithDevTools } from 'redux-devtools-extension';
|
import { composeWithDevTools } from 'redux-devtools-extension';
|
||||||
import backups, { ServerBackupStore } from '@/state/server/backups';
|
import backups, { ServerBackupStore } from '@/state/server/backups';
|
||||||
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
|
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
|
||||||
|
import databases, { ServerDatabaseStore } from '@/state/server/databases';
|
||||||
|
|
||||||
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running';
|
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running';
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ const server: ServerDataStore = {
|
||||||
permissions: [],
|
permissions: [],
|
||||||
|
|
||||||
getServer: thunk(async (actions, payload) => {
|
getServer: thunk(async (actions, payload) => {
|
||||||
const [server, permissions] = await getServer(payload);
|
const [ server, permissions ] = await getServer(payload);
|
||||||
|
|
||||||
actions.setServer(server);
|
actions.setServer(server);
|
||||||
actions.setPermissions(permissions);
|
actions.setPermissions(permissions);
|
||||||
|
@ -50,26 +50,6 @@ const status: ServerStatusStore = {
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
interface ServerDatabaseStore {
|
|
||||||
items: ServerDatabase[];
|
|
||||||
setDatabases: Action<ServerDatabaseStore, ServerDatabase[]>;
|
|
||||||
appendDatabase: Action<ServerDatabaseStore, ServerDatabase>;
|
|
||||||
removeDatabase: Action<ServerDatabaseStore, ServerDatabase>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const databases: ServerDatabaseStore = {
|
|
||||||
items: [],
|
|
||||||
setDatabases: action((state, payload) => {
|
|
||||||
state.items = payload;
|
|
||||||
}),
|
|
||||||
appendDatabase: action((state, payload) => {
|
|
||||||
state.items = state.items.filter(item => item.id !== payload.id).concat(payload);
|
|
||||||
}),
|
|
||||||
removeDatabase: action((state, payload) => {
|
|
||||||
state.items = state.items.filter(item => item.id !== payload.id);
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface ServerStore {
|
export interface ServerStore {
|
||||||
server: ServerDataStore;
|
server: ServerDataStore;
|
||||||
subusers: ServerSubuserStore;
|
subusers: ServerSubuserStore;
|
||||||
|
@ -94,7 +74,7 @@ export const ServerContext = createContextStore<ServerStore>({
|
||||||
clearServerState: action(state => {
|
clearServerState: action(state => {
|
||||||
state.server.data = undefined;
|
state.server.data = undefined;
|
||||||
state.server.permissions = [];
|
state.server.permissions = [];
|
||||||
state.databases.items = [];
|
state.databases.data = [];
|
||||||
state.subusers.data = [];
|
state.subusers.data = [];
|
||||||
state.files.directory = '/';
|
state.files.directory = '/';
|
||||||
state.files.contents = [];
|
state.files.contents = [];
|
||||||
|
|
Loading…
Reference in New Issue