2020-09-25 02:20:19 +00:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
2020-07-10 02:56:46 +00:00
|
|
|
import useSWR from 'swr';
|
|
|
|
import getServerAllocations from '@/api/server/network/getServerAllocations';
|
|
|
|
import { Allocation } from '@/api/server/getServer';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-08-23 02:05:43 +00:00
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
2020-08-26 05:09:54 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
|
2020-09-25 02:20:19 +00:00
|
|
|
import AllocationRow from '@/components/server/network/AllocationRow';
|
|
|
|
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
|
2020-07-10 02:56:46 +00:00
|
|
|
|
|
|
|
const NetworkContainer = () => {
|
2020-08-26 05:09:54 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
|
|
|
|
|
2020-07-10 03:00:05 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2020-09-25 02:20:19 +00:00
|
|
|
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), {
|
|
|
|
initialData: allocations,
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (error) {
|
|
|
|
clearAndAddHttpError({ key: 'server:network', error });
|
|
|
|
}
|
|
|
|
}, [ error ]);
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2020-09-25 02:20:19 +00:00
|
|
|
const setPrimaryAllocation = useCallback((id: number) => {
|
2020-07-10 02:56:46 +00:00
|
|
|
clearFlashes('server:network');
|
|
|
|
|
2020-07-10 03:36:08 +00:00
|
|
|
const initial = data;
|
|
|
|
mutate(data?.map(a => a.id === id ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2020-07-10 04:00:03 +00:00
|
|
|
setPrimaryServerAllocation(uuid, id)
|
2020-07-10 03:36:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
clearAndAddHttpError({ key: 'server:network', error });
|
|
|
|
mutate(initial, false);
|
|
|
|
});
|
2020-09-25 02:20:19 +00:00
|
|
|
}, []);
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2020-09-25 02:20:19 +00:00
|
|
|
const onNotesAdded = useCallback((id: number, notes: string) => {
|
|
|
|
mutate(data?.map(a => a.id === id ? { ...a, notes } : a), false);
|
|
|
|
}, []);
|
2020-07-10 02:56:46 +00:00
|
|
|
|
|
|
|
return (
|
2020-08-23 02:05:43 +00:00
|
|
|
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
|
2020-07-10 02:56:46 +00:00
|
|
|
{!data ?
|
|
|
|
<Spinner size={'large'} centered/>
|
|
|
|
:
|
2020-09-25 02:20:19 +00:00
|
|
|
data.map(allocation => (
|
|
|
|
<AllocationRow
|
|
|
|
key={`${allocation.ip}:${allocation.port}`}
|
|
|
|
allocation={allocation}
|
|
|
|
onSetPrimary={setPrimaryAllocation}
|
|
|
|
onNotesChanged={onNotesAdded}
|
|
|
|
/>
|
2020-07-10 02:56:46 +00:00
|
|
|
))
|
|
|
|
}
|
2020-08-23 02:05:43 +00:00
|
|
|
</ServerContentBlock>
|
2020-07-10 02:56:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NetworkContainer;
|