Files
eatvio-infra/app/Livewire/Profile/PhotoForm.php
2023-10-04 17:57:09 +03:00

110 lines
2.1 KiB
PHP

<?php
namespace App\Livewire\Profile;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
use Illuminate\Support\Facades\DB;
class PhotoForm extends Component
{
use WithFileUploads;
/**
* The component's state.
*
* @var array
*/
public $state = [];
/**
* The new avatar for the user.
*
* @var mixed
*/
public $photo;
public $isOnline = false;
public $user;
/**
* Prepare the component.
*
* @return void
*/
public function mount()
{
$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;
}
/**
* Update the user's profile information.
*
* @param \Laravel\Fortify\Contracts\UpdatesUserProfileInformation $updater
* @return void
*/
public function updateProfileInformation(UpdatesUserProfileInformation $updater)
{
$this->resetErrorBag();
$updater->update(
Auth::user(),
$this->photo
? array_merge($this->state, ['photo' => $this->photo])
: $this->state
);
$this->dispatch('saved');
}
public function updatedPhoto($newPhoto)
{
(Auth::user())->updateProfilePhoto($newPhoto);
$this->dispatch('saved');
}
/**
* Delete user's profile photo.
*
* @return void
*/
public function deleteProfilePhoto()
{
Auth::user()->deleteProfilePhoto();
$this->dispatch('refresh-navigation-menu');
}
/**
* Get the current user of the application.
*
* @return mixed
*/
public function getUserProperty()
{
return $this->user;
}
public function render()
{
return view('livewire.profile.photo-form');
}
}