Skip to content

Commit

Permalink
Merge pull request rrze-mmz#163 from rrze-mmz/147-filter-series-based…
Browse files Browse the repository at this point in the history
…-on-semester-shows-a-livewire-error

Bugfix semester filter in series data table
  • Loading branch information
stefanosgeo authored Oct 17, 2024
2 parents b44d4e5 + 8e245d2 commit ed17ef3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Http/Middleware/RememberPreviousUrlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function handle(Request $request, Closure $next): Response
{
if (! $request->is(
'login', 'logout', 'register', 'password/*', 'verify-email/*', 'verified/*', 'saml2Login', 'api/*',
'livewire'
'livewire/*'
)) {
session(['url.intended' => url()->current()]);
}
Expand Down
5 changes: 4 additions & 1 deletion app/Livewire/SeriesDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ protected function determineClipQuery($search)

// Apply semester filter if a semester is selected
if ($this->selectedSemesterID) {
$query->where('semester_id', $this->selectedSemesterID);
// Filter series where at least one clip has the selected semester_id
$query->whereHas('clips', function ($q) {
$q->where('semester_id', $this->selectedSemesterID);
});
}

return $query;
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Series.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function scopeWithLastPublicClip($query)
->public()
->withVideoAssets()
->whereColumn('series_id', 'series.id')
->orderByDesc('episode')
->orderByDesc('recording_date')
->take(1),
]);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/Livewire/SeriesDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use App\Enums\Role;
use App\Livewire\SeriesDataTable;
use App\Models\Clip;
use App\Models\Semester;
use Facades\Tests\Setup\SeriesFactory;

use function Pest\Laravel\get;
Expand Down Expand Up @@ -44,3 +46,24 @@
Livewire::test(SeriesDataTable::class)
->assertSee($series->title);
});

it('filters series based on semester', function () {
$seriesA = SeriesFactory::withClips(3)->withAssets(1)->create();
$seriesA->clips->each(function (Clip $clip) {
$clip->semester_id = Semester::find(1)->id;
$clip->save();
});
$seriesB = SeriesFactory::withClips(3)->withAssets(1)->create();
$seriesB->clips->each(function (Clip $clip) {
$clip->semester_id = Semester::find(2)->id;
$clip->save();
});

Livewire::test(SeriesDataTable::class)
->assertSee($seriesA->title)
->assertSee($seriesB->title)
->set('selectedSemesterID', Semester::find(1)->id)
->assertSee($seriesA->title)
->set('selectedSemesterID', Semester::find(2)->id)
->assertSee($seriesB->title);
});

0 comments on commit ed17ef3

Please sign in to comment.