From 81bd67cc7678785476f1c9dad547ebbe24d2d679 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 9 Dec 2019 22:03:10 -0800 Subject: [PATCH] Add ability for user to change server's name --- .../Api/Client/Servers/SettingsController.php | 48 ++++++++++++++ .../Servers/Settings/RenameServerRequest.php | 34 ++++++++++ resources/scripts/api/server/renameServer.ts | 9 +++ .../server/settings/RenameServerBox.tsx | 66 +++++++++++++++++++ .../server/settings/SettingsContainer.tsx | 8 ++- resources/scripts/routers/ServerRouter.tsx | 2 +- routes/api-client.php | 4 ++ 7 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/Api/Client/Servers/SettingsController.php create mode 100644 app/Http/Requests/Api/Client/Servers/Settings/RenameServerRequest.php create mode 100644 resources/scripts/api/server/renameServer.ts create mode 100644 resources/scripts/components/server/settings/RenameServerBox.tsx diff --git a/app/Http/Controllers/Api/Client/Servers/SettingsController.php b/app/Http/Controllers/Api/Client/Servers/SettingsController.php new file mode 100644 index 000000000..5b457fa9c --- /dev/null +++ b/app/Http/Controllers/Api/Client/Servers/SettingsController.php @@ -0,0 +1,48 @@ +repository = $repository; + } + + /** + * Renames a server. + * + * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest $request + * @param \Pterodactyl\Models\Server $server + * @return \Illuminate\Http\Response + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function rename(RenameServerRequest $request, Server $server) + { + $this->repository->update($server->id, [ + 'name' => $request->input('name'), + ]); + + return Response::create('', Response::HTTP_NO_CONTENT); + } +} diff --git a/app/Http/Requests/Api/Client/Servers/Settings/RenameServerRequest.php b/app/Http/Requests/Api/Client/Servers/Settings/RenameServerRequest.php new file mode 100644 index 000000000..fff2661f7 --- /dev/null +++ b/app/Http/Requests/Api/Client/Servers/Settings/RenameServerRequest.php @@ -0,0 +1,34 @@ + Server::getRules()['name'], + ]; + } +} diff --git a/resources/scripts/api/server/renameServer.ts b/resources/scripts/api/server/renameServer.ts new file mode 100644 index 000000000..a4a95141e --- /dev/null +++ b/resources/scripts/api/server/renameServer.ts @@ -0,0 +1,9 @@ +import http from '@/api/http'; + +export default (uuid: string, name: string): Promise => { + return new Promise((resolve, reject) => { + http.post(`/api/client/servers/${uuid}/settings/rename`, { name }) + .then(() => resolve()) + .catch(reject); + }); +}; diff --git a/resources/scripts/components/server/settings/RenameServerBox.tsx b/resources/scripts/components/server/settings/RenameServerBox.tsx new file mode 100644 index 000000000..5b1ed3181 --- /dev/null +++ b/resources/scripts/components/server/settings/RenameServerBox.tsx @@ -0,0 +1,66 @@ +import React from 'react'; +import { ServerContext } from '@/state/server'; +import TitledGreyBox from '@/components/elements/TitledGreyBox'; +import { Form, FormikProps, withFormik } from 'formik'; +import { Server } from '@/api/server/getServer'; +import { ActionCreator } from 'easy-peasy'; +import renameServer from '@/api/server/renameServer'; +import Field from '@/components/elements/Field'; +import { object, string } from 'yup'; +import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; + +interface OwnProps { + server: Server; + setServer: ActionCreator; +} + +interface Values { + name: string; +} + +const RenameServerBox = ({ isSubmitting, ...props }: OwnProps & FormikProps) => ( + + +
+ +
+ +
+ +
+); + +const EnhancedForm = withFormik({ + displayName: 'RenameServerBoxForm', + + mapPropsToValues: props => ({ + name: props.server.name, + }), + + validationSchema: () => object().shape({ + name: string().required().min(1), + }), + + handleSubmit: (values, { props, setSubmitting }) => { + renameServer(props.server.uuid, values.name) + .then(() => props.setServer({ ...props.server, name: values.name })) + .catch(error => { + console.error(error); + }) + .then(() => setSubmitting(false)); + }, +})(RenameServerBox); + +export default () => { + const server = ServerContext.useStoreState(state => state.server.data!); + const setServer = ServerContext.useStoreActions(actions => actions.server.setServer); + + return ; +}; diff --git a/resources/scripts/components/server/settings/SettingsContainer.tsx b/resources/scripts/components/server/settings/SettingsContainer.tsx index 65231ebee..0f0b31e81 100644 --- a/resources/scripts/components/server/settings/SettingsContainer.tsx +++ b/resources/scripts/components/server/settings/SettingsContainer.tsx @@ -4,14 +4,15 @@ import { ServerContext } from '@/state/server'; import { useStoreState } from 'easy-peasy'; import { ApplicationStore } from '@/state'; import { UserData } from '@/state/user'; +import RenameServerBox from '@/components/server/settings/RenameServerBox'; export default () => { const user = useStoreState(state => state.user.data!); const server = ServerContext.useStoreState(state => state.server.data!); return ( -
- +
+
{
+
+ +
); }; diff --git a/resources/scripts/routers/ServerRouter.tsx b/resources/scripts/routers/ServerRouter.tsx index 70ebf772d..d10255323 100644 --- a/resources/scripts/routers/ServerRouter.tsx +++ b/resources/scripts/routers/ServerRouter.tsx @@ -47,7 +47,7 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) -
+
{!server ?
diff --git a/routes/api-client.php b/routes/api-client.php index 5e54f8cf4..37f699ff1 100644 --- a/routes/api-client.php +++ b/routes/api-client.php @@ -63,4 +63,8 @@ Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateServ Route::group(['prefix' => '/users'], function () { Route::get('/', 'Servers\SubuserController@index'); }); + + Route::group(['prefix' => '/settings'], function () { + Route::post('/rename', 'Servers\SettingsController@rename'); + }); });