diff --git a/app/Http/Controllers/Admin/AccountsController.php b/app/Http/Controllers/Admin/AccountsController.php index aa933baab..813162a78 100644 --- a/app/Http/Controllers/Admin/AccountsController.php +++ b/app/Http/Controllers/Admin/AccountsController.php @@ -47,13 +47,17 @@ class AccountsController extends Controller ]); } - public function getDelete(Request $request, $id) + public function deleteView(Request $request, $id) { - $user = new UserRepository; - $user->delete($id); - - Alert::success('An account has been successfully deleted.')->flash(); - return redirect()->route('admin.accounts'); + try { + User::findOrFail($id)->delete(); + return response(null, 204); + } catch(\Exception $ex) { + Log::error($ex); + return response()->json([ + 'error' => 'An error occured while attempting to delete this user.' + ], 500); + } } public function postNew(Request $request) diff --git a/app/Http/Routes/AdminRoutes.php b/app/Http/Routes/AdminRoutes.php index b65d665ab..e02ca4ebe 100644 --- a/app/Http/Routes/AdminRoutes.php +++ b/app/Http/Routes/AdminRoutes.php @@ -55,8 +55,8 @@ class AdminRoutes { ]); // Delete an Account Matching an ID - $router->get('/delete/{id}', [ - 'uses' => 'Admin\AccountsController@getDelete' + $router->delete('/view/{id}', [ + 'uses' => 'Admin\AccountsController@deleteView' ]); }); diff --git a/resources/views/admin/accounts/view.blade.php b/resources/views/admin/accounts/view.blade.php index 65e390f1f..849760352 100644 --- a/resources/views/admin/accounts/view.blade.php +++ b/resources/views/admin/accounts/view.blade.php @@ -61,8 +61,8 @@ {!! csrf_field() !!} - - + + @@ -150,6 +150,22 @@ $(document).ready(function(){ }); return false; }); + $('button[data-action="deleteUser"]').click(function (event) { + event.preventDefault(); + $.ajax({ + method: 'DELETE', + url: '/admin/accounts/view/{{ $user->id }}', + headers: { + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }).done(function (data) { + alert('Account was successfully deleted from the system.'); + window.location = '/admin/accounts'; + }).fail(function (jqXHR) { + console.error(jqXHR); + alert('An error occured: ' + jqXHR.JSONResponse.error); + }) + }) }); @endsection