Skip to content

Commit c5608b1

Browse files
committed
rework UI of mail settings page to allow for saving settings before testing
1 parent 561b3dd commit c5608b1

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

app/Http/Controllers/Admin/Settings/MailController.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Exception;
66
use Illuminate\View\View;
77
use Illuminate\Http\Request;
8-
use Illuminate\Http\RedirectResponse;
8+
use Illuminate\Http\Response;
99
use Prologue\Alerts\AlertsMessageBag;
1010
use Illuminate\Contracts\Console\Kernel;
1111
use Pterodactyl\Notifications\MailTested;
@@ -85,13 +85,13 @@ public function index(): View
8585
* Handle request to update SMTP mail settings.
8686
*
8787
* @param \Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest $request
88-
* @return \Illuminate\Http\RedirectResponse
88+
* @return \Illuminate\Http\Response
8989
*
9090
* @throws DisplayException
9191
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
9292
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
9393
*/
94-
public function update(MailSettingsFormRequest $request): RedirectResponse
94+
public function update(MailSettingsFormRequest $request): Response
9595
{
9696
if ($this->config->get('mail.driver') !== 'smtp') {
9797
throw new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.');
@@ -111,30 +111,25 @@ public function update(MailSettingsFormRequest $request): RedirectResponse
111111
}
112112

113113
$this->kernel->call('queue:restart');
114-
$this->alert->success('Mail settings have been updated successfully and the queue worker was restarted to apply these changes.')->flash();
115114

116-
return redirect()->route('admin.settings.mail');
115+
return response('', 204);
117116
}
118117

119118
/**
120119
* Submit a request to send a test mail message.
121120
*
122121
* @param Request $request
123-
* @return \Illuminate\Http\RedirectResponse
122+
* @return \Illuminate\Http\Response
124123
*/
125-
public function test(Request $request): RedirectResponse
124+
public function test(Request $request): Response
126125
{
127126
try {
128127
Notification::route('mail', $request->user()->email)
129128
->notify(new MailTested($request->user()));
130129
} catch (Exception $exception) {
131-
$this->alert->danger(trans('base.mail.test_failed') . ' ' . $exception->getMessage())->flash();
132-
133-
return redirect()->route('admin.settings.mail');
130+
return response($exception->getMessage(), 500);
134131
}
135132

136-
$this->alert->success(trans('base.mail.test_succeeded'))->flash();
137-
138-
return redirect()->route('admin.settings.mail');
133+
return response('', 204);
139134
}
140135
}

resources/themes/pterodactyl/admin/settings/mail.blade.php

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</div>
3333
</div>
3434
@else
35-
<form action="{{ route('admin.settings.mail') }}" method="POST">
35+
<form>
3636
<div class="box-body">
3737
<div class="row">
3838
<div class="form-group col-md-6">
@@ -99,8 +99,8 @@
9999
<div class="box-footer">
100100
{{ csrf_field() }}
101101
<div class="pull-right">
102-
<a href="{{ route('admin.settings.mail.test') }}" class="btn btn-sm btn-success">Test</a>
103-
<button type="submit" name="_method" value="PATCH" class="btn btn-sm btn-primary">Save</button>
102+
<button type="button" id="testButton" class="btn btn-sm btn-success">Test</button>
103+
<button type="button" id="saveButton" class="btn btn-sm btn-primary">Save</button>
104104
</div>
105105
</div>
106106
</form>
@@ -109,3 +109,82 @@
109109
</div>
110110
</div>
111111
@endsection
112+
113+
@section('footer-scripts')
114+
{!! Theme::js('js/laroute.js?t={cache-version}') !!}
115+
{!! Theme::js('vendor/jquery/jquery.min.js?t={cache-version}') !!}
116+
{!! Theme::js('vendor/sweetalert/sweetalert.min.js?t={cache-version}') !!}
117+
118+
<script>
119+
function saveSettings() {
120+
return $.ajax({
121+
method: 'PATCH',
122+
url: Router.route('admin.settings.mail'),
123+
contentType: 'application/json',
124+
data: JSON.stringify({
125+
'mail:host': $('input[name="mail:host"]').val(),
126+
'mail:port': $('input[name="mail:port"]').val(),
127+
'mail:encryption': $('select[name="mail:encryption"]').val(),
128+
'mail:username': $('input[name="mail:username"]').val(),
129+
'mail:password': $('input[name="mail:password"]').val(),
130+
'mail:from:address': $('input[name="mail:from:address"]').val(),
131+
'mail:from:name': $('input[name="mail:from:name"]').val()
132+
}),
133+
headers: { 'X-CSRF-Token': $('input[name="_token"]').val() }
134+
}).fail(function (jqXHR) {
135+
showErrorDialog(jqXHR, 'save');
136+
});
137+
}
138+
139+
function testSettings() {
140+
return $.ajax({
141+
method: 'GET',
142+
url: Router.route('admin.settings.mail.test'),
143+
headers: { 'X-CSRF-Token': $('input[name="_token"]').val() }
144+
}).fail(function (jqXHR) {
145+
showErrorDialog(jqXHR, 'test');
146+
}).done(function () {
147+
swal({
148+
title: 'Success',
149+
text: 'The test message was sent successfully.',
150+
type: 'success'
151+
});
152+
});
153+
}
154+
155+
function saveAndTestSettings() {
156+
saveSettings().done(testSettings);
157+
}
158+
159+
function showErrorDialog(jqXHR, verb) {
160+
console.error(jqXHR);
161+
var errorText;
162+
if (jqXHR.responseJSON.error) {
163+
errorText = jqXHR.responseJSON.error;
164+
} else if (jqXHR.responseJSON.errors) {
165+
$.each(jqXHR.responseJSON.errors, function (i, v) {
166+
errorText += ', ' + v.detail;
167+
});
168+
}
169+
170+
swal({
171+
title: 'Whoops!',
172+
text: 'An error occurred while attempting to ' + verb + ' mail settings: ' + errorText,
173+
type: 'error'
174+
});
175+
}
176+
177+
$(document).ready(function () {
178+
$('#testButton').on('click', saveAndTestSettings);
179+
$('#saveButton').on('click', function () {
180+
saveSettings().done(function () {
181+
swal({
182+
title: 'Success',
183+
text: 'Mail settings have been updated successfully and the queue worker was restarted to apply these changes.',
184+
type: 'success'
185+
});
186+
});
187+
});
188+
});
189+
</script>
190+
@endsection

0 commit comments

Comments
 (0)