|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Livewire\Components\User; |
| 6 | + |
| 7 | +use App\Events\EmailAddressWasChanged; |
| 8 | +use Filament\Forms\Components\FileUpload; |
| 9 | +use Filament\Forms\Components\Section; |
| 10 | +use Filament\Forms\Components\Textarea; |
| 11 | +use Filament\Forms\Components\TextInput; |
| 12 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 13 | +use Filament\Forms\Contracts\HasForms; |
| 14 | +use Filament\Forms\Form; |
| 15 | +use Filament\Notifications\Notification; |
| 16 | +use Illuminate\Contracts\View\View; |
| 17 | +use Illuminate\Support\Facades\Auth; |
| 18 | +use Livewire\Component; |
| 19 | +use Ysfkaya\FilamentPhoneInput\Forms\PhoneInput; |
| 20 | + |
| 21 | +/** |
| 22 | + * @property Form $form |
| 23 | + */ |
| 24 | +final class Profil extends Component implements HasForms |
| 25 | +{ |
| 26 | + use InteractsWithForms; |
| 27 | + |
| 28 | + public ?array $data = []; |
| 29 | + |
| 30 | + public string $emailAddress = ''; |
| 31 | + |
| 32 | + public function mount(): void |
| 33 | + { |
| 34 | + $this->form->fill(Auth::user()->toArray()); // @phpstan-ignore-line |
| 35 | + $this->emailAddress = Auth::user()->email; // @phpstan-ignore-line |
| 36 | + } |
| 37 | + |
| 38 | + public function form(Form $form): Form |
| 39 | + { |
| 40 | + return $form |
| 41 | + ->schema([ |
| 42 | + Section::make(__('pages/account.settings.profile_title')) |
| 43 | + ->description(__('pages/account.settings.profile_description')) |
| 44 | + ->schema([ |
| 45 | + TextInput::make('username') |
| 46 | + ->label(__('validation.attributes.username')) |
| 47 | + ->prefix('laravel.cm/user/') |
| 48 | + ->required(), |
| 49 | + Textarea::make('bio') |
| 50 | + ->label(__('validation.attributes.bio')) |
| 51 | + ->rows(4) |
| 52 | + ->cols(20) |
| 53 | + ->helperText(__('pages/account.settings.bio_description')), |
| 54 | + FileUpload::make('avatar') |
| 55 | + ->label(__('validation.attributes.avatar')) |
| 56 | + ->helperText(__('pages/account.settings.avatar_description')) |
| 57 | + ->image() |
| 58 | + ->maxSize(1024), |
| 59 | + TextInput::make('website') |
| 60 | + ->label(__('validation.attributes.website')) |
| 61 | + ->placeholder('https://www.example.com') |
| 62 | + ->url(), |
| 63 | + ]), |
| 64 | + |
| 65 | + Section::make(__('pages/account.settings.personal_information_title')) |
| 66 | + ->description(__('pages/account.settings.personal_information_description')) |
| 67 | + ->schema([ |
| 68 | + TextInput::make('name') |
| 69 | + ->label(__('validation.attributes.last_name')) |
| 70 | + ->required(), |
| 71 | + TextInput::make('email') |
| 72 | + ->label(__('validation.attributes.email')) |
| 73 | + ->email() |
| 74 | + ->suffixIcon(fn () => (Auth::user()?->hasVerifiedEmail() ? 'heroicon-m-check-circle' : 'heroicon-m-exclamation-triangle')) |
| 75 | + ->suffixIconColor(fn () => (Auth::user()?->hasVerifiedEmail()) ? 'success' : 'warning') |
| 76 | + ->HelperText(fn () => (! Auth::user()?->hasVerifiedEmail()) ? __('pages/account.settings.unverified_mail') |
| 77 | + : '') |
| 78 | + ->required(), |
| 79 | + TextInput::make('location') |
| 80 | + ->label(__('validation.attributes.location')), |
| 81 | + PhoneInput::make('phone_number') |
| 82 | + ->label(__('validation.attributes.phone')), |
| 83 | + ]), |
| 84 | + |
| 85 | + Section::make(__('pages/account.settings.social_network_title')) |
| 86 | + ->description(__('pages/account.settings.social_network_description')) |
| 87 | + ->schema([ |
| 88 | + TextInput::make('twitter_profile') |
| 89 | + ->label(__('Twitter')) |
| 90 | + ->helperText(__('pages/account.settings.twitter_helper_text')) |
| 91 | + ->prefix('twitter.com/'), |
| 92 | + TextInput::make('github_profile') |
| 93 | + ->label(__('GitHub')) |
| 94 | + ->prefix('github.com/'), |
| 95 | + TextInput::make('linkedin_profile') |
| 96 | + ->label(__('LinkedIn')) |
| 97 | + ->prefix('linkedin.com/in/'), |
| 98 | + ]), |
| 99 | + ]) |
| 100 | + ->statePath('data') |
| 101 | + ->model(Auth::user()); |
| 102 | + } |
| 103 | + |
| 104 | + public function updateProfil(): void |
| 105 | + { |
| 106 | + $this->validate(); |
| 107 | + Auth::user()->update($this->form->getState()); // @phpstan-ignore-line |
| 108 | + $user = Auth::user()->refresh(); // @phpstan-ignore-line |
| 109 | + if ($user->email !== $this->emailAddress) { |
| 110 | + $user->email_verified_at = null; |
| 111 | + $user->save(); |
| 112 | + |
| 113 | + event(new EmailAddressWasChanged($user)); |
| 114 | + } |
| 115 | + |
| 116 | + Notification::make() |
| 117 | + ->success() |
| 118 | + ->title(__('notifications.user.profile_updated')) |
| 119 | + ->duration(3500) |
| 120 | + ->send(); |
| 121 | + } |
| 122 | + |
| 123 | + public function render(): View |
| 124 | + { |
| 125 | + return view('livewire.components.user.profil'); |
| 126 | + } |
| 127 | +} |
0 commit comments