Skip to content

Commit db7e4e7

Browse files
committed
UI cleanup for add allocation button
1 parent abb043c commit db7e4e7

File tree

2 files changed

+37
-49
lines changed

2 files changed

+37
-49
lines changed

resources/scripts/api/server/network/newServerAllocation.ts renamed to resources/scripts/api/server/network/createServerAllocation.ts

File renamed without changes.

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

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import { useDeepMemoize } from '@/plugins/useDeepMemoize';
1010
import AllocationRow from '@/components/server/network/AllocationRow';
1111
import setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
1212
import Button from '@/components/elements/Button';
13-
import newServerAllocation from '@/api/server/network/newServerAllocation';
13+
import createServerAllocation from '@/api/server/network/createServerAllocation';
1414
import tw from 'twin.macro';
15-
import GreyRowBox from '@/components/elements/GreyRowBox';
15+
import Can from '@/components/elements/Can';
16+
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
1617

1718
const NetworkContainer = () => {
19+
const [ loading, setLoading ] = useState(false);
1820
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
1921
const allocationLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.allocations);
2022
const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
21-
const [ addingAllocation, setAddingAllocation ] = useState(false);
2223

2324
const { clearFlashes, clearAndAddHttpError } = useFlash();
2425
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), {
@@ -32,7 +33,7 @@ const NetworkContainer = () => {
3233
}
3334
}, [ error ]);
3435

35-
const setPrimaryAllocation = useCallback((id: number) => {
36+
const setPrimaryAllocation = (id: number) => {
3637
clearFlashes('server:network');
3738

3839
const initial = data;
@@ -43,24 +44,16 @@ const NetworkContainer = () => {
4344
clearAndAddHttpError({ key: 'server:network', error });
4445
mutate(initial, false);
4546
});
46-
}, []);
47+
};
4748

48-
const getNewAllocation = () => {
49+
const onCreateAllocation = () => {
4950
clearFlashes('server:network');
50-
setAddingAllocation(true);
5151

52-
const initial = data;
53-
54-
newServerAllocation(uuid)
55-
.then(allocation => {
56-
mutate(data?.concat(allocation), false);
57-
setAddingAllocation(false);
58-
})
59-
.catch(error => {
60-
clearAndAddHttpError({ key: 'server:network', error });
61-
mutate(initial, false);
62-
setAddingAllocation(false);
63-
});
52+
setLoading(true);
53+
createServerAllocation(uuid)
54+
.then(allocation => mutate(data?.concat(allocation), false))
55+
.catch(error => clearAndAddHttpError({ key: 'server:network', error }))
56+
.then(() => setLoading(false));
6457
};
6558

6659
const onNotesAdded = useCallback((id: number, notes: string) => {
@@ -72,37 +65,32 @@ const NetworkContainer = () => {
7265
{!data ?
7366
<Spinner size={'large'} centered/>
7467
:
75-
data.map(allocation => (
76-
<AllocationRow
77-
key={`${allocation.ip}:${allocation.port}`}
78-
allocation={allocation}
79-
onSetPrimary={setPrimaryAllocation}
80-
onNotesChanged={onNotesAdded}
81-
/>
82-
))
83-
}
84-
{allocationLimit > data!.length ?
85-
<GreyRowBox
86-
$hoverable={false}
87-
css={tw`mt-2 overflow-x-auto flex items-center justify-center`}
88-
>
89-
{addingAllocation ?
90-
<Spinner size={'base'} centered/>
91-
:
92-
<Button
93-
color={'primary'}
94-
isSecondary
95-
onClick={() => getNewAllocation()}
96-
css={tw`my-2`}
97-
>
98-
Add New Allocation
99-
</Button>
68+
<>
69+
{
70+
data.map(allocation => (
71+
<AllocationRow
72+
key={`${allocation.ip}:${allocation.port}`}
73+
allocation={allocation}
74+
onSetPrimary={setPrimaryAllocation}
75+
onNotesChanged={onNotesAdded}
76+
/>
77+
))
10078
}
101-
</GreyRowBox>
102-
:
103-
<p css={tw`mt-2 text-center text-sm text-neutral-400`}>
104-
You have reached the max number of allocations allowed for your server.
105-
</p>
79+
<Can action={'allocation.create'}>
80+
<SpinnerOverlay visible={loading}/>
81+
<div css={tw`mt-6 sm:flex items-center justify-end`}>
82+
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
83+
You are currently using {data.length} of {allocationLimit} allowed allocations for this
84+
server.
85+
</p>
86+
{allocationLimit > data.length &&
87+
<Button css={tw`w-full sm:w-auto`} color={'primary'} onClick={onCreateAllocation}>
88+
Create Allocation
89+
</Button>
90+
}
91+
</div>
92+
</Can>
93+
</>
10694
}
10795
</ServerContentBlock>
10896
);

0 commit comments

Comments
 (0)