Skip to content

Commit

Permalink
Merge pull request #3 from discoverydesign/fix-2
Browse files Browse the repository at this point in the history
Fix control not being passed in some cases & refresh form data when being given control
  • Loading branch information
discoveryjames authored Aug 10, 2024
2 parents 9d50abd + 327e95a commit 578a57d
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/Forms/Components/GazeBanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public function lock(bool | Closure $fnc = true): static
$this->registerListeners([
'FilamentGaze::takeControl' => [
function () {
// Very hacky, maybe a better solution for this?
$this->getLivewire()->mount($this->getLivewire()->getForm('form')->getRecord()?->id);
$this->refreshForm();
$this->takeControl();
},
],
Expand All @@ -118,11 +117,12 @@ public function takeControl()
$identifier = $this->getIdentifier();
$curViewers = Cache::get('filament-gaze-' . $identifier, []);

$authGuard = Filament::getCurrentPanel()->getAuthGuard();
foreach ($curViewers as $key => $viewer) {
$curViewers[$key]['has_control'] = false;

if ($viewer['id'] == auth()->id()) {
if ($viewer['id'] == auth()->guard($authGuard)->id()) {
$curViewers[$key]['has_control'] = true;
} else {
$curViewers[$key]['has_control'] = false;
}
}

Expand All @@ -143,6 +143,12 @@ public function getIdentifier()
return $this->identifier;
}

public function refreshForm()
{
// Very hacky, maybe a better solution for this?
$this->getLivewire()->mount($this->getLivewire()->getForm('form')->getRecord()?->id);
}

/**
* Refresh the list of viewers.
*
Expand All @@ -168,6 +174,7 @@ function () {
$guardProvider = config('auth.guards.' . $authGuard . '.provider');
$guardModel = config('auth.providers.' . $guardProvider . '.model');

$someoneHasLockState = false;
$lockState = false;

// Check over all current viewers
Expand All @@ -176,18 +183,30 @@ function () {
$model = $guardModel::find($viewer['id']);
$expires = Carbon::parse($viewer['expires']);

if (! $model) {
unset($curViewers[$key]);

continue;
}

// Remove expired viewers
if ($expires->isPast()) {
unset($curViewers[$key]);

continue;
}

// If current user, remove them so they can be re-added below.
if (! $model || ($model?->id == auth()?->id())) {

if ($viewer['id'] == auth()->guard($authGuard)?->id()) {
// Preserve their active lock state
$lockState = $viewer['has_control'];

unset($curViewers[$key]);
}

if ($viewer['has_control']) {
$someoneHasLockState = true;
}
}

$user = auth()->guard($authGuard)->user();
Expand All @@ -199,6 +218,21 @@ function () {
'has_control' => $this->isLockable && ($lockState || (count($curViewers) === 0)),
];

// If no one has lock state, give it to the first person.
// Annoyingly the table isn't sorted so we can't just grab the first person.
if ($this->isLockable && ! $someoneHasLockState) {
foreach ($curViewers as $key => $viewer) {
$curViewers[$key]['has_control'] = true;

// Refresh the form is it's the current user being given control.
if ($viewer['id'] == auth()->guard($authGuard)->id()) {
$this->refreshForm();
}

break;
}
}

$this->currentViewers = $curViewers;

Cache::put('filament-gaze-' . $identifier, $curViewers, now()->addSeconds($this->pollTimer * 2));
Expand All @@ -215,8 +249,9 @@ public function render(): \Illuminate\Contracts\View\View

$formattedViewers = '';
$currentViewers = collect($this->currentViewers);
$filteredViewers = $currentViewers->filter(function ($viewer) {
return $viewer['id'] != auth()->id();
$authGuard = Filament::getCurrentPanel()->getAuthGuard();
$filteredViewers = $currentViewers->filter(function ($viewer) use ($authGuard) {
return $viewer['id'] != auth()->guard($authGuard)->id();
});

$finalText = '';
Expand All @@ -242,7 +277,7 @@ public function render(): \Illuminate\Contracts\View\View
}

$lockUser = collect($this->currentViewers)->where('has_control', true)->first();
$hasControl = isset($lockUser) && $lockUser['id'] == auth()->id();
$hasControl = isset($lockUser) && $lockUser['id'] == auth()->guard($authGuard)->id();

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

0 comments on commit 578a57d

Please sign in to comment.