<?php
namespace JanusHercules\Shared\Presentation\Component;
use App\Entity\Profile\JoboffererProfile;
use App\Entity\Profile\JobseekerProfile;
use App\Entity\User;
use Exception;
use JanusHercules\Shared\Presentation\Service\HeaderPresentationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
#[AsLiveComponent(
'NavigationItem',
'@janus_hercules.shared.presentation/common/navigation_item_live_component.html.twig',
csrf: false,
)]
class NavigationItemLiveComponent extends AbstractController
{
use DefaultActionTrait;
use ComponentToolsTrait;
#[LiveProp]
public ?string $icon = '';
#[LiveProp]
public string $url = '';
#[LiveProp]
public string $label = '';
#[LiveProp(writable: true)]
public ?string $type = '';
public function __construct(
private readonly HeaderPresentationService $headerPresentationService,
) {
}
#[LiveAction]
public function needsPolling(): bool
{
return in_array($this->type, ['RADAR', 'MESSAGE']);
}
/**
* @throws Exception
*/
#[LiveAction]
public function refresh(): void
{
$this->getNumberOfUnreadNotifications();
}
/**
* @throws Exception
*/
public function mount(): void
{
$this->getNumberOfUnreadNotifications();
}
/**
* @throws Exception
*/
public function getNumberOfUnreadNotifications(): int
{
/** @var ?User $user */
$user = $this->getUser();
if (is_null($user)) {
return 0;
}
/** @var ?JobseekerProfile $jobseekerProfile */
$jobseekerProfile = $user->getDefaultJobseekerProfile();
if (!is_null($jobseekerProfile)) {
if ($this->type === 'RADAR') {
return $this->headerPresentationService->getTotalNumberOfUnreadJobradarMatches($jobseekerProfile);
} elseif ($this->type === 'MESSAGE') {
return $this->headerPresentationService->getUnreadMessagesForJobseekerOrJobofferer($jobseekerProfile);
}
}
/** @var ?JoboffererProfile $joboffererProfile */
$joboffererProfile = $user->getDefaultJoboffererProfile();
if (!is_null($joboffererProfile)) {
if ($this->type === 'MESSAGE') {
return $this->headerPresentationService->getUnreadMessagesForJobseekerOrJobofferer($joboffererProfile);
}
}
return 0;
}
}