2021-01-05 21:52:49 +00:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
|
import getDatabases, { Context as DatabasesContext } from '@/api/admin/databases/getDatabases';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { AdminContext } from '@/state/admin';
|
|
|
|
import { NavLink, useRouteMatch } from 'react-router-dom';
|
2020-08-22 22:49:18 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
2021-01-05 21:52:49 +00:00
|
|
|
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
|
|
|
import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper } from '@/components/admin/AdminTable';
|
2020-08-22 22:49:18 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2021-05-19 02:53:42 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2020-08-22 22:49:18 +00:00
|
|
|
|
2021-01-05 21:52:49 +00:00
|
|
|
const RowCheckbox = ({ id }: { id: number}) => {
|
|
|
|
const isChecked = AdminContext.useStoreState(state => state.databases.selectedDatabases.indexOf(id) >= 0);
|
|
|
|
const appendSelectedDatabase = AdminContext.useStoreActions(actions => actions.databases.appendSelectedDatabase);
|
|
|
|
const removeSelectedDatabase = AdminContext.useStoreActions(actions => actions.databases.removeSelectedDatabase);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AdminCheckbox
|
|
|
|
name={id.toString()}
|
|
|
|
checked={isChecked}
|
|
|
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.currentTarget.checked) {
|
|
|
|
appendSelectedDatabase(id);
|
|
|
|
} else {
|
|
|
|
removeSelectedDatabase(id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DatabasesContainer = () => {
|
|
|
|
const match = useRouteMatch();
|
|
|
|
|
|
|
|
const { page, setPage } = useContext(DatabasesContext);
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
const { data: databases, error, isValidating } = getDatabases();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!error) {
|
|
|
|
clearFlashes('databases');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:02:49 +00:00
|
|
|
clearAndAddHttpError({ key: 'databases', error });
|
2021-01-05 21:52:49 +00:00
|
|
|
}, [ error ]);
|
|
|
|
|
|
|
|
const length = databases?.items?.length || 0;
|
|
|
|
|
|
|
|
const setSelectedDatabases = AdminContext.useStoreActions(actions => actions.databases.setSelectedDatabases);
|
|
|
|
const selectedDatabasesLength = AdminContext.useStoreState(state => state.databases.selectedDatabases.length);
|
|
|
|
|
|
|
|
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSelectedDatabases(e.currentTarget.checked ? (databases?.items?.map(database => database.id) || []) : []);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSelectedDatabases([]);
|
|
|
|
}, [ page ]);
|
|
|
|
|
2020-08-22 22:49:18 +00:00
|
|
|
return (
|
2021-01-07 16:44:24 +00:00
|
|
|
<AdminContentBlock title={'Databases'}>
|
2021-01-05 21:52:49 +00:00
|
|
|
<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-06 22:39:23 +00:00
|
|
|
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>Database Hosts</h2>
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>Database hosts that servers can have databases created on.</p>
|
2020-08-22 22:49:18 +00:00
|
|
|
</div>
|
|
|
|
|
2021-01-10 18:34:14 +00:00
|
|
|
<div css={tw`flex ml-auto pl-4`}>
|
|
|
|
<NavLink to={`${match.url}/new`}>
|
|
|
|
<Button type={'button'} size={'large'} css={tw`h-10 px-4 py-0 whitespace-nowrap`}>
|
|
|
|
New Database Host
|
|
|
|
</Button>
|
|
|
|
</NavLink>
|
|
|
|
</div>
|
2020-08-22 22:49:18 +00:00
|
|
|
</div>
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
<FlashMessageRender byKey={'databases'} css={tw`mb-4`}/>
|
|
|
|
|
|
|
|
<AdminTable>
|
|
|
|
{ databases === undefined || (error && isValidating) ?
|
|
|
|
<Loading/>
|
|
|
|
:
|
|
|
|
length < 1 ?
|
|
|
|
<NoItems/>
|
|
|
|
:
|
|
|
|
<ContentWrapper
|
|
|
|
checked={selectedDatabasesLength === (length === 0 ? -1 : length)}
|
|
|
|
onSelectAllClick={onSelectAllClick}
|
|
|
|
>
|
|
|
|
<Pagination data={databases} onPageSelect={setPage}>
|
|
|
|
<div css={tw`overflow-x-auto`}>
|
|
|
|
<table css={tw`w-full table-auto`}>
|
|
|
|
<TableHead>
|
|
|
|
<TableHeader name={'ID'}/>
|
|
|
|
<TableHeader name={'Name'}/>
|
|
|
|
<TableHeader name={'Address'}/>
|
2021-01-06 16:17:54 +00:00
|
|
|
<TableHeader name={'Username'}/>
|
2021-01-05 21:52:49 +00:00
|
|
|
</TableHead>
|
|
|
|
|
|
|
|
<TableBody>
|
|
|
|
{
|
|
|
|
databases.items.map(database => (
|
|
|
|
<TableRow key={database.id}>
|
|
|
|
<td css={tw`pl-6`}>
|
|
|
|
<RowCheckbox id={database.id}/>
|
|
|
|
</td>
|
|
|
|
|
2021-01-06 16:17:54 +00:00
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<CopyOnClick text={database.id.toString()}>
|
|
|
|
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{database.id}</code>
|
|
|
|
</CopyOnClick>
|
|
|
|
</td>
|
|
|
|
|
2021-01-05 21:52:49 +00:00
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<NavLink to={`${match.url}/${database.id}`} css={tw`text-primary-400 hover:text-primary-300`}>
|
|
|
|
{database.name}
|
|
|
|
</NavLink>
|
|
|
|
</td>
|
2021-01-06 16:17:54 +00:00
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
|
|
|
<CopyOnClick text={database.getAddress()}>
|
|
|
|
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>{database.getAddress()}</code>
|
|
|
|
</CopyOnClick>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>{database.username}</td>
|
2021-01-05 21:52:49 +00:00
|
|
|
</TableRow>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</TableBody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</Pagination>
|
|
|
|
</ContentWrapper>
|
|
|
|
}
|
|
|
|
</AdminTable>
|
2020-08-22 22:49:18 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|
2021-01-05 21:52:49 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const [ page, setPage ] = useState<number>(1);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DatabasesContext.Provider value={{ page, setPage }}>
|
|
|
|
<DatabasesContainer/>
|
|
|
|
</DatabasesContext.Provider>
|
|
|
|
);
|
|
|
|
};
|