fixes messages for integration test, removes slack request file, rehouses ajax method

This commit is contained in:
Godfrey M 2023-02-01 16:51:05 -08:00
parent f95a904d90
commit 3a86547ef5
4 changed files with 56 additions and 89 deletions

View file

@ -143,37 +143,6 @@ class SettingsController extends Controller
} }
public function slacktest($slack_endpoint,$slack_channel,$slack_botname)
{
// If validation passes, continue to the curl request
$slack = new Client([
'base_url' => e($slack_endpoint),
'defaults' => [
'exceptions' => false,
],
]);
$payload = json_encode(
[
'channel' => e($slack_channel),
'text' => trans('general.slack_test_msg'),
'username' => e($slack_botname),
'icon_emoji' => ':heart:',
]);
try {
$slack->post($slack_endpoint, ['body' => $payload]);
return response()->json(['message' => 'Success'], 200);
} catch (\Exception $e) {
return response()->json(['message' => 'Please check the channel name and webhook endpoint URL ('.e($slack_endpoint).'). Slack responded with: '.$e->getMessage()], 400);
}
//}
return response()->json(['message' => 'Something went wrong :( '], 400);
}
/** /**
* Test the email configuration * Test the email configuration
* *

View file

@ -2,9 +2,9 @@
namespace App\Http\Livewire; namespace App\Http\Livewire;
use GuzzleHttp\Client;
use Livewire\Component; use Livewire\Component;
use App\Models\Setting; use App\Models\Setting;
use App\Http\Controllers\Api\SettingsController;
class SlackSettingsForm extends Component class SlackSettingsForm extends Component
{ {
@ -28,8 +28,35 @@ class SlackSettingsForm extends Component
return view('livewire.slack-settings-form'); return view('livewire.slack-settings-form');
} }
public function testSlack($slack_endpoint, $slack_channel, $slack_botname){ public function testSlack(){
SettingsController::testSlack($slack_endpoint,$slack_channel,$slack_botname);
// If validation passes, continue to the curl request
$slack = new Client([
'base_url' => e($this->slack_endpoint),
'defaults' => [
'exceptions' => false,
],
]);
$payload = json_encode(
[
'channel' => e($this->slack_channel),
'text' => trans('general.slack_test_msg'),
'username' => e($this->slack_botname),
'icon_emoji' => ':heart:',
]);
try {
$slack->post($this->slack_endpoint, ['body' => $payload]);
return session()->flash('success' , 'Your Slack Integration works!');
} catch (\Exception $e) {
return session()->flash('error' , 'Please check the channel name and webhook endpoint URL ('.e($this->slack_endpoint).'). Slack responded with: '.$e->getMessage());
}
//}
return session()->flash('message' , 'Something went wrong :( ');
} }
@ -47,7 +74,7 @@ class SlackSettingsForm extends Component
$this->setting->save(); $this->setting->save();
session()->flash('message',trans('admin/settings/message.update.success')); session()->flash('save',trans('admin/settings/message.update.success'));
} }

View file

@ -1,32 +0,0 @@
<?php
namespace App\Http\Requests;
class SlackSettingsRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:"https://hooks.slack.com"|nullable',
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
'slack_botname' => 'string|nullable',
];
}
}

View file

@ -1,11 +1,26 @@
<div class="col-md-12" style="border-top: 0px;"> <div class="col-md-12" style="border-top: 0px;">
@if (session()->has('message')) @if (session()->has('save'))
<div class="alert alert-success"> <div class="alert alert-success">
{{session('message')}} {{session('save')}}
</div> </div>
@endif @endif
@if(session()->has('success'))
<div class="alert alert-success">
{{session('success')}}
</div>
@endif
@if(session()->has('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
@endif
@if(session()->has('message'))
<div class="alert alert-danger">
{{session('message')}}
</div>
@endif
<form wire:submit.prevent ="submit"> <form wire:submit.prevent ="submit">
{{csrf_field()}} {{csrf_field()}}
@ -34,11 +49,11 @@
</div> </div>
<div class="col-md-10"> <div class="col-md-10">
@if (config('app.lock_passwords')===true) @if (config('app.lock_passwords')===true)
<input type="text" wire:model.lazy="slack_channel" id="slack_channel" class= 'form-control' placeholder="'#IT-Ops'" {{old('slack_channel', $slack_channel)}} ><br> <input type="text" wire:model.lazy="slack_channel" id="slack_channel" class='form-control' placeholder="#IT-Ops" value="{{old('slack_channel', $slack_channel)}}" ><br>
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p> <p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
@else @else
<input type="text" wire:model.lazy="slack_channel" id="slack_channel" class= 'form-control' placeholder="'#IT-Ops'" {{old('slack_channel', $slack_channel)}} ><br> <input type="text" wire:model.lazy="slack_channel" id="slack_channel" class= 'form-control' placeholder="#IT-Ops" value="{{old('slack_channel', $slack_channel)}}" ><br>
@endif @endif
{!! $errors->first('slack_channel', '<span class="alert-msg" aria-hidden="true">:message</span>') !!} {!! $errors->first('slack_channel', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</div> </div>
@ -64,33 +79,21 @@
<!--Slack Integration Test--> <!--Slack Integration Test-->
@if($slack_endpoint != null && $slack_channel != null && $slack_botname != null) @if($slack_endpoint != null && $slack_channel != null && $slack_botname != null)
<div class="form-group"> <div class="form-group">
<div class="col-md-2"> <div class="col-md-2">
{{ Form::label('test_slack', 'Test Slack') }} {{ Form::label('test_slack', 'Test Slack') }}
</div> </div>
<div class="col-md-10"> <div class="col-md-10">
<button wire:submit.prevent="testSlack({{$slack_channel,$slack_endpoint,$slack_botname}})" id="test_slack" class="btn btn-default btn-sm pull-left"><span>{!! trans('admin/settings/general.slack_test') !!}</span></button> <a href="#" wire:click.prevent="testSlack" id="test_slack" class="btn btn-default btn-sm pull-left"><span>{!! trans('admin/settings/general.slack_test') !!}</span></a>
<div wire:loading><i class="fas fa-spinner spin"></i></div> <div wire:loading><i class="fas fa-spinner spin"></i></div>
@if($response == 'success')
message here
@endif
@if($response == 'error')
message here
@endif
</div> </div>
</div> </div>
@endif @endif
{{-- <div class="col-md-10 col-md-offset-2">--}}
{{-- <span id="slacktesticon"></span>--}}
{{-- <span id="slacktestresult"></span>--}}
{{-- <span id="slackteststatus"></span>--}}
{{-- </div>--}}
<div class="box-footer"> <div class="box-footer" style="margin-top: 45px;">
<div class="text-left col-md-6">
<div class="text-right col-md-12">
<a class="btn btn-link text-left" href="{{ route('settings.index') }}">{{ trans('button.cancel') }}</a> <a class="btn btn-link text-left" href="{{ route('settings.index') }}">{{ trans('button.cancel') }}</a>
</div>
<div class="text-right col-md-6">
<button type="submit" id="save_slack" class="btn btn-primary"><i class="fas fa-check icon-white" aria-hidden="true"></i> {{ trans('general.save') }}</button> <button type="submit" id="save_slack" class="btn btn-primary"><i class="fas fa-check icon-white" aria-hidden="true"></i> {{ trans('general.save') }}</button>
</div> </div>
</div><!--box-footer--> </div><!--box-footer-->