ui(admin): add "working" React admin ui
This commit is contained in:
parent
d1c7494933
commit
5402584508
199 changed files with 13387 additions and 151 deletions
|
@ -0,0 +1,216 @@
|
|||
import type { ChangeEvent } from 'react';
|
||||
import { useContext, useEffect } from 'react';
|
||||
import { NavLink } from 'react-router-dom';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
import type { Filters } from '@/api/admin/nodes/allocations/getAllocations';
|
||||
import getAllocations, { Context as AllocationsContext } from '@/api/admin/nodes/allocations/getAllocations';
|
||||
import AdminCheckbox from '@/components/admin/AdminCheckbox';
|
||||
import AdminTable, {
|
||||
ContentWrapper,
|
||||
Loading,
|
||||
NoItems,
|
||||
Pagination,
|
||||
TableBody,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
useTableHooks,
|
||||
} from '@/components/admin/AdminTable';
|
||||
import DeleteAllocationButton from '@/components/admin/nodes/allocations/DeleteAllocationButton';
|
||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||
import useFlash from '@/plugins/useFlash';
|
||||
import { AdminContext } from '@/state/admin';
|
||||
|
||||
function RowCheckbox({ id }: { id: number }) {
|
||||
const isChecked = AdminContext.useStoreState(state => state.allocations.selectedAllocations.indexOf(id) >= 0);
|
||||
const appendSelectedAllocation = AdminContext.useStoreActions(
|
||||
actions => actions.allocations.appendSelectedAllocation,
|
||||
);
|
||||
const removeSelectedAllocation = AdminContext.useStoreActions(
|
||||
actions => actions.allocations.removeSelectedAllocation,
|
||||
);
|
||||
|
||||
return (
|
||||
<AdminCheckbox
|
||||
name={id.toString()}
|
||||
checked={isChecked}
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||
if (e.currentTarget.checked) {
|
||||
appendSelectedAllocation(id);
|
||||
} else {
|
||||
removeSelectedAllocation(id);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
interface Props {
|
||||
nodeId: number;
|
||||
filters?: Filters;
|
||||
}
|
||||
|
||||
function AllocationsTable({ nodeId, filters }: Props) {
|
||||
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
||||
|
||||
const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(AllocationsContext);
|
||||
const { data: allocations, error, isValidating, mutate } = getAllocations(nodeId, ['server']);
|
||||
|
||||
const length = allocations?.items?.length || 0;
|
||||
|
||||
const setSelectedAllocations = AdminContext.useStoreActions(actions => actions.allocations.setSelectedAllocations);
|
||||
const selectedAllocationLength = AdminContext.useStoreState(state => state.allocations.selectedAllocations.length);
|
||||
|
||||
const onSelectAllClick = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setSelectedAllocations(
|
||||
e.currentTarget.checked ? allocations?.items?.map?.(allocation => allocation.id) || [] : [],
|
||||
);
|
||||
};
|
||||
|
||||
const onSearch = (query: string): Promise<void> => {
|
||||
return new Promise(resolve => {
|
||||
if (query.length < 2) {
|
||||
setFilters(filters || null);
|
||||
} else {
|
||||
setFilters({ ...filters, ip: query });
|
||||
}
|
||||
return resolve();
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setSelectedAllocations([]);
|
||||
}, [page]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!error) {
|
||||
clearFlashes('allocations');
|
||||
return;
|
||||
}
|
||||
|
||||
clearAndAddHttpError({ key: 'allocations', error });
|
||||
}, [error]);
|
||||
|
||||
return (
|
||||
<AdminTable>
|
||||
<ContentWrapper
|
||||
checked={selectedAllocationLength === (length === 0 ? -1 : length)}
|
||||
onSelectAllClick={onSelectAllClick}
|
||||
onSearch={onSearch}
|
||||
>
|
||||
<Pagination data={allocations} onPageSelect={setPage}>
|
||||
<div css={tw`overflow-x-auto`}>
|
||||
<table css={tw`w-full table-auto`}>
|
||||
<TableHead>
|
||||
<TableHeader
|
||||
name={'IP Address'}
|
||||
direction={sort === 'ip' ? (sortDirection ? 1 : 2) : null}
|
||||
onClick={() => setSort('ip')}
|
||||
/>
|
||||
<TableHeader name={'Alias'} />
|
||||
<TableHeader
|
||||
name={'Port'}
|
||||
direction={sort === 'port' ? (sortDirection ? 1 : 2) : null}
|
||||
onClick={() => setSort('port')}
|
||||
/>
|
||||
<TableHeader name={'Assigned To'} />
|
||||
<TableHeader />
|
||||
</TableHead>
|
||||
|
||||
<TableBody>
|
||||
{allocations !== undefined &&
|
||||
!error &&
|
||||
!isValidating &&
|
||||
length > 0 &&
|
||||
allocations.items.map(allocation => (
|
||||
<tr key={allocation.id} css={tw`h-10 hover:bg-neutral-600`}>
|
||||
<td css={tw`pl-6`}>
|
||||
<RowCheckbox id={allocation.id} />
|
||||
</td>
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<CopyOnClick text={allocation.ip}>
|
||||
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
|
||||
{allocation.ip}
|
||||
</code>
|
||||
</CopyOnClick>
|
||||
</td>
|
||||
|
||||
{allocation.alias !== null ? (
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<CopyOnClick text={allocation.alias}>
|
||||
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
|
||||
{allocation.alias}
|
||||
</code>
|
||||
</CopyOnClick>
|
||||
</td>
|
||||
) : (
|
||||
<td />
|
||||
)}
|
||||
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<CopyOnClick text={allocation.port}>
|
||||
<code css={tw`font-mono bg-neutral-900 rounded py-1 px-2`}>
|
||||
{allocation.port}
|
||||
</code>
|
||||
</CopyOnClick>
|
||||
</td>
|
||||
|
||||
{allocation.relations.server !== undefined ? (
|
||||
<td css={tw`px-6 text-sm text-neutral-200 text-left whitespace-nowrap`}>
|
||||
<NavLink
|
||||
to={`/admin/servers/${allocation.serverId}`}
|
||||
css={tw`text-primary-400 hover:text-primary-300`}
|
||||
>
|
||||
{allocation.relations.server.name}
|
||||
</NavLink>
|
||||
</td>
|
||||
) : (
|
||||
<td />
|
||||
)}
|
||||
|
||||
<td>
|
||||
<DeleteAllocationButton
|
||||
nodeId={nodeId}
|
||||
allocationId={allocation.id}
|
||||
onDeleted={async () => {
|
||||
await mutate(allocations => ({
|
||||
pagination: allocations!.pagination,
|
||||
items: allocations!.items.filter(
|
||||
a => a.id === allocation.id,
|
||||
),
|
||||
}));
|
||||
|
||||
// Go back a page if no more items will exist on the current page.
|
||||
if (allocations?.items.length - (1 % 10) === 0) {
|
||||
setPage(p => p - 1);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</TableBody>
|
||||
</table>
|
||||
|
||||
{allocations === undefined || (error && isValidating) ? (
|
||||
<Loading />
|
||||
) : length < 1 ? (
|
||||
<NoItems />
|
||||
) : null}
|
||||
</div>
|
||||
</Pagination>
|
||||
</ContentWrapper>
|
||||
</AdminTable>
|
||||
);
|
||||
}
|
||||
|
||||
export default (props: Props) => {
|
||||
const hooks = useTableHooks<Filters>(props.filters);
|
||||
|
||||
return (
|
||||
<AllocationsContext.Provider value={hooks}>
|
||||
<AllocationsTable {...props} />
|
||||
</AllocationsContext.Provider>
|
||||
);
|
||||
};
|
|
@ -0,0 +1,118 @@
|
|||
import type { FormikHelpers } from 'formik';
|
||||
import { Form, Formik } from 'formik';
|
||||
import { useEffect, useState } from 'react';
|
||||
import tw from 'twin.macro';
|
||||
import { array, number, object, string } from 'yup';
|
||||
|
||||
import createAllocation from '@/api/admin/nodes/allocations/createAllocation';
|
||||
import getAllocations from '@/api/admin/nodes/getAllocations';
|
||||
import getAllocations2 from '@/api/admin/nodes/allocations/getAllocations';
|
||||
import Button from '@/components/elements/Button';
|
||||
import Field from '@/components/elements/Field';
|
||||
import type { Option } from '@/components/elements/SelectField';
|
||||
import SelectField from '@/components/elements/SelectField';
|
||||
|
||||
interface Values {
|
||||
ips: string[];
|
||||
ports: number[];
|
||||
alias: string;
|
||||
}
|
||||
|
||||
const distinct = (value: any, index: any, self: any) => {
|
||||
return self.indexOf(value) === index;
|
||||
};
|
||||
|
||||
function CreateAllocationForm({ nodeId }: { nodeId: number }) {
|
||||
const [ips, setIPs] = useState<Option[]>([]);
|
||||
const [ports] = useState<Option[]>([]);
|
||||
|
||||
const { mutate } = getAllocations2(nodeId, ['server']);
|
||||
|
||||
useEffect(() => {
|
||||
getAllocations(nodeId).then(allocations => {
|
||||
setIPs(
|
||||
allocations
|
||||
.map(a => a.ip)
|
||||
.filter(distinct)
|
||||
.map(ip => {
|
||||
return { value: ip, label: ip };
|
||||
}),
|
||||
);
|
||||
});
|
||||
}, [nodeId]);
|
||||
|
||||
const isValidIP = (inputValue: string): boolean => {
|
||||
// TODO: Better way of checking for a valid ip (and CIDR)
|
||||
return inputValue.match(/^([0-9a-f.:/]+)$/) !== null;
|
||||
};
|
||||
|
||||
const isValidPort = (inputValue: string): boolean => {
|
||||
// TODO: Better way of checking for a valid port (and port range)
|
||||
return inputValue.match(/^([0-9-]+)$/) !== null;
|
||||
};
|
||||
|
||||
const submit = ({ ips, ports, alias }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
||||
setSubmitting(false);
|
||||
|
||||
ips.forEach(async ip => {
|
||||
const allocations = await createAllocation(nodeId, { ip, ports, alias }, ['server']);
|
||||
await mutate(data => ({ ...data!, items: { ...data!.items!, ...allocations } }));
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
onSubmit={submit}
|
||||
initialValues={{
|
||||
ips: [] as string[],
|
||||
ports: [] as number[],
|
||||
alias: '',
|
||||
}}
|
||||
validationSchema={object().shape({
|
||||
ips: array(string()).min(1, 'You must select at least one ip address.'),
|
||||
ports: array(number()).min(1, 'You must select at least one port.'),
|
||||
})}
|
||||
>
|
||||
{({ isSubmitting, isValid }) => (
|
||||
<Form>
|
||||
<SelectField
|
||||
id={'ips'}
|
||||
name={'ips'}
|
||||
label={'IPs and CIDRs'}
|
||||
options={ips}
|
||||
isValidNewOption={isValidIP}
|
||||
isMulti
|
||||
isSearchable
|
||||
isCreatable
|
||||
css={tw`mb-6`}
|
||||
/>
|
||||
|
||||
<SelectField
|
||||
id={'ports'}
|
||||
name={'ports'}
|
||||
label={'Ports'}
|
||||
options={ports}
|
||||
isValidNewOption={isValidPort}
|
||||
isMulti
|
||||
isSearchable
|
||||
isCreatable
|
||||
/>
|
||||
|
||||
<div css={tw`mt-6`}>
|
||||
<Field id={'alias'} name={'alias'} label={'Alias'} type={'text'} />
|
||||
</div>
|
||||
|
||||
<div css={tw`w-full flex flex-row items-center mt-6`}>
|
||||
<div css={tw`flex ml-auto`}>
|
||||
<Button type={'submit'} disabled={isSubmitting || !isValid}>
|
||||
Create Allocations
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
);
|
||||
}
|
||||
|
||||
export default CreateAllocationForm;
|
|
@ -0,0 +1,77 @@
|
|||
import type { Actions } from 'easy-peasy';
|
||||
import { useStoreActions } from 'easy-peasy';
|
||||
import { useState } from 'react';
|
||||
import tw from 'twin.macro';
|
||||
|
||||
import deleteAllocation from '@/api/admin/nodes/allocations/deleteAllocation';
|
||||
import Button from '@/components/elements/Button';
|
||||
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
||||
import type { ApplicationStore } from '@/state';
|
||||
|
||||
interface Props {
|
||||
nodeId: number;
|
||||
allocationId: number;
|
||||
onDeleted?: () => void;
|
||||
}
|
||||
|
||||
export default ({ nodeId, allocationId, onDeleted }: Props) => {
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
||||
(actions: Actions<ApplicationStore>) => actions.flashes,
|
||||
);
|
||||
|
||||
const onDelete = () => {
|
||||
setLoading(true);
|
||||
clearFlashes('allocation');
|
||||
|
||||
deleteAllocation(nodeId, allocationId)
|
||||
.then(() => {
|
||||
setLoading(false);
|
||||
setVisible(false);
|
||||
if (onDeleted !== undefined) {
|
||||
onDeleted();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
clearAndAddHttpError({ key: 'allocation', error });
|
||||
|
||||
setLoading(false);
|
||||
setVisible(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<ConfirmationModal
|
||||
visible={visible}
|
||||
title={'Delete allocation?'}
|
||||
buttonText={'Yes, delete allocation'}
|
||||
onConfirmed={onDelete}
|
||||
showSpinnerOverlay={loading}
|
||||
onModalDismissed={() => setVisible(false)}
|
||||
>
|
||||
Are you sure you want to delete this allocation?
|
||||
</ConfirmationModal>
|
||||
|
||||
<Button type={'button'} size={'inline'} color={'red'} onClick={() => setVisible(true)}>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
css={tw`h-5 w-5`}
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue