2019-07-10 05:00:29 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-03-30 05:20:27 +00:00
|
|
|
import getServerDatabases from '@/api/server/getServerDatabases';
|
2019-07-10 05:00:29 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import DatabaseRow from '@/components/server/databases/DatabaseRow';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import { CSSTransition } from 'react-transition-group';
|
2019-07-10 05:41:09 +00:00
|
|
|
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
|
2020-03-30 05:20:27 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2019-07-10 05:00:29 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
|
|
|
const server = ServerContext.useStoreState(state => state.server.data!);
|
2019-07-27 22:17:50 +00:00
|
|
|
const databases = ServerContext.useStoreState(state => state.databases.items);
|
|
|
|
const { setDatabases, appendDatabase, removeDatabase } = ServerContext.useStoreActions(state => state.databases);
|
2019-07-10 05:00:29 +00:00
|
|
|
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2019-07-27 22:17:50 +00:00
|
|
|
setLoading(!databases.length);
|
2019-07-10 05:00:29 +00:00
|
|
|
clearFlashes('databases');
|
2019-07-27 22:17:50 +00:00
|
|
|
|
2019-07-10 05:00:29 +00:00
|
|
|
getServerDatabases(server.uuid)
|
|
|
|
.then(databases => {
|
|
|
|
setDatabases(databases);
|
|
|
|
setLoading(false);
|
|
|
|
})
|
|
|
|
.catch(error => addFlash({
|
|
|
|
key: 'databases',
|
|
|
|
title: 'Error',
|
|
|
|
message: httpErrorToHuman(error),
|
|
|
|
type: 'error',
|
|
|
|
}));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2019-07-10 05:06:42 +00:00
|
|
|
<div className={'my-10 mb-6'}>
|
2019-07-10 05:00:29 +00:00
|
|
|
<FlashMessageRender byKey={'databases'}/>
|
|
|
|
{loading ?
|
2019-08-17 23:03:10 +00:00
|
|
|
<Spinner size={'large'} centered={true}/>
|
2019-07-10 05:00:29 +00:00
|
|
|
:
|
|
|
|
<CSSTransition classNames={'fade'} timeout={250}>
|
2020-03-30 05:20:27 +00:00
|
|
|
<>
|
2019-07-10 05:00:29 +00:00
|
|
|
{databases.length > 0 ?
|
2019-07-17 05:15:14 +00:00
|
|
|
databases.map((database, index) => (
|
|
|
|
<DatabaseRow
|
|
|
|
key={database.id}
|
2019-07-27 22:17:50 +00:00
|
|
|
databaseId={database.id}
|
|
|
|
onDelete={() => removeDatabase(database)}
|
2019-07-17 05:15:14 +00:00
|
|
|
className={index > 0 ? 'mt-1' : undefined}
|
|
|
|
/>
|
|
|
|
))
|
2019-07-10 05:00:29 +00:00
|
|
|
:
|
2019-12-22 00:40:28 +00:00
|
|
|
<p className={'text-center text-sm text-neutral-400'}>
|
2020-01-19 21:54:39 +00:00
|
|
|
{server.featureLimits.databases > 0 ?
|
2020-03-30 05:20:27 +00:00
|
|
|
`It looks like you have no databases.`
|
2020-01-19 21:54:39 +00:00
|
|
|
:
|
|
|
|
`Databases cannot be created for this server.`
|
|
|
|
}
|
2019-07-10 05:06:42 +00:00
|
|
|
</p>
|
2019-07-10 05:00:29 +00:00
|
|
|
}
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.create'}>
|
|
|
|
{server.featureLimits.databases > 0 &&
|
|
|
|
<div className={'mt-6 flex justify-end'}>
|
|
|
|
<CreateDatabaseButton onCreated={appendDatabase}/>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</Can>
|
|
|
|
</>
|
2019-07-10 05:00:29 +00:00
|
|
|
</CSSTransition>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|