-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamSwitcher.php
More file actions
66 lines (54 loc) · 2.05 KB
/
Copy pathTeamSwitcher.php
File metadata and controls
66 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleUsers\Livewire;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use NyonCode\WireCore\Core\Plugin\Contracts\IdentifiesHookTarget;
use NyonCode\WireCore\Notifications\NotificationManager;
use NyonCode\WireModuleUsers\Support\Teams;
/**
* The control in the top bar that says which team you are looking at.
*
* It reaches the chrome through `PageChrome::TOPBAR` rather than by editing the
* shell's layout, which is the same seam the media picker uses at the other end
* of the document: the shell sits above every module and cannot name their
* views, and a module cannot reach into the shell's Blade.
*
* Switching redirects rather than re-rendering, and that is not laziness. The
* team scopes every permission read on the request, so a page composed *before*
* the switch — its menu, its actions, the rows a policy let through — is a page
* built for the team you just left. A fresh request is the only honest answer.
*/
class TeamSwitcher extends Component implements IdentifiesHookTarget
{
public int|string|null $current = null;
public function mount(): void
{
$this->current = Teams::currentId();
}
public function switchTo(int|string $team): void
{
if (! Teams::switchTo($team)) {
// Not a member, or teams are off: the control said otherwise, and
// the control is markup.
return;
}
$this->current = $team;
NotificationManager::success(__('wire-module-users::messages.team_switched', [
'team' => Teams::optionsFor()[$team] ?? '',
]));
$this->redirect(request()->header('Referer') ?? url('/'), navigate: false);
}
public function hookKey(): ?string
{
return 'users.team-switcher';
}
public function render(): View
{
$teams = Teams::optionsFor();
return view('wire-module-users::livewire.team-switcher', [
'teams' => $teams,
'currentLabel' => $teams[$this->current] ?? null,
]);
}
}