Skip to content

Commit

Permalink
Added a Team Switcher at the top right corner of the portal
Browse files Browse the repository at this point in the history
- Users with more than one team set can switch companies
- Users with only one company will simply have the team display on their header
  • Loading branch information
coolsam726 committed Sep 28, 2023
1 parent 6799a7b commit 9d58df3
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 21 deletions.
2 changes: 1 addition & 1 deletion resources/dist/strathmore-theme.css

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions resources/views/team-switcher.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@if(auth()->user()?->teams?->count())
<x-filament::modal>
<x-slot name="trigger">
<x-filament::button icon="heroicon-s-chevron-double-down" icon-position="after">
{{auth()->user()->team?->name ?? 'No Company Set'}}: SWITCH
</x-filament::button>
</x-slot>

<livewire:savannabits-saas::switch-team/>
</x-filament::modal>
@else
<div class="font-black text-sm">
{{ auth()->user()->team?->name ?: 'No Company Selected' }}
</div>
@endif
3 changes: 1 addition & 2 deletions src/Concerns/Model/HasTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Savannabits\Saas\Concerns\Model;

use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Model;
use Savannabits\Saas\Models\Team;
use Illuminate\Database\Eloquent\Model;
use function Savannabits\Saas\default_team;

trait HasTeam
Expand Down
2 changes: 1 addition & 1 deletion src/Filament/Pages/RegisterTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Savannabits\Saas\Filament\Pages;

use App\Models\Team;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Tenancy\RegisterTenant;
use Illuminate\Auth\Access\AuthorizationException;
use Savannabits\Saas\Models\Team;
use function Filament\authorize;

class RegisterTeam extends RegisterTenant
Expand Down
74 changes: 74 additions & 0 deletions src/Filament/Resources/TeamResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Savannabits\Saas\Filament\Resources;

use Savannabits\Sass\Filament\Resources\TeamResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Savannabits\Saas\Custom\Filament\Columns\ActiveStatusColumn;
use Savannabits\Saas\Models\Team;

