UI cleanup for add allocation button

This commit is contained in:
Dane Everitt 2020-10-31 14:10:53 -07:00
parent abb043c1cc
commit db7e4e749f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 37 additions and 49 deletions

View file

@ -10,15 +10,16 @@ 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 setPrimaryServerAllocation from '@/api/server/network/setPrimaryServerAllocation';
import Button from '@/components/elements/Button'; import Button from '@/components/elements/Button';
import newServerAllocation from '@/api/server/network/newServerAllocation'; import createServerAllocation from '@/api/server/network/createServerAllocation';
import tw from 'twin.macro'; import tw from 'twin.macro';
import GreyRowBox from '@/components/elements/GreyRowBox'; import Can from '@/components/elements/Can';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
const NetworkContainer = () => { const NetworkContainer = () => {
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)); const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations));
const [ addingAllocation, setAddingAllocation ] = useState(false);
const { clearFlashes, clearAndAddHttpError } = useFlash(); const { clearFlashes, clearAndAddHttpError } = useFlash();
const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), { const { data, error, mutate } = useSWR<Allocation[]>(uuid, key => getServerAllocations(key), {
@ -32,7 +33,7 @@ const NetworkContainer = () => {
} }
}, [ error ]); }, [ error ]);
const setPrimaryAllocation = useCallback((id: number) => { const setPrimaryAllocation = (id: number) => {
clearFlashes('server:network'); clearFlashes('server:network');
const initial = data; const initial = data;
@ -43,24 +44,16 @@ const NetworkContainer = () => {
clearAndAddHttpError({ key: 'server:network', error }); clearAndAddHttpError({ key: 'server:network', error });
mutate(initial, false); mutate(initial, false);
}); });
}, []); };
const getNewAllocation = () => { const onCreateAllocation = () => {
clearFlashes('server:network'); clearFlashes('server:network');
setAddingAllocation(true);
const initial = data; setLoading(true);
createServerAllocation(uuid)
newServerAllocation(uuid) .then(allocation => mutate(data?.concat(allocation), false))
.then(allocation => { .catch(error => clearAndAddHttpError({ key: 'server:network', error }))
mutate(data?.concat(allocation), false); .then(() => setLoading(false));
setAddingAllocation(false);
})
.catch(error => {
clearAndAddHttpError({ key: 'server:network', error });
mutate(initial, false);
setAddingAllocation(false);
});
}; };
const onNotesAdded = useCallback((id: number, notes: string) => { const onNotesAdded = useCallback((id: number, notes: string) => {
@ -72,6 +65,8 @@ const NetworkContainer = () => {
{!data ? {!data ?
<Spinner size={'large'} centered/> <Spinner size={'large'} centered/>
: :
<>
{
data.map(allocation => ( data.map(allocation => (
<AllocationRow <AllocationRow
key={`${allocation.ip}:${allocation.port}`} key={`${allocation.ip}:${allocation.port}`}
@ -81,28 +76,21 @@ const NetworkContainer = () => {
/> />
)) ))
} }
{allocationLimit > data!.length ? <Can action={'allocation.create'}>
<GreyRowBox <SpinnerOverlay visible={loading}/>
$hoverable={false} <div css={tw`mt-6 sm:flex items-center justify-end`}>
css={tw`mt-2 overflow-x-auto flex items-center justify-center`} <p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
> You are currently using {data.length} of {allocationLimit} allowed allocations for this
{addingAllocation ? server.
<Spinner size={'base'} centered/> </p>
: {allocationLimit > data.length &&
<Button <Button css={tw`w-full sm:w-auto`} color={'primary'} onClick={onCreateAllocation}>
color={'primary'} Create Allocation
isSecondary
onClick={() => getNewAllocation()}
css={tw`my-2`}
>
Add New Allocation
</Button> </Button>
} }
</GreyRowBox> </div>
: </Can>
<p css={tw`mt-2 text-center text-sm text-neutral-400`}> </>
You have reached the max number of allocations allowed for your server.
</p>
} }
</ServerContentBlock> </ServerContentBlock>
); );