Skip to content

Commit

Permalink
Merge branch 'main' into documents-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Apr 11, 2024
2 parents 9097812 + 30f0334 commit 3dc7823
Show file tree
Hide file tree
Showing 21 changed files with 728 additions and 215 deletions.
6 changes: 6 additions & 0 deletions app/Enum/OrganisationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ enum OrganisationStatus: string

case active = 'active';
case inactive = 'inactive';
case invited = 'invited';

protected function labelKeyPrefix(): ?string
{
return 'organisation.status';
}
}
26 changes: 22 additions & 4 deletions app/Filament/Filters/ResourceTreeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace App\Filament\Filters;

use App\Models\Resource\Category;
use Cache;
use Closure;
use Filament\Forms\Components\Select;
use Filament\Tables\Filters\BaseFilter;
use Illuminate\Database\Eloquent\Builder;

class ResourceTreeFilter extends BaseFilter
{
Expand All @@ -29,9 +31,13 @@ protected function setUp(): void
{
parent::setUp();

$categories = Category::query()
->with('subcategories.types')
->get();
$categories = Cache::driver('array')
->rememberForever(
'resource-tree-filter-categories',
fn () => Category::query()
->with('subcategories.types')
->get()
);

$this->form(fn () => [
Select::make('category')
Expand Down Expand Up @@ -61,6 +67,18 @@ protected function setUp(): void
->pluck('name', 'id')
)
->hidden(fn (Select $component) => empty($component->getOptions())),
]);
])
->query(function (Builder $query, array $data) use ($categories) {
$subcategory = $categories->firstWhere('id', $data['category'])
?->subcategories
->firstWhere('id', $data['subcategory']);

$type = $subcategory?->types
->firstWhere('id', $data['type']);

return $query->when($data['category'], fn (Builder $query, $value) => $query->where('category_id', $value))
->when($subcategory?->id, fn (Builder $query, $value) => $query->where('subcategory_id', $value))
->when($type?->id, fn (Builder $query, $value) => $query->whereRelation('types', 'type_id', $value));
});
}
}
18 changes: 6 additions & 12 deletions app/Filament/Resources/OrganisationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Enum\NGOType;
use App\Enum\OrganisationAreaType;
use App\Enum\OrganisationStatus;
use App\Enum\OrganisationType;
use App\Filament\Forms\Components\Location;
use App\Filament\Resources\OrganisationResource\Pages;
Expand Down Expand Up @@ -126,6 +127,7 @@ public static function form(Form $form): Form
->maxLength(1000)
->rows(2)
->helperText(__('organisation.help.description'))
->required()
->columnSpanFull(),
]),

Expand Down Expand Up @@ -379,21 +381,13 @@ public static function table(Table $table): Table
->sortable()
->toggleable(),

IconColumn::make('status')
->options([
'heroicon-o-x-circle',
'heroicon-o-x' => 'inactive',
'heroicon-o-check' => 'active',
])
Tables\Columns\BadgeColumn::make('status')
->colors([
'secondary',
'danger' => 'inactive',
'secondary' => 'inactive',
'warning' => 'guest',
'success' => 'active',
])
->label(__('organisation.field.status'))
->searchable()
->sortable()
->toggleable(),
->enum(OrganisationStatus::options()),

