Enhance the system link/register selection

This commit is contained in:
Deon George 2023-10-05 11:11:21 +11:00
parent 32c0088339
commit 495a27cfed
3 changed files with 40 additions and 115 deletions

View File

@ -826,11 +826,11 @@ class SystemController extends Controller
if ($request->isMethod('GET')) if ($request->isMethod('GET'))
return view('user.system.register'); return view('user.system.register');
if ($request->action === 'register' && $request->system_id) if ($request->action === 'register' && $request->name && is_numeric($request->name))
return view('user.system.widget.register_confirm') return view('user.system.widget.register_confirm')
->with('o',System::findOrFail($request->system_id)); ->with('o',System::findOrFail($request->name));
$o = System::findOrNew($request->system_id); $o = System::findOrNew(is_numeric($request->system_id) ? $request->system_id : NULL);
// If the system exists, and we are 'register', we'll start the address claim process // If the system exists, and we are 'register', we'll start the address claim process
if ($o->exists && $request->action === 'Link') { if ($o->exists && $request->action === 'Link') {

View File

@ -8,7 +8,7 @@ use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use App\Classes\FTN\Packet; use App\Classes\FTN\Packet;
use App\Models\System; use App\Models\{Setup,System};
// @todo rename to SystemRegisterRequest // @todo rename to SystemRegisterRequest
class SystemRegister extends FormRequest class SystemRegister extends FormRequest
@ -22,9 +22,17 @@ class SystemRegister extends FormRequest
*/ */
public function authorize(Request $request) public function authorize(Request $request)
{ {
$this->so = System::findOrNew($request->system_id); $this->so = new System;
return Gate::allows($this->so->users->count() ? 'update' : 'register',$this->so); if (is_numeric($request->name)) {
$this->so = System::findOrNew($request->name);
// Cannot claim this site
if ($this->so->id === Setup::findOrFail(config('app.id'))->system_id)
return FALSE;
}
return Gate::allows(is_numeric($request->name) && $this->so->users->count() ? 'update' : 'register',$this->so);
} }
public function messages(): array public function messages(): array

View File

@ -19,12 +19,18 @@
<div class="col-4"> <div class="col-4">
<label for="system" class="form-label">BBS Name</label> <label for="system" class="form-label">BBS Name</label>
<div class="input-group has-validation"> <div class="input-group has-validation">
<span class="input-group-text"><i class="bi bi-pc"></i></span> <span class="input-group-text"><i class="bi bi-laptop-fill"></i></span>
<input type="text" style="z-index: 0" class="form-control @error('name') is-invalid @enderror" id="name" placeholder="BBS Name" name="name" value="{{ old('name') }}" required autofocus> <select style="width: 80%;" class="form-select @error('system_id') is-invalid @enderror" id="system" name="id" required>
<span id="search-icon" style="width: 0;"><i style="border-radius: 50%;" class="spinner-border spinner-border-sm text-dark d-none"></i></span> <option value="">&nbsp;</option>
<div id="system_search_results"></div> @foreach (\App\Models\System::select(['systems.id','systems.name'])
->active()
->whereRaw('id NOT IN (SELECT system_id FROM system_user)')
->cursor() as $oo)
<option value="{{ $oo->id }}" @if(old('id')===$oo->id)selected @endif>{{ $oo->name }}</option>
@endforeach
</select>
<span class="invalid-feedback" role="alert"> <span class="invalid-feedback" role="alert">
@error('name') @error('system_id')
{{ $message }} {{ $message }}
@else @else
BBS Name is required. BBS Name is required.
@ -66,45 +72,20 @@
@endsection @endsection
@section('page-css') @section('page-css')
<style> @css('select2')
input#name + span {
left: -1.5em;
top: 0.5em;
position:relative
}
div#system_search_results ul {
color:#eeeeee;
background-color:#292929;
font-size: .85rem;
padding: 0 5px 0 5px;
z-index: 99;
top: -0.5em;
left: 31em !important;
}
div#system_search_results ul li.dropdown-header {
display: block;
color: #fff !important;
}
div#system_search_results ul li,
div#system_search_results ul li a {
display: block;
color: #aaa !important;
margin: 0 !important;
border: 0 !important;
width: inherit;
text-indent: 0 !important;
padding-left: 0 !important;
}
div#system_search_results ul li:hover {
padding-left: 0;
text-indent: 0;
}
div#system_search_results ul li:before {
content:""!important
}
</style>
@append @append
@section('page-scripts') @section('page-scripts')
@js('select2')
<script type="text/javascript">
$(document).ready(function() {
$('#system').select2({
placeholder: 'See if your BBS exists',
tags: true,
});
});
</script>
<script> <script>
var system_id; var system_id;
var noauth = new bootstrap.Modal(document.getElementById('no-auth'), {}); var noauth = new bootstrap.Modal(document.getElementById('no-auth'), {});
@ -119,7 +100,7 @@
$.ajax({ $.ajax({
url : '{{ url('user/system/register') }}', url : '{{ url('user/system/register') }}',
type : 'POST', type : 'POST',
data : { system_id: system_id,name: $('#name').val(),action: 'register',old: {!! json_encode(old()) !!} }, data : { system_id: system_id,name: $('#system').val(),action: 'register',old: {!! json_encode(old()) !!} },
dataType : 'json', dataType : 'json',
async : true, async : true,
cache : false, cache : false,
@ -136,7 +117,7 @@
@if($errors->count()) @if($errors->count())
@foreach($errors->keys() as $key) @foreach($errors->keys() as $key)
validation('{{ $key }}','{{ $errors->first($key) }}'); validation('{{ $key }}','{{ $errors->first($key) }}');
@endforeach @endforeach
@endif @endif
@ -171,78 +152,14 @@
} }
$(document).ready(function() { $(document).ready(function() {
$('input[id=name]').typeahead({
autoSelect: false,
scrollHeight: 10,
theme: 'bootstrap5',
delay: 500,
minLength: 3,
items: {{ $search_limit ?? 5 }},
fitToElement: false,
selectOnBlur: false,
appendTo: "#system_search_results",
source: function (query,process) {
systemsearch('{{ url('systems/orphan') }}',query,process);
},
matcher: function () { return true; },
// Disable sorting and just return the items (items should by the ajax method)
sorter: function(items) {
return items;
},
updater: function (item) {
system_id = item.id;
return item.name;
},
})
.on('keyup keypress', function(event) {
var key = event.keyCode || event.which;
if (key === 13) {
event.preventDefault();
return false;
}
});
$('button[name=submit]').on('click',function() { $('button[name=submit]').on('click',function() {
icon = $(this).parent().find('i'); icon = $(this).parent().find('i');
if (! $('#name').val()) if (! $('#system').val())
return; return;
getform(icon); getform(icon);
}) })
}); });
var c=0;
var systemsearch = _.debounce(function(url,query,process,icon){
icon = $('#search-icon').find('i');
$.ajax({
url : url,
type : 'GET',
data : 'term=' + query,
dataType : 'JSON',
async : true,
cache : false,
beforeSend : function() {
if (c++ === 0) {
icon.removeClass('d-none');
}
},
success : function(data) {
// if json is null, means no match, won't do again.
if(data==null || (data.length===0)) return;
process(data);
},
complete : function() {
if (--c === 0) {
icon.addClass('d-none');
}
}
})
}, 500);
</script> </script>
@append @append