|
5 | 5 | namespace App\Livewire\Components\User;
|
6 | 6 |
|
7 | 7 | use App\Models\User;
|
| 8 | +use Filament\Forms; |
| 9 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 10 | +use Filament\Forms\Contracts\HasForms; |
| 11 | +use Filament\Forms\Form; |
| 12 | +use Filament\Notifications\Notification; |
8 | 13 | use Illuminate\Contracts\View\View;
|
9 | 14 | use Illuminate\Support\Facades\Auth;
|
10 | 15 | use Livewire\Attributes\Computed;
|
11 | 16 | use Livewire\Component;
|
12 | 17 |
|
13 | 18 | /**
|
| 19 | + * @property Form $form |
14 | 20 | * @property User $user
|
15 | 21 | */
|
16 |
| -final class Preferences extends Component |
| 22 | +final class Preferences extends Component implements HasForms |
17 | 23 | {
|
18 |
| - public string $theme = 'light'; |
| 24 | + use InteractsWithForms; |
| 25 | + |
| 26 | + public ?array $data = []; |
| 27 | + |
| 28 | + public function mount(): void |
| 29 | + { |
| 30 | + $this->form->fill([ |
| 31 | + 'theme' => $this->user->setting('theme', 'light'), |
| 32 | + 'locale' => $this->user->setting('locale', config('app.locale')), |
| 33 | + ]); |
| 34 | + } |
19 | 35 |
|
20 | 36 | #[Computed]
|
21 | 37 | public function user(): User
|
22 | 38 | {
|
23 | 39 | return Auth::user(); // @phpstan-ignore-line
|
24 | 40 | }
|
25 | 41 |
|
26 |
| - public function mount(): void |
| 42 | + public function form(Form $form): Form |
27 | 43 | {
|
28 |
| - $this->theme = get_current_theme(); |
| 44 | + return $form |
| 45 | + ->schema([ |
| 46 | + Forms\Components\ToggleButtons::make('theme') |
| 47 | + ->label('Theme') |
| 48 | + ->options([ |
| 49 | + 'light' => 'Light', |
| 50 | + 'dark' => 'Dark', |
| 51 | + ]) |
| 52 | + ->icons([ |
| 53 | + 'light' => 'phosphor-sun-duotone', |
| 54 | + 'dark' => 'phosphor-moon-duotone', |
| 55 | + ]) |
| 56 | + ->grouped(), |
| 57 | + Forms\Components\Select::make('locale') |
| 58 | + ->label(__('global.language')) |
| 59 | + ->options([ |
| 60 | + 'fr' => __('global.french'), |
| 61 | + 'en' => __('global.english'), |
| 62 | + ]), |
| 63 | + ]) |
| 64 | + ->statePath('data'); |
29 | 65 | }
|
30 | 66 |
|
31 |
| - public function updatedTheme(string $value): void |
| 67 | + public function save(): void |
32 | 68 | {
|
33 |
| - $this->user->settings(['theme' => $value]); |
| 69 | + $this->validate(); |
| 70 | + |
| 71 | + $this->user->settings($this->form->getState()); |
| 72 | + |
| 73 | + $this->dispatch('theme-changed', get_current_theme()); |
34 | 74 |
|
35 |
| - $this->redirectRoute('settings', navigate: true); |
| 75 | + Notification::make() |
| 76 | + ->success() |
| 77 | + ->title(__('notifications.user.profile_updated')) |
| 78 | + ->duration(3500) |
| 79 | + ->send(); |
36 | 80 | }
|
37 | 81 |
|
38 | 82 | public function render(): View
|
|
0 commit comments