Skip to content

Commit

Permalink
refactor: organisation status toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Jan 23, 2024
1 parent 2869100 commit 749570d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\OrganisationResource\Actions;

use App\Models\Organisation;
use Filament\Pages\Actions\Action;

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

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

$this->color('secondary');

$this->action(function (Organisation $record, Action $action) {
$record->setActive();
$action->success();
});

$this->requiresConfirmation();

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

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

$this->successNotificationTitle(__('organisation.action.change_status.inactive.success'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\OrganisationResource\Actions;

use App\Models\Organisation;
use Filament\Pages\Actions\Action;

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

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

$this->color('secondary');

$this->action(function (Organisation $record, Action $action) {
$record->setInactive();
$action->success();
});

$this->requiresConfirmation();

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

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

$this->successNotificationTitle(__('organisation.action.change_status.active.success'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace App\Filament\Resources\OrganisationResource\Pages;

use App\Filament\Resources\OrganisationResource;
use App\Filament\Resources\OrganisationResource\Actions\ActivateOrganisationAction;
use App\Filament\Resources\OrganisationResource\Actions\DeactivateOrganisationAction;
use App\Models\Organisation;
use Filament\Pages\Actions\Action;
use Filament\Pages\Actions\DeleteAction;
use Filament\Pages\Actions\EditAction;
use Filament\Resources\Pages\ViewRecord;
Expand All @@ -22,23 +23,14 @@ public function getTitle(): string

protected function getActions(): array
{
$status = $this->getRecord()->status?->value;

return [
Action::make('change_status')
->visible(fn () => auth()->user()->isPlatformAdmin())
->record($this->getRecord())
->action(function (Organisation $record, Action $action) {
$record->toggleStatus();
$action->success();
})
->requiresConfirmation()
->label(__('organisation.action.change_status.' . $status . '.button'))
->successNotificationTitle(__('organisation.action.change_status.' . $status . '.success'))
->modalHeading(__('organisation.action.change_status.' . $status . '.heading'))
->modalSubheading(__('organisation.action.change_status.' . $status . '.subheading'))
->modalButton(__('organisation.action.change_status.' . $status . '.button'))
->color('secondary'),
ActivateOrganisationAction::make()
->visible(fn (Organisation $record) => auth()->user()->isPlatformAdmin() && $record->isInactive())
->record($this->getRecord()),

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

EditAction::make(),

Expand Down
7 changes: 0 additions & 7 deletions app/Models/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,6 @@ public function isInactive(): bool
return $this->status->is(OrganisationStatus::inactive);
}

public function toggleStatus(): bool
{
return $this->isInactive()
? $this->setActive()
: $this->setInActive();
}

public function routeNotificationForMail(?Notification $notification = null): string
{
return data_get($this->contact_person, ['email'], $this->email);
Expand Down

0 comments on commit 749570d

Please sign in to comment.