From 6df90a12d8c5c27c2b41ac5f3d145ef15eec8367 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 3 Oct 2021 16:06:48 -0600 Subject: [PATCH] ui(admin): add delete egg variable button --- .../Eggs/EggVariableController.php | 16 ++++++++ .../api/admin/eggs/deleteEggVariable.ts | 9 +++++ .../scripts/components/admin/AdminBox.tsx | 6 ++- .../nests/eggs/EggVariablesContainer.tsx | 38 +++++++++++++++++-- routes/api-application.php | 1 + 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 resources/scripts/api/admin/eggs/deleteEggVariable.ts diff --git a/app/Http/Controllers/Api/Application/Eggs/EggVariableController.php b/app/Http/Controllers/Api/Application/Eggs/EggVariableController.php index 7ffb86399..682c4359c 100644 --- a/app/Http/Controllers/Api/Application/Eggs/EggVariableController.php +++ b/app/Http/Controllers/Api/Application/Eggs/EggVariableController.php @@ -3,6 +3,9 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Eggs; use Pterodactyl\Models\Egg; +use Illuminate\Http\Request; +use Illuminate\Http\Response; +use Pterodactyl\Models\EggVariable; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Services\Eggs\Variables\VariableUpdateService; use Pterodactyl\Services\Eggs\Variables\VariableCreationService; @@ -62,4 +65,17 @@ class EggVariableController extends ApplicationApiController ->transformWith(EggVariableTransformer::class) ->toArray(); } + + /** + * Deletes a single egg variable. + */ + public function delete(Request $request, Egg $egg, EggVariable $eggVariable): Response + { + EggVariable::query() + ->where('id', $eggVariable->id) + ->where('egg_id', $egg->id) + ->delete(); + + return $this->returnNoContent(); + } } diff --git a/resources/scripts/api/admin/eggs/deleteEggVariable.ts b/resources/scripts/api/admin/eggs/deleteEggVariable.ts new file mode 100644 index 000000000..967798f55 --- /dev/null +++ b/resources/scripts/api/admin/eggs/deleteEggVariable.ts @@ -0,0 +1,9 @@ +import http from '@/api/http'; + +export default (eggId: number, variableId: number): Promise => { + return new Promise((resolve, reject) => { + http.delete(`/api/application/eggs/${eggId}/variables/${variableId}`) + .then(() => resolve()) + .catch(reject); + }); +}; diff --git a/resources/scripts/components/admin/AdminBox.tsx b/resources/scripts/components/admin/AdminBox.tsx index 3c77e8180..d7e570690 100644 --- a/resources/scripts/components/admin/AdminBox.tsx +++ b/resources/scripts/components/admin/AdminBox.tsx @@ -10,16 +10,17 @@ interface Props { className?: string; padding?: boolean; children: React.ReactNode; + button?: React.ReactNode; } -const AdminBox = ({ icon, title, className, padding, children }: Props) => { +const AdminBox = ({ icon, title, className, padding, children, button }: Props) => { if (padding === undefined) { padding = true; } return (
-
+
{typeof title === 'string' ?

{icon && }{title} @@ -27,6 +28,7 @@ const AdminBox = ({ icon, title, className, padding, children }: Props) => { : title } + {button}

{children} diff --git a/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx b/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx index 17ba749e4..e9905eb44 100644 --- a/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx +++ b/resources/scripts/components/admin/nests/eggs/EggVariablesContainer.tsx @@ -1,3 +1,4 @@ +import deleteEggVariable from '@/api/admin/eggs/deleteEggVariable'; import { Form, Formik, FormikHelpers, useFormikContext } from 'formik'; import React from 'react'; import tw from 'twin.macro'; @@ -83,11 +84,26 @@ export function EggVariableForm ({ prefix }: { prefix: string }) { ); } -function EggVariableBox ({ variable, prefix }: { variable: EggVariable, prefix: string }) { +function EggVariableBox ({ onDeleteClick, variable, prefix }: { onDeleteClick: () => void, variable: EggVariable, prefix: string }) { const { isSubmitting } = useFormikContext(); return ( - {variable.name}

}> + {variable.name}

} + button={ + + } + > @@ -117,7 +133,23 @@ export default function EggVariablesContainer ({ egg }: { egg: Egg }) {
- {egg.relations?.variables?.map((v, i) => )} + {egg.relations?.variables?.map((v, i) => ( + { + deleteEggVariable(egg.id, v.id) + .then(async () => await mutate(egg => ({ + ...egg!, + relations: { + variables: egg!.relations.variables!.filter(v2 => v.id === v2.id), + }, + }))) + .catch(error => clearAndAddHttpError({ key: 'egg', error })); + }} + /> + ))}
diff --git a/routes/api-application.php b/routes/api-application.php index 67c3a5c50..b58eddd8e 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -41,6 +41,7 @@ Route::group(['prefix' => '/eggs'], function () { Route::patch('/{egg}/variables', [\Pterodactyl\Http\Controllers\Api\Application\Eggs\EggVariableController::class, 'update']); Route::delete('/{egg}', [\Pterodactyl\Http\Controllers\Api\Application\Eggs\EggController::class, 'delete']); + Route::delete('/{egg}/variables/{eggVariable}', [\Pterodactyl\Http\Controllers\Api\Application\Eggs\EggVariableController::class, 'delete']); }); /*