Update allocations to support ids; protect endpoints; support notes

This commit is contained in:
Dane Everitt 2020-07-09 20:36:08 -07:00
parent 9c3b9a0fae
commit 2278927fb6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
19 changed files with 217 additions and 62 deletions

View file

@ -2,9 +2,11 @@ import http, { FractalResponseData, FractalResponseList } from '@/api/http';
import { rawDataToServerAllocation } from '@/api/transformers';
export interface Allocation {
id: number;
ip: string;
alias: string | null;
port: number;
notes: string | null;
isDefault: boolean;
}

View file

@ -0,0 +1,4 @@
import { Allocation } from '@/api/server/getServer';
import http from '@/api/http';
export default async (uuid: string, id: number): Promise<Allocation> => await http.delete(`/api/client/servers/${uuid}/network/allocations/${id}`);

View file

@ -3,7 +3,7 @@ import { rawDataToServerAllocation } from '@/api/transformers';
import { Allocation } from '@/api/server/getServer';
export default async (uuid: string): Promise<Allocation[]> => {
const { data } = await http.get(`/api/client/servers/${uuid}/network`);
const { data } = await http.get(`/api/client/servers/${uuid}/network/allocations`);
return (data.data || []).map(rawDataToServerAllocation);
};

View file

@ -2,8 +2,8 @@ import { Allocation } from '@/api/server/getServer';
import http from '@/api/http';
import { rawDataToServerAllocation } from '@/api/transformers';
export default async (uuid: string, ip: string, port: number): Promise<Allocation> => {
const { data } = await http.put(`/api/client/servers/${uuid}/network/primary`, { ip, port });
export default async (uuid: string, id: number): Promise<Allocation> => {
const { data } = await http.post(`/api/client/servers/${uuid}/network/allocations/${id}/primary`);
return rawDataToServerAllocation(data);
};

View file

@ -0,0 +1,9 @@
import { Allocation } from '@/api/server/getServer';
import http from '@/api/http';
import { rawDataToServerAllocation } from '@/api/transformers';
export default async (uuid: string, id: number, notes: string | null): Promise<Allocation> => {
const { data } = await http.post(`/api/client/servers/${uuid}/network/allocations/${id}`, { notes });
return rawDataToServerAllocation(data);
};

View file

@ -2,8 +2,10 @@ import { Allocation } from '@/api/server/getServer';
import { FractalResponseData } from '@/api/http';
export const rawDataToServerAllocation = (data: FractalResponseData): Allocation => ({
id: data.attributes.id,
ip: data.attributes.ip,
alias: data.attributes.ip_alias,
port: data.attributes.port,
notes: data.attributes.notes,
isDefault: data.attributes.is_default,
});

View file

@ -23,16 +23,17 @@ const NetworkContainer = () => {
const { clearFlashes, clearAndAddHttpError } = useFlash();
const { data, error, mutate } = useSWR<Allocation[]>(server.uuid, key => getServerAllocations(key), { initialData: server.allocations });
const setPrimaryAllocation = (ip: string, port: number) => {
const setPrimaryAllocation = (id: number) => {
clearFlashes('server:network');
mutate(data?.map(a => (a.ip === ip && a.port === port) ? { ...a, isDefault: true } : {
...a,
isDefault: false,
}), false);
const initial = data;
mutate(data?.map(a => a.id === id ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
setPrimaryServerAllocation(server.uuid, ip, port)
.catch(error => clearAndAddHttpError({ key: 'server:network', error }));
setPrimaryServerAllocation(server.uuid, id)
.catch(error => {
clearAndAddHttpError({ key: 'server:network', error });
mutate(initial, false);
});
};
useEffect(() => {
@ -46,7 +47,7 @@ const NetworkContainer = () => {
{!data ?
<Spinner size={'large'} centered/>
:
data.map(({ ip, port, alias, isDefault }, index) => (
data.map(({ id, ip, port, alias, isDefault }, index) => (
<GreyRowBox key={`${ip}:${port}`} css={index > 0 ? tw`mt-2` : undefined}>
<div css={tw`pl-4 pr-6 text-neutral-400`}>
<FontAwesomeIcon icon={faNetworkWired}/>
@ -70,7 +71,7 @@ const NetworkContainer = () => {
isSecondary
size={'xsmall'}
color={'primary'}
onClick={() => setPrimaryAllocation(ip, port)}
onClick={() => setPrimaryAllocation(id)}
>
Make Primary
</Button>