Skip to content

Commit

Permalink
switcher auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Dec 9, 2022
1 parent 6715289 commit ee4f40f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
22 changes: 20 additions & 2 deletions resources/views/fields/switch.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
}
</style>

@php
$uniqueId = uniqid();
@endphp

<div x-data='{checked : {{ $field->getOnValue() == $field->formViewValue($item) ? 'true' : 'false'}}}' class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
<input type="hidden"
name="{{ $field->name() }}"
:value="checked ? '{{ $field->getOnValue() }}' : '{{ $field->getOffValue() }}'"
value="{{ $field->getOnValue() == $field->formViewValue($item) ? '1' : '0'}}">

<input @change='checked=!checked'
<input @change='checked=!checked;change_switcher_{{ $field->id($uniqueId) }}($event.target.checked)'
{{ $field->isDisabled() ? "disabled" : "" }}
{{ $field->getOnValue() == $field->formViewValue($item) ? 'checked' : ''}}
:value="checked ? '{{ $field->getOnValue() }}' : '{{ $field->getOffValue() }}'"
Expand All @@ -30,4 +34,18 @@ class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 app
<label for="{{ $field->id() }}" class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-300 cursor-pointer">

</label>
</div>

<script>
async function change_switcher_{{ $field->id($uniqueId) }}(checked)
{
@if($autoUpdate ?? false)
await fetch('{{ route(config('moonshine.route.prefix') . '.auto-update') }}?' + new URLSearchParams({
value: checked,
key: '{{ $item->getKey() }}',
model: '{{ str_replace('\\', '\\\\', get_class($item)) }}',
field: '{{ $field->field() }}'
}))
@endif
}
</script>
</div>
13 changes: 12 additions & 1 deletion src/Fields/SwitchBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Leeto\MoonShine\Fields;

use Illuminate\Database\Eloquent\Model;
use Leeto\MoonShine\Helpers\Condition;
use Leeto\MoonShine\Traits\Fields\BooleanTrait;

class SwitchBoolean extends Field
Expand All @@ -13,10 +14,20 @@ class SwitchBoolean extends Field

protected static string $view = 'moonshine::fields.switch';

protected bool $autoUpdate = true;

public function autoUpdate(mixed $condition = null): static
{
$this->autoUpdate = Condition::boolean($condition, true);

return $this;
}

public function indexViewValue(Model $item, bool $container = true): mixed
{
return view('moonshine::fields.switch', [
'field' => $this->disabled(),
'field' => $this,
'autoUpdate' => $this->autoUpdate,
'item' => $item
]);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Http/Controllers/MoonShineDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ public function attachments(Request $request): array

return [];
}

public function autoUpdate(Request $request): array
{
$class = $request->get('model');
$model = new $class();
$item = $model->findOrFail($request->get('key'));

$item->update([
$request->get('field') => $request->boolean('value')
]);

return $item->toArray();
}
}
1 change: 1 addition & 0 deletions src/MoonShine.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected function addRoutes(): void
->name(config('moonshine.route.prefix').'.')->group(function () {
Route::get('/', [MoonShineDashboardController::class, 'index'])->name('index');
Route::post('/attachments', [MoonShineDashboardController::class, 'attachments'])->name('attachments');
Route::get('/auto-update', [MoonShineDashboardController::class, 'autoUpdate'])->name('auto-update');

Route::get('/login', [MoonShineAuthController::class, 'login'])->name('login');
Route::post('/authenticate', [MoonShineAuthController::class, 'authenticate'])->name('authenticate');
Expand Down

0 comments on commit ee4f40f

Please sign in to comment.