From ec704d8d83aba9d003619713b79f86a966d6e145 Mon Sep 17 00:00:00 2001 From: Benjamin Nussbaum Date: Fri, 5 Sep 2025 13:12:47 +0200 Subject: [PATCH] fix(#88): allow selection of playlist for multiple devices --- .../views/livewire/plugins/recipe.blade.php | 150 ++++++++++++------ 1 file changed, 98 insertions(+), 52 deletions(-) diff --git a/resources/views/livewire/plugins/recipe.blade.php b/resources/views/livewire/plugins/recipe.blade.php index 3a8e7cc..bfa3028 100644 --- a/resources/views/livewire/plugins/recipe.blade.php +++ b/resources/views/livewire/plugins/recipe.blade.php @@ -24,11 +24,11 @@ new class extends Component { public $data_payload; public ?Carbon $data_payload_updated_at; public array $checked_devices = []; - public string $playlist_name = ''; - public array|null $selected_weekdays = null; - public string $active_from = ''; - public string $active_until = ''; - public string $selected_playlist = ''; + public array $device_playlists = []; + public array $device_playlist_names = []; + public array $device_weekdays = []; + public array $device_active_from = []; + public array $device_active_until = []; public string $mashup_layout = 'full'; public array $mashup_plugins = []; public array $configuration_template = []; @@ -176,29 +176,40 @@ new class extends Component { { $this->validate([ 'checked_devices' => 'required|array|min:1', - 'selected_playlist' => 'required|string', 'mashup_layout' => 'required|string', 'mashup_plugins' => 'required_if:mashup_layout,1Lx1R,1Lx2R,2Lx1R,1Tx1B,2Tx1B,1Tx2B,2x2|array', ]); + // Validate that each checked device has a playlist selected + foreach ($this->checked_devices as $deviceId) { + if (!isset($this->device_playlists[$deviceId]) || empty($this->device_playlists[$deviceId])) { + $this->addError('device_playlists.' . $deviceId, 'Please select a playlist for each device.'); + return; + } + + // If creating new playlist, validate required fields + if ($this->device_playlists[$deviceId] === 'new') { + if (!isset($this->device_playlist_names[$deviceId]) || empty($this->device_playlist_names[$deviceId])) { + $this->addError('device_playlist_names.' . $deviceId, 'Playlist name is required when creating a new playlist.'); + return; + } + } + } + foreach ($this->checked_devices as $deviceId) { $playlist = null; - if ($this->selected_playlist === 'new') { + if ($this->device_playlists[$deviceId] === 'new') { // Create new playlist - $this->validate([ - 'playlist_name' => 'required|string|max:255', - ]); - $playlist = \App\Models\Playlist::create([ 'device_id' => $deviceId, - 'name' => $this->playlist_name, - 'weekdays' => !empty($this->selected_weekdays) ? $this->selected_weekdays : null, - 'active_from' => $this->active_from ?: null, - 'active_until' => $this->active_until ?: null, + 'name' => $this->device_playlist_names[$deviceId], + 'weekdays' => !empty($this->device_weekdays[$deviceId] ?? null) ? $this->device_weekdays[$deviceId] : null, + 'active_from' => $this->device_active_from[$deviceId] ?? null, + 'active_until' => $this->device_active_until[$deviceId] ?? null, ]); } else { - $playlist = \App\Models\Playlist::findOrFail($this->selected_playlist); + $playlist = \App\Models\Playlist::findOrFail($this->device_playlists[$deviceId]); } // Add plugin to playlist @@ -222,7 +233,16 @@ new class extends Component { } } - $this->reset(['checked_devices', 'playlist_name', 'selected_weekdays', 'active_from', 'active_until', 'selected_playlist', 'mashup_layout', 'mashup_plugins']); + $this->reset([ + 'checked_devices', + 'device_playlists', + 'device_playlist_names', + 'device_weekdays', + 'device_active_from', + 'device_active_until', + 'mashup_layout', + 'mashup_plugins' + ]); Flux::modal('add-to-playlist')->close(); } @@ -252,6 +272,16 @@ new class extends Component { return \App\Models\Playlist::where('device_id', $deviceId)->get(); } + public function hasAnyPlaylistSelected(): bool + { + foreach ($this->checked_devices as $deviceId) { + if (isset($this->device_playlists[$deviceId]) && !empty($this->device_playlists[$deviceId])) { + return true; + } + } + return false; + } + public function getConfigurationValue($key, $default = null) { return $this->configuration[$key] ?? $default; @@ -450,44 +480,60 @@ HTML; - @if(count($checked_devices) === 1) - -
- - - @foreach($this->getDevicePlaylists($checked_devices[0]) as $playlist) - - @endforeach - - + @if(count($checked_devices) > 0) + +
+ @foreach($checked_devices as $deviceId) + @php + $device = auth()->user()->devices->find($deviceId); + @endphp +
+
+ {{ $device->name }} +
+ +
+ + + @foreach($this->getDevicePlaylists($deviceId) as $playlist) + + @endforeach + + +
+ + @if(isset($device_playlists[$deviceId]) && $device_playlists[$deviceId] === 'new') +
+
+ +
+
+ + + + + + + + + +
+
+
+ +
+
+ +
+
+
+ @endif +
+ @endforeach
@endif - @if($selected_playlist) - @if($selected_playlist === 'new') -
- -
-
- - - - - - - - - -
- -
- -
- -
- -
- @endif + @if(count($checked_devices) > 0 && $this->hasAnyPlaylistSelected())