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 { 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';
|
2020-04-10 17:56:25 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import useServer from '@/plugins/useServer';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2019-07-10 05:00:29 +00:00
|
|
|
|
|
|
|
export default () => {
|
2020-04-10 17:56:25 +00:00
|
|
|
const { uuid, featureLimits } = useServer();
|
|
|
|
const { addError, clearFlashes } = useFlash();
|
2019-07-10 05:00:29 +00:00
|
|
|
const [ loading, setLoading ] = useState(true);
|
2020-04-10 17:56:25 +00:00
|
|
|
|
|
|
|
const databases = ServerContext.useStoreState(state => state.databases.data);
|
|
|
|
const setDatabases = ServerContext.useStoreActions(state => state.databases.setDatabases);
|
2019-07-10 05:00:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-10 17:56:25 +00:00
|
|
|
getServerDatabases(uuid)
|
|
|
|
.then(databases => setDatabases(databases))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ key: 'databases', message: httpErrorToHuman(error) });
|
2019-07-10 05:00:29 +00:00
|
|
|
})
|
2020-04-10 17:56:25 +00:00
|
|
|
.then(() => setLoading(false));
|
2019-07-10 05:00:29 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2020-04-17 18:17:01 +00:00
|
|
|
<PageContentBlock>
|
2020-04-10 17:56:25 +00:00
|
|
|
<FlashMessageRender byKey={'databases'} className={'mb-4'}/>
|
|
|
|
{(!databases.length && 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}
|
2020-04-10 17:56:25 +00:00
|
|
|
database={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-04-10 17:56:25 +00:00
|
|
|
{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'}>
|
2020-06-24 03:13:58 +00:00
|
|
|
{(featureLimits.databases > 0 && databases.length > 0) &&
|
|
|
|
<p className="text-center text-xs text-neutral-400 mt-2">
|
|
|
|
{databases.length} of {featureLimits.databases} databases have been allocated to this server.
|
|
|
|
</p>
|
|
|
|
}
|
2020-06-03 03:40:40 +00:00
|
|
|
{featureLimits.databases > 0 && featureLimits.databases !== databases.length &&
|
2020-03-30 05:20:27 +00:00
|
|
|
<div className={'mt-6 flex justify-end'}>
|
2020-04-10 17:56:25 +00:00
|
|
|
<CreateDatabaseButton/>
|
2020-03-30 05:20:27 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</Can>
|
|
|
|
</>
|
2019-07-10 05:00:29 +00:00
|
|
|
</CSSTransition>
|
|
|
|
}
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2019-07-10 05:00:29 +00:00
|
|
|
);
|
|
|
|
};
|