Skip to content

Commit

Permalink
allow for closure for most methods
Browse files Browse the repository at this point in the history
  • Loading branch information
0wain committed Aug 7, 2024
1 parent 1764a54 commit db67c6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ GazeBanner::make()
#### Arguments
`timeInSeconds` - (int) The amount of time in seconds between each poll. Default is 30 seconds.

### `->identifier($name)`
### `->identifier($fnc)`

#### Description
`identifier` is used as a unique identifier for this gaze banner. Any other gaze banners with the same identifier will share the same list of active users. This can be useful if you want 2 or more difference resources to share the same list of active viewing users.

#### Arguments
`name` - (string) The name of the identifier. Default is the resource's model class combines with model Id.
`fnc` - (optional, closure | string) The name of the identifier. Default is the resource's model class combines with model Id.

### `->lock($state)`
### `->lock($fnc)`

#### Description
`lock` can be used to lock the resource for anyone but the current person editing the form. This can be useful if you want to prevent multiple people from editing the same resource at the same time. The controller is the first person to access the resource, or the person who has taken control of the resource. If you enable this after accessing a resource, you may need to run `php artisan cache:clear` as it is possible that the current cached viewers don't have a marked controller.

#### Arguments
`state` - (optional, bool) If the resource is lockable or not.
`fnc` - (optional, closure | bool) If the resource is lockable or not.

### `->canTakeControl($fnc)`

Expand Down
22 changes: 9 additions & 13 deletions src/Forms/Components/GazeBanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

use Carbon\Carbon;
use Closure;
use Filament\Actions\Contracts\HasLivewire;
use Filament\Facades\Filament;
use Filament\Forms\Components\Component;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Cache;
use Livewire\Livewire;

/**
* Class GazeBanner
Expand Down Expand Up @@ -46,7 +43,6 @@ class GazeBanner extends Component
*/
public bool $canTakeControl = false;


/**
* Create a new instance of the GazeBanner component.
*/
Expand All @@ -64,9 +60,9 @@ public static function make(array | Closure $schema = []): static
* @param string $identifier
* @return $this
*/
public function identifier($identifier)
public function identifier(string | Closure $fnc = true): static
{
$this->identifier = $identifier;
$this->identifier = (string) $this->evaluate($fnc);

return $this;
}
Expand All @@ -87,14 +83,14 @@ public function pollTimer($poll)
/**
* Set the lock state
*/
public function lock($state = true): static
public function lock(bool | Closure $fnc = true): static
{
$this->isLockable = $state;
$this->isLockable = (bool) $this->evaluate($fnc);

if ($state) {
if ($this->isLockable) {
$this->registerListeners([
'FilamentGaze::takeControl' => [
function() {
function () {
// Very hacky, maybe a better solution for this?
$this->getLivewire()->mount($this->getLivewire()->getForm('form')->getRecord()?->id);
$this->takeControl();
Expand Down Expand Up @@ -135,7 +131,7 @@ public function takeControl()

public function getIdentifier()
{
if (!$this->identifier) {
if (! $this->identifier) {
$record = $this->getRecord();
if (! $record) {
$this->identifier = (string) $this->getModel();
Expand All @@ -159,7 +155,7 @@ public function refreshViewers()
{
$this->registerListeners([
'FilamentGaze::takeControl' => [
function() {
function () {
$this->refreshViewers();
},
],
Expand Down Expand Up @@ -249,7 +245,7 @@ public function render(): \Illuminate\Contracts\View\View
$hasControl = isset($lockUser) && $lockUser['id'] == auth()->id();

if ($this->isLockable) {
$this->getLivewire()->getForm('form')->disabled(!$hasControl);
$this->getLivewire()->getForm('form')->disabled(! $hasControl);
}

return view('filament-gaze::forms.components.gaze-banner', [
Expand Down

0 comments on commit db67c6f

Please sign in to comment.