Skip to content

Commit

Permalink
[ADVAPP-979]: Enhance the user list experience by introducing a filte…
Browse files Browse the repository at this point in the history
…r for "no assigned team" (#1129)

* Add unassign filter into users team filter

* add count in test

* Add test case for team filter

* remove unneccessory function

* chore: fix code style

* Add new test

* Teams filter add group options

* replace id to getKey

---------

Co-authored-by: amit-canyon <[email protected]>
  • Loading branch information
amit-canyon and amit-canyon authored Nov 27, 2024
1 parent 85cf0be commit e9e40d5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,46 @@
)
->assertCanNotSeeTableRecords($supportTeamGroup);
});

it('it filters users based on team', function () {
asSuperAdmin();

$teamA = Team::factory()->create(['name' => 'Team A']);
$teamB = Team::factory()->create(['name' => 'Team B']);

$userInTeamA = User::factory()
->count(3)
->hasAttached($teamA, [], 'teams')
->create();

$userInTeamB = User::factory()
->count(3)
->hasAttached($teamB, [], 'teams')
->create();

$unassignedUser = User::factory()->count(2)->create();

livewire(ListUsers::class)
->assertCanSeeTableRecords($unassignedUser->merge($userInTeamA)->merge($userInTeamB))
->filterTable('teams', [$teamA->getKey()])
->assertCanSeeTableRecords(
$userInTeamA
)
->assertCanNotSeeTableRecords(
$unassignedUser->merge($userInTeamB)
)
->filterTable('teams', [$teamB->getKey()])
->assertCanSeeTableRecords(
$userInTeamB
)
->assertCanNotSeeTableRecords(
$unassignedUser->merge($userInTeamA)
)
->filterTable('teams', ['unassigned'])
->assertCanSeeTableRecords(
$unassignedUser
)
->assertCanNotSeeTableRecords(
$userInTeamA->merge($userInTeamB)
);
});
41 changes: 40 additions & 1 deletion app/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

use App\Models\User;
use Filament\Tables\Table;
use AdvisingApp\Team\Models\Team;
use Filament\Actions\CreateAction;
use Filament\Actions\ImportAction;
use App\Filament\Imports\UserImporter;
Expand All @@ -49,6 +50,7 @@
use App\Filament\Tables\Columns\IdColumn;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Database\Eloquent\Builder;
use Filament\Tables\Actions\RestoreAction;
use Filament\Tables\Filters\TrashedFilter;
use Illuminate\Contracts\Support\Htmlable;
Expand Down Expand Up @@ -153,14 +155,30 @@ public function table(Table $table): Table
->visible((fn () => auth()->user()->can('user.*.restore'))),
SelectFilter::make('teams')
->label('Team')
->relationship('teams', 'name')
->options($this->getTeamsOption())
->getSearchResultsUsing(fn (string $search) => Team::query()->where('name', 'like', '%' . $search . '%')->take(50)->pluck('name', 'id')->toArray())
->query(fn (Builder $query, array $data) => $this->teamFilter($query, $data))
->multiple()
->searchable()
->preload(),
])
->defaultSort('name', 'asc');
}

public function getTeamsOption(): array
{
$teams = Team::query()->take(50)->pluck('name', 'id')->toArray();

return [
'' => [
'unassigned' => 'Unassigned',
],
'Teams' => [
...$teams,
],
];
}

protected function getHeaderActions(): array
{
return [
Expand All @@ -170,4 +188,25 @@ protected function getHeaderActions(): array
CreateAction::make(),
];
}

protected function teamFilter(Builder $query, array $data): void
{
if (empty($data['values'])) {
return;
}

$query->where(function ($query) use ($data) {
$filteredValues = $data['values'];
$query->when(in_array('unassigned', $filteredValues), function ($query) {
$query->whereDoesntHave('teams');
})
->orWhereHas('teams', function ($query) use ($filteredValues) {
if (in_array('unassigned', $filteredValues)) {
unset($filteredValues[array_search('unassigned', $filteredValues)]);
}

$query->whereIn('team_id', $filteredValues);
});
});
}
}

0 comments on commit e9e40d5

Please sign in to comment.