forked from eatvio/eatvio-infra
81 lines
1.8 KiB
PHP
81 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Profile;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use Illuminate\Http\UploadedFile;
|
|
use App\Models\User;
|
|
use Illuminate\View\View;
|
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
|
|
|
class PhotoForm extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public array $state = [];
|
|
|
|
public UploadedFile|string $photo = '';
|
|
|
|
public bool|int $isOnline = false;
|
|
|
|
public User $user;
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->state = $this->user->withoutRelations()->toArray();
|
|
|
|
$lastActivity = data_get(
|
|
DB::table('sessions')
|
|
->select('last_activity')
|
|
->where('user_id', $this->user->id)
|
|
->first(),
|
|
'last_activity',
|
|
0
|
|
);
|
|
|
|
$this->isOnline = now()->unix() - $lastActivity < 150;
|
|
}
|
|
|
|
public function updateProfileInformation(UpdatesUserProfileInformation $updater): void
|
|
{
|
|
$this->resetErrorBag();
|
|
|
|
$updater->update(
|
|
Auth::user(),
|
|
$this->photo
|
|
? array_merge($this->state, ['photo' => $this->photo])
|
|
: $this->state
|
|
);
|
|
|
|
$this->dispatch('saved');
|
|
}
|
|
|
|
public function updatedPhoto(TemporaryUploadedFile $newPhoto): void
|
|
{
|
|
Auth::user()->updateProfilePhoto($newPhoto);
|
|
|
|
$this->dispatch('saved');
|
|
}
|
|
|
|
public function deleteProfilePhoto(): void
|
|
{
|
|
Auth::user()->deleteProfilePhoto();
|
|
|
|
$this->dispatch('refresh-navigation-menu');
|
|
}
|
|
|
|
public function getUserProperty(): User
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.profile.photo-form');
|
|
}
|
|
}
|