A free open source IT asset/license management system
Created livewire for checkout panel
snipecc63c0f1025e
diff --git a/app/Livewire/CheckoutTargetPanel.php b/app/Livewire/CheckoutTargetPanel.phpnew file mode 100644index 0000000000..b6ffe821a2--- /dev/null+++ b/app/Livewire/CheckoutTargetPanel.php@@ -0,0 +1,129 @@+<?php++namespace App\Livewire;++use App\Models\Asset;+use App\Models\Location;+use App\Models\User;+use Illuminate\Database\Eloquent\Model;+use Illuminate\Support\Collection;+use Illuminate\Support\Facades\Gate;+use Illuminate\View\View;+use Livewire\Attributes\Locked;+use Livewire\Attributes\On;+use Livewire\Component;++/**+ * Sidebar rendered next to the single-item checkout forms (assets, licenses,+ * accessories, consumables). Shows a table of items already checked out to+ * whichever target the operator has picked — user, asset, or location —+ * so they can see what the target already has before completing the checkout.+ *+ * The parent page hosts three non-Livewire select2 widgets (user / asset /+ * location) plus a radio-group toggle from checkout-selector.blade.php that+ * controls which of those is visible. A small bridge script inside this+ * component's blade listens to all four inputs and dispatches+ * `checkout-target-selected` with the current target-type + id. This+ * component picks the event up, re-resolves the relevant relation, and+ * re-renders.+ */+class CheckoutTargetPanel extends Component+{+ private const TYPES = ['assets', 'licenses', 'accessories', 'consumables'];++ private const TARGET_TYPES = ['user', 'asset', 'location'];++ /**+ * The item type this sidebar displays — the type being checked OUT.+ * Locked so the client can't tamper: the parent checkout page pins it+ * at mount based on which flow the user is in.+ */+ #[Locked]+ public string $type;++ public ?string $targetType = null;++ public ?int $targetId = null;++ public function mount(string $type): void+ {+ if (! in_array($type, self::TYPES, true)) {+ throw new \InvalidArgumentException("Unknown checkout-target-panel type: {$type}");+ }++ $this->type = $type;+ }++ /**+ * Bridge landing spot: any change to the target selects or the radio+ * toggle in the parent form dispatches to us with the current shape.+ * Nullable values come through when the operator clears a select.+ */+ #[On('checkout-target-selected')]+ public function targetSelected(?string $targetType, ?string $targetId): void+ {+ $this->targetType = in_array($targetType, self::TARGET_TYPES, true) ? $targetType : null;+ $this->targetId = $targetId !== null && $targetId !== '' ? (int) $targetId : null;+ }++ public function render(): View+ {+ return view('livewire.checkout-target-panel', [+ 'items' => $this->items(),+ 'noun' => $this->itemNoun(),+ ]);+ }++ private function items(): Collection+ {+ if ($this->targetType === null || $this->targetId === null) {+ return collect();+ }++ $target = $this->resolveTarget();+ if (! $target || ! Gate::allows('view', $target)) {+ return collect();+ }++ // Not every (item, target) combo is a valid checkout path in the+ // schema — consumables only go to users, licenses only to users+ // or assets. Falling out to an empty collection is intentional:+ // the operator switched to a target type that this item can't+ // actually be checked out to, so there's nothing to show.+ return match ("{$this->type}:{$this->targetType}") {+ 'assets:user' => $target->assets,+ 'licenses:user' => $target->licenses,+ 'accessories:user' => $target->accessories,+ 'consumables:user' => $target->consumables,++ 'assets:asset' => $target->assignedAssets,+ 'licenses:asset' => $target->licenses,+ 'accessories:asset' => $target->accessories,++ 'assets:location' => $target->assets,+ 'accessories:location' => $target->accessories,++ default => collect(),+ };+ }++ private function resolveTarget(): ?Model+ {+ return match ($this->targetType) {+ 'user' => User::find($this->targetId),+ 'asset' => Asset::find($this->targetId),+ 'location' => Location::find($this->targetId),+ default => null,+ };+ }++ private function itemNoun(): string+ {+ return match ($this->type) {+ 'assets' => trans('general.assets'),+ 'licenses' => trans('general.licenses'),+ 'accessories' => trans('general.accessories'),+ 'consumables' => trans('general.consumables'),+ };+ }+}diff --git a/resources/views/livewire/checkout-target-panel.blade.php b/resources/views/livewire/checkout-target-panel.blade.phpnew file mode 100644index 0000000000..007a53a85c--- /dev/null+++ b/resources/views/livewire/checkout-target-panel.blade.php@@ -0,0 +1,148 @@+<div class="col-md-5" @if ($targetId === null) style="display: none;" @endif>+ <div class="box box-primary">+ <div class="box-header with-border">+ <h2 class="box-title">+ {{ trans('admin/users/general.current_items', ['item' => $noun]) }}+ </h2>+ </div>+ <div class="box-body">+ <div class="row">+ <div class="col-md-12">+ <table class="table table-striped">+ @switch($type)++ @case('assets')+ <thead>+ <tr>+ <th scope="col"></th>+ <th scope="col">{{ trans('general.name') }}</th>+ <th scope="col">{{ trans('admin/hardware/form.tag') }}</th>+ <th scope="col">{{ trans('admin/hardware/form.serial') }}</th>+ </tr>+ </thead>+ <tbody>+ @forelse ($items as $asset)+ <tr>+ <td>+ @if ($asset->image_url ?? null)+ <img src="{{ $asset->image_url }}" style="max-height: {{ $snipeSettings->thumbnail_max_h }}px; width: auto;" alt="">+ @endif+ </td>+ <td>+ <a href="{{ route('hardware.show', $asset->id) }}">+ {{ $asset->name ? $asset->name.' ('.$asset->model?->name.')' : $asset->model?->name }}+ </a>+ </td>+ <td>{{ $asset->asset_tag }}</td>+ <td>{{ $asset->serial }}</td>+ </tr>+ @empty+ <tr><td colspan="4">{{ trans('admin/users/message.nothing_currently_assigned') }}</td></tr>+ @endforelse+ </tbody>+ @break++ @case('licenses')+ @php($canViewKeys = Gate::allows('viewKeys', \App\Models\License::class))+ <thead>+ <tr>+ <th scope="col">{{ trans('general.name') }}</th>+ @if ($canViewKeys)+ <th scope="col">{{ trans('admin/licenses/form.license_key') }}</th>+ @endif+ </tr>+ </thead>+ <tbody>+ @forelse ($items as $license)+ <tr>+ <td><a href="{{ route('licenses.show', $license->id) }}">{{ $license->name }}</a></td>+ @if ($canViewKeys)+ <td>{{ $license->serial }}</td>+ @endif+ </tr>+ @empty+ <tr><td colspan="{{ $canViewKeys ? 2 : 1 }}">{{ trans('admin/users/message.nothing_currently_assigned') }}</td></tr>+ @endforelse+ </tbody>+ @break++ @case('accessories')+ <thead>+ <tr>+ <th scope="col">{{ trans('general.name') }}</th>+ </tr>+ </thead>+ <tbody>+ @forelse ($items as $accessory)+ <tr>+ <td><a href="{{ route('accessories.show', $accessory->id) }}">{{ $accessory->name }}</a></td>+ </tr>+ @empty+ <tr><td>{{ trans('admin/users/message.nothing_currently_assigned') }}</td></tr>+ @endforelse+ </tbody>+ @break++ @case('consumables')+ <thead>+ <tr>+ <th scope="col">{{ trans('general.name') }}</th>+ </tr>+ </thead>+ <tbody>+ @forelse ($items as $consumable)+ <tr>+ <td><a href="{{ route('consumables.show', $consumable->id) }}">{{ $consumable->name }}</a></td>+ </tr>+ @empty+ <tr><td>{{ trans('admin/users/message.nothing_currently_assigned') }}</td></tr>+ @endforelse+ </tbody>+ @break++ @endswitch+ </table>+ </div>+ </div>+ </div>+ </div>++ @script+ <script>+ // Bridge the non-Livewire target selects (user / asset / location)+ // plus the checkout-selector radio toggle to this component. On+ // any change we resolve the currently-active target (based on the+ // radio) + its select value and dispatch. Pages that don't have+ // the radio (consumables/checkout is user-only) fall back to+ // 'user' as the assumed target type.+ //+ // The surrounding Livewire directive runs once per component+ // INSTANCE, so binding at this level doesn't accumulate handlers.+ var dispatchTarget = function () {+ var radioVal = $('input[name="checkout_to_type"]:checked').val();+ var targetType = radioVal || 'user';+ var selectSelectors = {+ 'user': '#assigned_user_select',+ 'asset': '#assigned_asset_select',+ 'location': '#assigned_location_location_select',+ };+ var $select = $(selectSelectors[targetType]);+ var targetId = $select.length ? ($select.val() || null) : null;++ $wire.dispatch('checkout-target-selected', {+ targetType: targetType,+ targetId: targetId,+ });+ };++ $('#assigned_user, #assigned_asset, #assigned_location').on('change', dispatchTarget);+ $('input[name="checkout_to_type"]').on('change', dispatchTarget);++ // Initial state on page load: pick up whatever the parent form+ // has server-rendered as the pre-selected target (session+ // remembers checkout_to_type across errors) so the sidebar shows+ // straight away instead of waiting for a change event.+ dispatchTarget();+ </script>+ @endscript+</div>