TextColumn::make('county.name')
->label(__('organisation.field.hq'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\OrganisationResource\Actions;

use App\Models\Organisation;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Illuminate\Support\Facades\RateLimiter;

class ResendInvitationAction extends Action
{
public static function getDefaultName(): ?string
{
return 'resend_invitation';
}

protected function setUp(): void
{
parent::setUp();

$this->color('secondary');

$this->action(function (Organisation $record) {
$key = $this->getRateLimiterKey($record);
$maxAttempts = 1;

if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
return $this->failure();
}

RateLimiter::increment($key, 3600); // 1h

$record->users->first()->sendWelcomeNotification();
$this->success();
});

$this->requiresConfirmation();

$this->label(__('organisation.action.resend_invitation.button'));

$this->modalHeading(__('organisation.action.resend_invitation.heading'));
$this->modalSubheading(__('organisation.action.resend_invitation.subheading'));
$this->modalButton(__('organisation.action.resend_invitation.button'));

$this->successNotificationTitle(__('organisation.action.resend_invitation.success'));

$this->failureNotification(
fn (Notification $notification) => $notification
->danger()
->title(__('organisation.action.resend_invitation.failure_title'))
->body(__('organisation.action.resend_invitation.failure_body'))
);
}

private function getRateLimiterKey(Organisation $organisation): string
{
return 'resend-invitation:' . $organisation->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Filament\Resources\OrganisationResource;
use App\Filament\Resources\OrganisationResource\Actions\ActivateOrganisationAction;
use App\Filament\Resources\OrganisationResource\Actions\DeactivateOrganisationAction;
use App\Filament\Resources\OrganisationResource\Actions\ResendInvitationAction;
use App\Models\Organisation;
use Filament\Pages\Actions\DeleteAction;
use Filament\Pages\Actions\EditAction;
Expand All @@ -28,6 +29,10 @@ protected function getActions(): array
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isInactive())
->record($this->getRecord()),

ResendInvitationAction::make()
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isInvited())
->record($this->getRecord()),

DeactivateOrganisationAction::make()
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isActive())
->record($this->getRecord()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Filament\Resources\OrganisationResource\RelationManagers;

use App\Filament\Filters\ResourceTreeFilter;
use App\Filament\Forms\FieldGroups;
use App\Filament\Resources\ResourceResource;
use App\Filament\Tables\Actions\ExportAction;
Expand Down Expand Up @@ -145,22 +146,14 @@ public static function table(Table $table): Table
->toggleable(),
])
->filters([
SelectFilter::make('category')
->label(__('resource.fields.category'))
->relationship('category', 'name'),

SelectFilter::make('subcategory')
->relationship('subcategory', 'name')
->label(__('resource.fields.subcategory')),

SelectFilter::make('type')
->label(__('resource.fields.type'))
->relationship('types', 'name'),

SelectFilter::make('county')
->label(__('general.county'))
->relationship('county', 'name'),

ResourceTreeFilter::make('cat')
->columns(3)
->columnSpan(3),

])
->headerActions([
ExportAction::make(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function table(Table $table): Table
])
->headerActions([
ExportAction::make(),

// VolunteerResource\Actions\ImportVolunteersAction::make(),
Tables\Actions\CreateAction::make()
->requiresConfirmation()
->modalHeading(__('volunteer.modal.heading'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\VolunteerResource\Actions;

use App\Models\Volunteer;
use pxlrbt\FilamentExcel\Actions\Pages\ExportAction;
use pxlrbt\FilamentExcel\Columns\Column;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

class ExportVolunteersExample extends ExportAction
{
protected function setUp(): void
{
parent::setUp();
$this->label(__('volunteer.labels.download_example'));

$this->exports([
ExcelExport::make()
->withColumns([
Column::make('first_name'),
Column::make('last_name'),
Column::make('email'),
Column::make('phone'),
Column::make('cnp'),
Column::make('rol'),
Column::make('specialisations'),
Column::make('has_first_aid_accreditation'),
Column::make('county')
->formatStateUsing(fn ($state) => $state->name),
Column::make('city')
->formatStateUsing(fn ($state) => $state->name),
]),
]);
}

public function handleExport(array $data)
{
$examples = Volunteer::factory()
->count(10)
->make()
->each(function ($item) {
$item->load('county');
$item->load('city');

return $item;
});

$examplesArray = [];
foreach ($examples as $example) {
$examplesArray[] = $example;
}

$exportable = $this->getSelectedExport($data);
$livewire = $this->getLivewire();

return app()->call([$exportable, 'hydrate'], [
'livewire' => $this->getLivewire(),
'records' => $examples,
'formData' => data_get($data, $exportable->getName()),
])->export();
}
}
Loading

0 comments on commit 3dc7823

Please sign in to comment.