Skip to content

Commit

Permalink
Review/users (#288)
Browse files Browse the repository at this point in the history
* wip

* Add count in organizations  tables

* Add id on organization widgets

* add hooks for php-cs-fixer

* test-cs-fixer

* wip on user relations manager

* test hooks

* retusari users
  • Loading branch information
gheorghelupu17 authored Sep 12, 2023
1 parent 051a6c7 commit 2eb91d1
Show file tree
Hide file tree
Showing 21 changed files with 582 additions and 27 deletions.
61 changes: 61 additions & 0 deletions .githooks/cs-fixer
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
unset CDPATH
IFS='
'
get_config() {
for candidate in .php-cs-fixer.php .php-cs-fixer.dist.php .php_cs .php_cs.dist; do
if test -f "./$candidate"; then
echo "./$candidate"
return
fi
done
}

run_php_cs_fixer() {
echo $(pwd);
php_cs_fixer_bin=./vendor/bin/php-cs-fixer
if test ! -f "$php_cs_fixer_bin"; then
echo "php-cs-fixer file not found"
return # Noop, php-cs-fixer not found.
fi

config="$(get_config)"
if test -z "$config"; then
echo "php_cs_fixer_bin not found"
return # Noop, php-cs-fixer config not found.
fi

changed_files=$(git diff --staged --name-only --diff-filter=ACMRTUXB)

if ! echo "${changed_files}" | grep -qE "^(laravel/)?(\\.php-cs-fixer(\\.dist)?|composer\\.lock)$"; then

extra_args=$(printf -- '--path-mode=intersection\n--\n%s' "${changed_files}")
else
extra_args=''
fi

rerun_with_violation_details() {
echo >&2 "\033[0;33mCoding guidelines pre-commit check failed.\033[1;0m"
echo >&2 "\033[0;33mRunning check again in verbose mode ...\033[1;0m"

"$php_cs_fixer_bin" fix --config="$config"\
--stop-on-violation \
--using-cache=no \
${extra_args}

exit 1
}

# PHP Coding Standards Fixer is run quietly. If there are errors it is run
# again in verbose mode to provide more information about which rules and
# which files are failing the check.
"$php_cs_fixer_bin" fix \
--config="$config" \
--stop-on-violation \
--using-cache=no \
${extra_args} || rerun_with_violation_details
}


run_php_cs_fixer

7 changes: 7 additions & 0 deletions app/Enums/VolunteerStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;

enum VolunteerStatus: string
{
use ArrayableEnum;
case PENDING = 'pending';
case APPROVED = 'approved';
case REJECTED = 'rejected';
public function translationKeyPrefix(): string
{
return 'volunteer.statuses';
}
}
5 changes: 5 additions & 0 deletions app/Filament/Resources/OrganizationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class OrganizationResource extends Resource

protected static ?string $navigationIcon = 'heroicon-o-office-building';

protected static function getNavigationBadge(): ?string
{
return (string) static::$model::count();
}

protected static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ApprovedOrganizationsWidget extends BaseOrganizationsWidget

protected function getTableHeading(): string
{
return __('organization.heading.approved');
return __('organization.heading.approved', ['number' => $this->getTableQuery()->count()]);
}

protected function getTableQuery(): Builder
Expand All @@ -37,6 +37,13 @@ protected function getTableQueryStringIdentifier(): ?string
protected function getTableColumns(): array
{
return [
TextColumn::make('id')
->label(__('field.id'))
->formatStateUsing(
fn ($state) => __('field.id_format', ['number' => $state])
)
->sortable(),

TitleWithImageColumn::make('name')
->label(__('organization.organization'))
->image(fn ($record) => $record->getFirstMediaUrl('logo'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ protected function getTableHeaderActions(): array
protected function getTableColumns(): array
{
return [
TextColumn::make('id')
->label(__('field.id'))
->formatStateUsing(
fn ($state) => __('field.id_format', ['number' => $state])
)
->sortable(),

TitleWithImageColumn::make('name')
->label(__('organization.organization'))
->image(fn ($record) => $record->cover_image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PendingChangesOrganizationsWidget extends BaseOrganizationsWidget
{
protected function getTableHeading(): string
{
return __('organization.heading.pending_changes');
return __('organization.heading.pending_changes', ['number' => $this->getTableQuery()->count()]);
}

protected function getTableQuery(): Builder
Expand Down Expand Up @@ -50,6 +50,13 @@ protected function getTableQueryStringIdentifier(): ?string
protected function getTableColumns(): array
{
return [
TextColumn::make('id')
->label(__('field.id'))
->formatStateUsing(
fn ($state) => __('field.id_format', ['number' => $state])
)
->sortable(),

TitleWithImageColumn::make('name')
->label(__('organization.organization'))
->image(fn (Organization $record) => $record->getFirstMediaUrl('logo'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PendingOrganizationsWidget extends BaseOrganizationsWidget
{
protected function getTableHeading(): string
{
return __('organization.heading.in_approval');
return __('organization.heading.in_approval', ['number' => $this->getTableQuery()->count()]);
}

protected function getTableQuery(): Builder
Expand All @@ -34,6 +34,13 @@ protected function getTableQueryStringIdentifier(): ?string
protected function getTableColumns(): array
{
return [
TextColumn::make('id')
->label(__('field.id'))
->formatStateUsing(
fn ($state) => __('field.id_format', ['number' => $state])
)
->sortable(),

TitleWithImageColumn::make('name')
->label(__('organization.organization'))
->image(fn ($record) => $record->getFirstMediaUrl('logo'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RejectedOrganizationsWidget extends BaseOrganizationsWidget

protected function getTableHeading(): string
{
return __('organization.heading.rejected');
return __('organization.heading.rejected', ['number' => $this->getTableQuery()->count()]);
}

protected function getTableQuery(): Builder
Expand All @@ -37,6 +37,13 @@ protected function getTableQueryStringIdentifier(): ?string
protected function getTableColumns(): array
{
return [
TextColumn::make('id')
->label(__('field.id'))
->formatStateUsing(
fn ($state) => __('field.id_format', ['number' => $state])
)
->sortable(),

TitleWithImageColumn::make('name')
->label(__('organization.organization'))
->image(fn ($record) => $record->getFirstMediaUrl('logo'))
Expand Down
124 changes: 115 additions & 9 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,160 @@

namespace App\Filament\Resources;

use App\Enums\UserRole;
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\RelationManagers\BadgesRelationManager;
use App\Filament\Resources\UserResource\RelationManagers\DonationsRelationManager;
use App\Filament\Resources\UserResource\RelationManagers\VolunteersRelationManager;
use App\Models\User;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Illuminate\Database\Eloquent\Builder;

class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationGroup = 'Administrează';

protected static ?int $navigationSort = 2;

protected static ?string $navigationIcon = 'heroicon-o-collection';
protected static ?string $navigationIcon = 'heroicon-o-users';

public static function getPluralLabel(): ?string
{
return __('user.label.plural');
}

public static function getModelLabel(): string
{
return __('user.label.singular');
}

protected static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
}

public static function form(Form $form): Form
{
return $form;
return $form->schema([
Fieldset::make(__('user.labels.general_data'))
->columns(1)->schema([
TextInput::make('name')
->label(__('user.name'))
->inlineLabel()
->required()
->maxLength(255),
TextInput::make('email')
->label(__('user.email'))
->email()
->unique('users', 'email')
->inlineLabel()
->required(),
Select::make('role')
->label(__('user.role'))
->options(
collect(
UserRole::options()
)->only(
[
UserRole::bb_admin->value,
UserRole::bb_manager->value,
UserRole::ngo_admin->value,
]
)->toArray()
)->reactive()
->inlineLabel()
->visibleOn(['edit', 'create'])
->required(),
Select::make('role')
->label(__('user.role'))
->options(UserRole::options())
->reactive()
->inlineLabel()
->visibleOn(['view'])
->required(),
Select::make('organization')
->label(__('user.organization'))
->relationship('organization', 'name')
->hidden(function (callable $get) {
return $get('role') !== UserRole::ngo_admin->value;
})
->searchable()
->preload()
->required(),
]),

]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')->searchable()->sortable(),
TextColumn::make('email')->searchable()->sortable(),
TextColumn::make('name')
->label(__('user.name'))
->searchable()
->sortable(),
TextColumn::make('email')
->label(__('user.email'))
->searchable()
->sortable(),
TextColumn::make('role')
->label(__('user.role'))
->getStateUsing(fn ($record) => $record->role->label())
->searchable()
->sortable(),
])
->filters([
//
SelectFilter::make('role')
->label(__('user.filters.type'))
->placeholder(__('user.filters.placeholder'))
->options(UserRole::options())
->multiple(),
SelectFilter::make('organization')
->label(__('user.organization'))
->relationship('organization', 'name')
->searchable(),
TernaryFilter::make('has_donations')
->label(__('user.filters.has_donations'))
->queries(
true: fn (Builder $query) => $query->whereHas('donations'),
false: fn (Builder $query) => $query->doesntHave('donations'),
),
TernaryFilter::make('is_volunteer')
->label(__('user.filters.is_volunteer'))
->queries(
true: fn (Builder $query) => $query->has('volunteer'),
false: fn (Builder $query) => $query->doesntHave('volunteer'),
),

])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
DonationsRelationManager::class,
VolunteersRelationManager::class,
BadgesRelationManager::class
];
}


public static function getPages(): array
{
return [
Expand Down
12 changes: 11 additions & 1 deletion app/Filament/Resources/UserResource/Pages/ViewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ class ViewUser extends ViewRecord
protected function getActions(): array
{
return [
Actions\EditAction::make(),
Actions\EditAction::make()->hidden(fn () => $this->getRecord()->isDonor()),
];
}

public function hasCombinedRelationManagerTabsWithForm(): bool
{
return true;
}

public function getFormTabLabel(): ?string
{
return __('user.label.singular');
}
}
Loading

0 comments on commit 2eb91d1

Please sign in to comment.