class TeamResource extends Resource
{
protected static ?string $model = Team::class;

protected static ?string $navigationIcon = 'heroicon-o-building-office-2';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('code')
->required()
->maxLength(255),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Toggle::make('is_active')
->required(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('code')
->searchable(),
Tables\Columns\TextColumn::make('name')
->searchable(),
ActiveStatusColumn::make(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getPages(): array
{
return [
'index' => \Savannabits\Saas\Filament\Resources\TeamResource\Pages\ManageTeams::route('/'),
];
}
}
19 changes: 19 additions & 0 deletions src/Filament/Resources/TeamResource/Pages/ManageTeams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Savannabits\Saas\Filament\Resources\TeamResource\Pages;

use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;
use Savannabits\Saas\Filament\Resources\TeamResource;

class ManageTeams extends ManageRecords
{
protected static string $resource = TeamResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
64 changes: 64 additions & 0 deletions src/Livewire/SwitchTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Savannabits\Saas\Livewire;

use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Savannabits\Saas\Models\User;

class SwitchTeam extends Component implements HasForms
{
use InteractsWithForms;
use InteractsWithFormActions;
public ?array $data = [];

public function mount(): void
{
$this->form->fill();
}

public function form(Form $form): Form
{
return $form
->schema([
Select::make('team_id')
->label('Select Team/Company')
->options(fn() => filament()->auth()->user()->teams()->pluck('name','id'))
->searchable()
->default(filament()->auth()->user()->team?->id)
->required(),
])
->statePath('data');
}

public function create(): void
{
$data = $this->form->getState();
User::find(Auth::id())->updateQuietly(['team_id' => $data['team_id']]);

$this->redirect(request()->header('Referer'));
}

public function render(): string
{
return <<<'blade'
<div>
<form wire:submit="create">
{{ $this->form }}
<x-filament::button class="mt-4 w-full" icon="heroicon-o-building-office-2" type="submit">
SUBMIT
</x-filament::button>
</form>
<x-filament-actions::modals />
</div>
blade;
}
}
6 changes: 2 additions & 4 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand All @@ -18,15 +17,14 @@
use Laravel\Sanctum\HasApiTokens;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;
use LdapRecord\Laravel\Auth\LdapAuthenticatable;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Translatable\HasTranslations;
use Savannabits\Saas\Concerns\HasArmor;
use Savannabits\Saas\Concerns\Model\HasAuditColumns;
use Savannabits\Saas\Concerns\Model\HasCodeFactory;
use Savannabits\Saas\Concerns\Model\HasDocStatus;
use Savannabits\Saas\Concerns\Model\HasTeam;
use Savannabits\Saas\Support\Utils;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class User extends Authenticatable implements HasAvatar,FilamentUser,HasTenants, HasMedia, LdapAuthenticatable

Check failure on line 29 in src/Models/User.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-stable - ubuntu-latest

Trait "Laravel\Sanctum\HasApiTokens" not found
{
Expand Down
17 changes: 17 additions & 0 deletions src/Policies/TeamPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Savannabits\Saas\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Savannabits\Saas\Concerns\Policy\InheritsStandardPolicy;
use Savannabits\Saas\Filament\Resources\TeamResource;

class TeamPolicy
{
use HandlesAuthorization, InheritsStandardPolicy;

function getResourceClass(): string
{
return TeamResource::class;
}
}
13 changes: 9 additions & 4 deletions src/SaasPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Savannabits\Saas;

use App\Models\Team;
use Filament\Contracts\Plugin;
use Filament\Navigation\NavigationGroup;
use Filament\Panel;
use Filament\Support\Facades\FilamentView;
use Illuminate\Support\Facades\Blade;
use Savannabits\Saas\Filament\Pages\ManageWebserviceSettings;
use Savannabits\Saas\Filament\Pages\RegisterTeam;
use Savannabits\Saas\Filament\Resources\CountryResource;
use Savannabits\Saas\Filament\Resources\CurrencyResource;
use Savannabits\Saas\Filament\Resources\TeamResource;
use Savannabits\Saas\Helpers\Framework;
use Savannabits\Saas\Http\Middleware\RedirectIfInertiaMiddleware;
use Savannabits\Saas\Models\Team;
use Savannabits\Saas\Settings\WebserviceSettings;

class SaasPlugin implements Plugin
{
Expand Down Expand Up @@ -51,6 +52,7 @@ public function register(Panel $panel): void
}
if ($this->shouldRegisterResources()) {
$panel->resources([
TeamResource::class,
CurrencyResource::class,
CountryResource::class,
]);
Expand All @@ -59,7 +61,10 @@ public function register(Panel $panel): void

public function boot(Panel $panel): void
{
//
FilamentView::registerRenderHook(
'panels::user-menu.before',
fn() => view('savannabits-saas::team-switcher')
);
}

public static function make(): static
Expand Down
3 changes: 3 additions & 0 deletions src/SaasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Filament\Support\Facades\FilamentIcon;
use Illuminate\Filesystem\Filesystem;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;
use Savannabits\Saas\Commands\ArmorGenerateCommand;
use Savannabits\Saas\Commands\ArmorSuperAdminCommand;
use Savannabits\Saas\Livewire\SwitchTeam;
use Savannabits\Saas\Providers\SavannabitsServiceProvider;
use Savannabits\Saas\Seeders\AccessDatabaseSeeder;
use Savannabits\Saas\Seeders\FrameworkSeeder;
Expand Down Expand Up @@ -81,6 +83,7 @@ public function packageRegistered(): void

public function packageBooted(): void
{
Livewire::component(static::$viewNamespace.'::switch-team',SwitchTeam::class);
$this->registerConfigs();
// Asset Registration
FilamentAsset::register(
Expand Down
3 changes: 1 addition & 2 deletions src/Seeders/AdminUserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Savannabits\Saas\Seeders;

use Savannabits\Saas\Contracts\DocStatus;
use App\Models\Team;
use App\Models\User;
use Hash;
use Illuminate\Database\Seeder;
use Savannabits\Saas\Models\Team;
use Str;
use Throwable;
class AdminUserSeeder extends Seeder
Expand Down
4 changes: 1 addition & 3 deletions src/Seeders/Framework/TeamTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Savannabits\Saas\Seeders\Framework;

use App\Models\Team;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Auth;
use Savannabits\Saas\Helpers\Access;
use Savannabits\Saas\Models\Team;

class TeamTableSeeder extends Seeder
{
Expand Down
3 changes: 1 addition & 2 deletions src/Seeders/SystemUserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Savannabits\Saas\Seeders;

use Savannabits\Saas\Contracts\DocStatus;
use App\Models\Team;
use App\Models\User;
use Hash;
use Illuminate\Database\Seeder;
use Savannabits\Saas\Models\Team;
use Savannabits\Saas\Seeders\Framework\TeamTableSeeder;
use Throwable;
use function Savannabits\Saas\default_team;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
namespace Savannabits\Saas;

use App\Model\User;
use App\Models\Team;
use Savannabits\Saas\Helpers\Access;
use Savannabits\Saas\Models\Currency;
use Savannabits\Saas\Models\Team;
use App\Model\User;
use Savannabits\Saas\Settings\WebserviceSettings;

if (!function_exists('Savannabits\Saas\team')) {
Expand Down

0 comments on commit 9d58df3

Please sign in to comment.