Skip to content

Commit 49c29aa

Browse files
committed
Logic fixes
1 parent 6cb21fb commit 49c29aa

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

resources/scripts/api/swr/getServerAllocations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import http from '@/api/http';
44
import { rawDataToServerAllocation } from '@/api/transformers';
55
import { Allocation } from '@/api/server/getServer';
66

7-
export default (initialData?: Allocation[]) => {
7+
export default () => {
88
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
99

1010
return useSWR<Allocation[]>([ 'server:allocations', uuid ], async () => {
1111
const { data } = await http.get(`/api/client/servers/${uuid}/network/allocations`);
1212

1313
return (data.data || []).map(rawDataToServerAllocation);
14-
}, { initialData, revalidateOnFocus: false });
14+
}, { revalidateOnFocus: false, revalidateOnMount: false });
1515
};

resources/scripts/components/server/network/AllocationRow.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo, useState } from 'react';
1+
import React, { memo, useCallback, useState } from 'react';
22
import isEqual from 'react-fast-compare';
33
import tw from 'twin.macro';
44
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
@@ -16,20 +16,25 @@ import useFlash from '@/plugins/useFlash';
1616
import { ServerContext } from '@/state/server';
1717
import CopyOnClick from '@/components/elements/CopyOnClick';
1818
import DeleteAllocationButton from '@/components/server/network/DeleteAllocationButton';
19+
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
20+
import getServerAllocations from '@/api/swr/getServerAllocations';
1921

2022
const Code = styled.code`${tw`font-mono py-1 px-2 bg-neutral-900 rounded text-sm inline-block`}`;
2123
const Label = styled.label`${tw`uppercase text-xs mt-1 text-neutral-400 block px-1 select-none transition-colors duration-150`}`;
2224

2325
interface Props {
2426
allocation: Allocation;
25-
onSetPrimary: (id: number) => void;
26-
onNotesChanged: (id: number, notes: string) => void;
2727
}
2828

29-
const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
29+
const AllocationRow = ({ allocation }: Props) => {
3030
const [ loading, setLoading ] = useState(false);
3131
const { clearFlashes, clearAndAddHttpError } = useFlash();
3232
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
33+
const { mutate } = getServerAllocations();
34+
35+
const onNotesChanged = useCallback((id: number, notes: string) => {
36+
mutate(data => data?.map(a => a.id === id ? { ...a, notes } : a), false);
37+
}, []);
3338

3439
const setAllocationNotes = debounce((notes: string) => {
3540
setLoading(true);
@@ -41,6 +46,17 @@ const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
4146
.then(() => setLoading(false));
4247
}, 750);
4348

49+
const setPrimaryAllocation = () => {
50+
clearFlashes('server:network');
51+
mutate(data => data?.map(a => ({ ...a, isDefault: a.id === allocation.id })), false);
52+
53+
setPrimaryServerAllocation(uuid, allocation.id)
54+
.catch(error => {
55+
clearAndAddHttpError({ key: 'server:network', error });
56+
mutate();
57+
});
58+
};
59+
4460
return (
4561
<GreyRowBox $hoverable={false} css={tw`flex-wrap md:flex-no-wrap mt-2`}>
4662
<div css={tw`flex items-center w-full md:w-auto`}>
@@ -81,7 +97,7 @@ const AllocationRow = ({ allocation, onSetPrimary, onNotesChanged }: Props) => {
8197
isSecondary
8298
size={'xsmall'}
8399
color={'primary'}
84-
onClick={() => onSetPrimary(allocation.id)}
100+
onClick={setPrimaryAllocation}
85101
>
86102
Make Primary
87103
</Button>

resources/scripts/components/server/network/NetworkContainer.tsx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
1-
import React, { useCallback, useEffect, useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import Spinner from '@/components/elements/Spinner';
33
import useFlash from '@/plugins/useFlash';
44
import ServerContentBlock from '@/components/elements/ServerContentBlock';
55
import { ServerContext } from '@/state/server';
6-
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
76
import AllocationRow from '@/components/server/network/AllocationRow';
8-
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
97
import Button from '@/components/elements/Button';
108
import createServerAllocation from '@/api/server/network/createServerAllocation';
119
import tw from 'twin.macro';
1210
import Can from '@/components/elements/Can';
1311
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
1412
import getServerAllocations from '@/api/swr/getServerAllocations';
13+
import isEqual from 'react-fast-compare';
14+
import { Allocation } from '@/api/server/getServer';
1515

1616
const NetworkContainer = () => {
1717
const [ loading, setLoading ] = useState(false);
1818
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
1919
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
20-
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
20+
// @ts-ignore
21+
const allocations: Allocation[] = ServerContext.useStoreState(state => state.server.data!.allocations, isEqual);
2122

2223
const { clearFlashes, clearAndAddHttpError } = useFlash();
23-
const { data, error, mutate } = getServerAllocations(allocations);
24+
const { data, error, mutate } = getServerAllocations();
25+
26+
useEffect(() => {
27+
mutate(allocations, false);
28+
}, []);
2429

2530
useEffect(() => {
2631
if (error) {
2732
clearAndAddHttpError({ key: 'server:network', error });
2833
}
2934
}, [ error ]);
3035

31-
const setPrimaryAllocation = (id: number) => {
32-
clearFlashes('server:network');
33-
34-
const initial = data;
35-
mutate(data?.map(a => a.id === id ? { ...a, isDefault: true } : { ...a, isDefault: false }), false);
36-
37-
setPrimaryServerAllocation(uuid, id)
38-
.catch(error => {
39-
clearAndAddHttpError({ key: 'server:network', error });
40-
mutate(initial, false);
41-
});
42-
};
43-
4436
const onCreateAllocation = () => {
4537
clearFlashes('server:network');
4638

@@ -51,10 +43,6 @@ const NetworkContainer = () => {
5143
.then(() => setLoading(false));
5244
};
5345

54-
const onNotesAdded = useCallback((id: number, notes: string) => {
55-
mutate(data?.map(a => a.id === id ? { ...a, notes } : a), false);
56-
}, []);
57-
5846
return (
5947
<ServerContentBlock showFlashKey={'server:network'} title={'Network'}>
6048
{!data ?
@@ -66,8 +54,6 @@ const NetworkContainer = () => {
6654
<AllocationRow
6755
key={`${allocation.ip}:${allocation.port}`}
6856
allocation={allocation}
69-
onSetPrimary={setPrimaryAllocation}
70-
onNotesChanged={onNotesAdded}
7157
/>
7258
))
7359
}

0 commit comments

Comments
 (0)