diff --git a/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php b/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php index 532054993..b26107649 100644 --- a/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php +++ b/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php @@ -138,18 +138,19 @@ class NetworkAllocationController extends ClientApiController * @return \Illuminate\Http\JsonResponse * * @throws \Pterodactyl\Exceptions\DisplayException - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation) { if ($allocation->id === $server->allocation_id) { throw new DisplayException( - 'Cannot delete the primary allocation for a server.' + 'You cannot delete the primary allocation for this server.' ); } - $this->repository->update($allocation->id, ['server_id' => null, 'notes' => null]); + Allocation::query()->where('id', $allocation->id)->update([ + 'notes' => null, + 'server_id' => null, + ]); return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT); } diff --git a/app/Services/Allocations/FindAssignableAllocationService.php b/app/Services/Allocations/FindAssignableAllocationService.php index c529547d6..ec816cb81 100644 --- a/app/Services/Allocations/FindAssignableAllocationService.php +++ b/app/Services/Allocations/FindAssignableAllocationService.php @@ -108,7 +108,6 @@ class FindAssignableAllocationService throw new NoAutoAllocationSpaceAvailableException; } - // dd($available, array_rand($available)); // Pick a random port out of the remaining available ports. /** @var int $port */ $port = $available[array_rand($available)]; diff --git a/resources/scripts/api/server/network/getServerAllocations.ts b/resources/scripts/api/server/network/getServerAllocations.ts deleted file mode 100644 index 7309bd266..000000000 --- a/resources/scripts/api/server/network/getServerAllocations.ts +++ /dev/null @@ -1,9 +0,0 @@ -import http from '@/api/http'; -import { rawDataToServerAllocation } from '@/api/transformers'; -import { Allocation } from '@/api/server/getServer'; - -export default async (uuid: string): Promise => { - const { data } = await http.get(`/api/client/servers/${uuid}/network/allocations`); - - return (data.data || []).map(rawDataToServerAllocation); -}; diff --git a/resources/scripts/api/swr/getServerAllocations.ts b/resources/scripts/api/swr/getServerAllocations.ts new file mode 100644 index 000000000..00af65c87 --- /dev/null +++ b/resources/scripts/api/swr/getServerAllocations.ts @@ -0,0 +1,15 @@ +import { ServerContext } from '@/state/server'; +import useSWR from 'swr'; +import http from '@/api/http'; +import { rawDataToServerAllocation } from '@/api/transformers'; +import { Allocation } from '@/api/server/getServer'; + +export default (initialData?: Allocation[]) => { + const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); + + return useSWR([ 'server:allocations', uuid ], async () => { + const { data } = await http.get(`/api/client/servers/${uuid}/network/allocations`); + + return (data.data || []).map(rawDataToServerAllocation); + }, { initialData, revalidateOnFocus: false }); +}; diff --git a/resources/scripts/components/elements/ConfirmationModal.tsx b/resources/scripts/components/elements/ConfirmationModal.tsx index 1b5b3ce1e..7233731f5 100644 --- a/resources/scripts/components/elements/ConfirmationModal.tsx +++ b/resources/scripts/components/elements/ConfirmationModal.tsx @@ -20,7 +20,7 @@ const ConfirmationModal = ({ title, children, buttonText, onConfirmed }: Props)

{title}

{children}

- - + <> + + + + + + + }
diff --git a/resources/scripts/components/server/network/DeleteAllocationButton.tsx b/resources/scripts/components/server/network/DeleteAllocationButton.tsx new file mode 100644 index 000000000..e52ba3916 --- /dev/null +++ b/resources/scripts/components/server/network/DeleteAllocationButton.tsx @@ -0,0 +1,51 @@ +import React, { useState } from 'react'; +import { faTrashAlt } from '@fortawesome/free-solid-svg-icons'; +import tw from 'twin.macro'; +import Icon from '@/components/elements/Icon'; +import ConfirmationModal from '@/components/elements/ConfirmationModal'; +import { ServerContext } from '@/state/server'; +import deleteServerAllocation from '@/api/server/network/deleteServerAllocation'; +import getServerAllocations from '@/api/swr/getServerAllocations'; +import useFlash from '@/plugins/useFlash'; + +interface Props { + allocation: number; +} + +const DeleteAllocationButton = ({ allocation }: Props) => { + const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); + const [ confirm, setConfirm ] = useState(false); + const { mutate } = getServerAllocations(); + const { clearFlashes, clearAndAddHttpError } = useFlash(); + + const deleteAllocation = () => { + clearFlashes('server:network'); + + mutate(data => data?.filter(a => a.id !== allocation), false); + deleteServerAllocation(uuid, allocation) + .catch(error => clearAndAddHttpError({ key: 'server:network', error })); + }; + + return ( + <> + setConfirm(false)} + > + This allocation will be immediately removed from your server. Are you sure you want to continue? + + + + ); +}; + +export default DeleteAllocationButton; diff --git a/resources/scripts/components/server/network/NetworkContainer.tsx b/resources/scripts/components/server/network/NetworkContainer.tsx index cd789657d..39855fcf3 100644 --- a/resources/scripts/components/server/network/NetworkContainer.tsx +++ b/resources/scripts/components/server/network/NetworkContainer.tsx @@ -1,7 +1,4 @@ import React, { useCallback, useEffect, useState } from 'react'; -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'; import ServerContentBlock from '@/components/elements/ServerContentBlock'; @@ -14,6 +11,7 @@ import createServerAllocation from '@/api/server/network/createServerAllocation' import tw from 'twin.macro'; import Can from '@/components/elements/Can'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; +import getServerAllocations from '@/api/swr/getServerAllocations'; const NetworkContainer = () => { const [ loading, setLoading ] = useState(false); @@ -22,10 +20,7 @@ const NetworkContainer = () => { const allocations = useDeepMemoize(ServerContext.useStoreState(state => state.server.data!.allocations)); const { clearFlashes, clearAndAddHttpError } = useFlash(); - const { data, error, mutate } = useSWR(uuid, key => getServerAllocations(key), { - initialData: allocations, - revalidateOnFocus: false, - }); + const { data, error, mutate } = getServerAllocations(allocations); useEffect(() => { if (error) {