Skip to content

Commit

Permalink
refactor: refactor Profile component
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiorodriguesroque committed Nov 13, 2024
1 parent 27aec10 commit 9376b9a
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions app/Livewire/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Livewire;

use App\Models\UserSocial;
use Filament\Forms;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use ResourceBundle;
use App\Models\User;
use Filament\Tables;
Expand All @@ -22,22 +25,27 @@
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Actions\Concerns\InteractsWithActions;

/**
* @property Forms\Form $form
*/
class Profile extends Component implements HasForms, HasTable, HasActions
{
use InteractsWithForms, InteractsWithTable, InteractsWithActions;

public $name;
public $email;
public $username;
public $locale;
public $per_page_setting;
public $notification_settings;
public $date_locale;
public string $name;
public string $email;
public string $username;
public string $locale;
public string $per_page_setting;
public string $notification_settings;
public string $date_locale;
public User $user;

public function mount(): void
{
$this->user = auth()->user();
if (auth()->user()) {
$this->user = auth()->user();
}

$this->form->fill([
'name' => $this->user->name,
Expand Down Expand Up @@ -134,7 +142,7 @@ public function submit(): void
->send();
}

public function logout()
public function logout(): RedirectResponse
{
auth()->logout();

Expand Down Expand Up @@ -165,14 +173,19 @@ public function deleteAction(): Action
->email()
->required()
->helperText('Enter your account\'s email address to delete your account')
->in([auth()->user()->email])
->in(function () {
/** @var User $user */
$user = auth()->user();

return [$user->email];
})
])
->action(fn () => $this->delete());
}

public function delete()
public function delete(): RedirectResponse
{
auth()->user()->delete();
auth()->user()?->delete();

auth()->logout();

Expand All @@ -188,16 +201,19 @@ public function getLocalesProperty(): array
->toArray();
}

public function render()
public function render(): View
{
return view('livewire.profile', [
'hasSsoLoginAvailable' => SsoProvider::isEnabled(),
]);
}

protected function getTableQuery(): Builder
/**
* @return Builder<UserSocial>|null
*/
protected function getTableQuery(): Builder|null
{
return auth()->user()->userSocials()->latest()->getQuery();
return auth()->user()?->userSocials()->latest()->getQuery();
}

protected function getTableColumns(): array
Expand All @@ -214,18 +230,23 @@ protected function getTableBulkActions(): array
return [
Tables\Actions\BulkAction::make('delete')
->action(function (Collection $records) {
/** @var UserSocial $record */
foreach ($records as $record) {
$endpoint = config('services.sso.endpoints.revoke') ?? config('services.sso.url') . '/api/oauth/revoke';
$endpoint = config('services.sso.endpoints.revoke')
? config()->string('services.sso.endpoints.revoke')
: config()->string('services.sso.url') . '/api/oauth/revoke';

$client = Http::withToken($record->access_token)->timeout(5);
if ($record->access_token) {
$client = Http::withToken($record->access_token)->timeout(5);

if (config('services.sso.http_verify') === false) {
$client->withoutVerifying();
}
if (config('services.sso.http_verify') === false) {
$client->withoutVerifying();
}

$client->delete($endpoint);
$client->delete($endpoint);

$record->delete();
$record->delete();
}
}
})
->deselectRecordsAfterCompletion()
Expand Down

0 comments on commit 9376b9a

Please sign in to comment.