Logic fixes
This commit is contained in:
parent
6cb21fb920
commit
49c29aae47
3 changed files with 33 additions and 31 deletions
|
@ -4,12 +4,12 @@ import http from '@/api/http';
|
||||||
import { rawDataToServerAllocation } from '@/api/transformers';
|
import { rawDataToServerAllocation } from '@/api/transformers';
|
||||||
import { Allocation } from '@/api/server/getServer';
|
import { Allocation } from '@/api/server/getServer';
|
||||||
|
|
||||||
export default (initialData?: Allocation[]) => {
|
export default () => {
|
||||||
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
||||||
|
|
||||||
return useSWR<Allocation[]>([ 'server:allocations', uuid ], async () => {
|
return useSWR<Allocation[]>([ 'server:allocations', uuid ], async () => {
|
||||||
const { data } = await http.get(`/api/client/servers/${uuid}/network/allocations`);
|
const { data } = await http.get(`/api/client/servers/${uuid}/network/allocations`);
|
||||||
|
|
||||||
return (data.data || []).map(rawDataToServerAllocation);
|
return (data.data || []).map(rawDataToServerAllocation);
|
||||||
}, { initialData, revalidateOnFocus: false });
|
}, { revalidateOnFocus: false, revalidateOnMount: false });
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { memo, useState } from 'react';
|
import React, { memo, useCallback, useState } from 'react';
|
||||||
import isEqual from 'react-fast-compare';
|
import isEqual from 'react-fast-compare';
|
||||||
import tw from 'twin.macro';
|
import tw from 'twin.macro';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
|
@ -16,20 +16,25 @@ import useFlash from '@/plugins/useFlash';
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import CopyOnClick from '@/components/elements/CopyOnClick';
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
||||||
import DeleteAllocationButton from '@/components/server/network/DeleteAllocationButton';
|
import DeleteAllocationButton from '@/components/server/network/DeleteAllocationButton';
|
||||||
|
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
|
||||||
|
import getServerAllocations from '@/api/swr/getServerAllocations';
|
||||||
|
|
||||||
const Code = styled.code`${tw`font-mono py-1 px-2 bg-neutral-900 rounded text-sm inline-block`}`;
|
const Code = styled.code`${tw`font-mono py-1 px-2 bg-neutral-900 rounded text-sm inline-block`}`;
|
||||||
const Label = styled.label`${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}`;
|
const Label = styled.label`${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}`;
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
allocation: Allocation;
|
allocation: Allocation;
|
||||||
onSetPrimary: (id: number) => void;
|
|
||||||
onNotesChanged: (id: number, notes: string) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
|
const AllocationRow = ({ allocation }: Props) => {
|
||||||
const [ loading, setLoading ] = useState(false);
|
const [ loading, setLoading ] = useState(false);
|
||||||
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
||||||
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
||||||
|
const { mutate } = getServerAllocations();
|
||||||
|
|
||||||
|
const onNotesChanged = useCallback((id: number, notes: string) => {
|
||||||
|
mutate(data => data?.map(a => a.id === id ? { ...a, notes } : a), false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const setAllocationNotes = debounce((notes: string) => {
|
const setAllocationNotes = debounce((notes: string) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
@ -41,6 +46,17 @@ const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
|
||||||
.then(() => setLoading(false));
|
.then(() => setLoading(false));
|
||||||
}, 750);
|
}, 750);
|
||||||
|
|
||||||
|
const setPrimaryAllocation = () => {
|
||||||
|
clearFlashes('server:network');
|
||||||
|
mutate(data => data?.map(a => ({ ...a, isDefault: a.id === allocation.id })), false);
|
||||||
|
|
||||||
|
setPrimaryServerAllocation(uuid, allocation.id)
|
||||||
|
.catch(error => {
|
||||||
|
clearAndAddHttpError({ key: 'server:network', error });
|
||||||
|
mutate();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GreyRowBox $hoverable={false} css={tw`flex-wrap md:flex-no-wrap mt-2`}>
|
<GreyRowBox $hoverable={false} css={tw`flex-wrap md:flex-no-wrap mt-2`}>
|
||||||
<div css={tw`flex items-center w-full md:w-auto`}>
|
<div css={tw`flex items-center w-full md:w-auto`}>
|
||||||
|
@ -81,7 +97,7 @@ const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
|
||||||
isSecondary
|
isSecondary
|
||||||
size={'xsmall'}
|
size={'xsmall'}
|
||||||
color={'primary'}
|
color={'primary'}
|
||||||
onClick={() => onSetPrimary(allocation.id)}
|
onClick={setPrimaryAllocation}
|
||||||
>
|
>
|
||||||
Make Primary
|
Make Primary
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
@ -1,26 +1,31 @@
|
||||||
import React, { useCallback, useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import Spinner from '@/components/elements/Spinner';
|
import Spinner from '@/components/elements/Spinner';
|
||||||
import useFlash from '@/plugins/useFlash';
|
import useFlash from '@/plugins/useFlash';
|
||||||
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
|
|
||||||
import AllocationRow from '@/components/server/network/AllocationRow';
|
import AllocationRow from '@/components/server/network/AllocationRow';
|
||||||
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
|
|
||||||
import Button from '@/components/elements/Button';
|
import Button from '@/components/elements/Button';
|
||||||
import createServerAllocation from '@/api/server/network/createServerAllocation';
|
import createServerAllocation from '@/api/server/network/createServerAllocation';
|
||||||
import tw from 'twin.macro';
|
import tw from 'twin.macro';
|
||||||
import Can from '@/components/elements/Can';
|
import Can from '@/components/elements/Can';
|
||||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
import getServerAllocations from '@/api/swr/getServerAllocations';
|
import getServerAllocations from '@/api/swr/getServerAllocations';
|
||||||
|
import isEqual from 'react-fast-compare';
|
||||||
|
import { Allocation } from '@/api/server/getServer';
|
||||||
|
|
||||||
const NetworkContainer = () => {
|
const NetworkContainer = () => {
|
||||||
const [ loading, setLoading ] = useState(false);
|
const [ loading, setLoading ] = useState(false);
|
||||||
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
||||||
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
|
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
|
||||||
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
|
// @ts-ignore
|
||||||
|
const allocations: Allocation[] = ServerContext.useStoreState(state => state.server.data!.allocations, isEqual);
|
||||||
|
|
||||||
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
||||||
const { data, error, mutate } = getServerAllocations(allocations);
|
const { data, error, mutate } = getServerAllocations();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
mutate(allocations, false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -28,19 +33,6 @@ const NetworkContainer = () => {
|
||||||
}
|
}
|
||||||
}, [ error ]);
|
}, [ error ]);
|
||||||
|
|
||||||
const setPrimaryAllocation = (id: number) => {
|
|
||||||
clearFlashes('server:network');
|
|
||||||
|
|
||||||
const initial = data;
|
|
||||||
mutate(data?.map(a => a.id === id ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
|
|
||||||
|
|
||||||
setPrimaryServerAllocation(uuid, id)
|
|
||||||
.catch(error => {
|
|
||||||
clearAndAddHttpError({ key: 'server:network', error });
|
|
||||||
mutate(initial, false);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const onCreateAllocation = () => {
|
const onCreateAllocation = () => {
|
||||||
clearFlashes('server:network');
|
clearFlashes('server:network');
|
||||||
|
|
||||||
|
@ -51,10 +43,6 @@ const NetworkContainer = () => {
|
||||||
.then(() => setLoading(false));
|
.then(() => setLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
const onNotesAdded = useCallback((id: number, notes: string) => {
|
|
||||||
mutate(data?.map(a => a.id === id ? { ...a, notes } : a), false);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
|
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
|
||||||
{!data ?
|
{!data ?
|
||||||
|
@ -66,8 +54,6 @@ const NetworkContainer = () => {
|
||||||
<AllocationRow
|
<AllocationRow
|
||||||
key={`${allocation.ip}:${allocation.port}`}
|
key={`${allocation.ip}:${allocation.port}`}
|
||||||
allocation={allocation}
|
allocation={allocation}
|
||||||
onSetPrimary={setPrimaryAllocation}
|
|
||||||
onNotesChanged={onNotesAdded}
|
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue