Files
Toiminnanohjaus/app/Modules/Notifications/Presentation/Livewire/⚡bell.blade.php
T
petriandClaude Sonnet 5.5 f906f9a43f Add notifications: header bell, task start run and assignment notices
A new Notifications module shows a bell beside the profile menu with the
number of unseen notifications and a panel of the last 7 days; opening
it marks them seen, and each notification links to its job or task.

- A daily run at 14.00 (notifications:task-starts) tells the assignee
  and members of tasks that start by the next working day, skipping
  holidays and days off, done tasks and archived jobs.
- Jobs publishes PeopleAddedToTask and PeopleAddedToJob when someone
  else makes a user a task's assignee or member or gives them a role on
  a job; those users are notified at once. UpdateTask and ScheduleTask
  now take the acting user.
- A notification link to #task-<id> opens that task card on the job page.

Also: simplifying job fields per service unit (K31) moves to the end of
phase 4, and the workflow template browser test waits for Livewire's
progress bar before its last accessibility check.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 08:28:01 +02:00

194 lines
8.0 KiB
PHP

<?php
use App\Modules\Notifications\Application\Contracts\NotificationQueries;
use App\Modules\Notifications\Application\DTOs\NotificationSummary;
use App\Modules\Notifications\Application\UseCases\MarkNotificationsSeen;
use App\Modules\Notifications\Application\UseCases\NotifyTasksStartingSoon;
use App\Modules\Notifications\Domain\Enums\NotificationType;
use Carbon\CarbonImmutable;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* The signed-in user's notifications in the header: a bell with the number
* of unseen ones and a panel with those of the last 7 days. Opening the
* panel marks them seen; those that were new are marked "Uusi" until the
* page is left.
*/
new class extends Component
{
/**
* Notifications that were unseen when the panel was opened.
*
* @var list<int>
*/
#[Locked]
public array $newIds = [];
/**
* @return list<NotificationSummary>
*/
#[Computed]
public function notifications(): array
{
return app(NotificationQueries::class)->recentFor((int) auth()->id(), $this->since());
}
#[Computed]
public function unseenCount(): int
{
return app(NotificationQueries::class)->unseenCount((int) auth()->id(), $this->since());
}
public function open(MarkNotificationsSeen $markSeen): void
{
$unseen = array_values(array_map(
fn (NotificationSummary $notification): int => $notification->id,
array_filter($this->notifications, fn (NotificationSummary $notification): bool => ! $notification->seen),
));
if ($unseen === []) {
return;
}
$this->newIds = array_values(array_unique([...$this->newIds, ...$unseen]));
$markSeen->execute((int) auth()->id());
unset($this->notifications, $this->unseenCount);
}
public function text(NotificationSummary $notification): string
{
return match ($notification->type) {
NotificationType::TaskStartsSoon => __('notifications.types.task_starts_soon', [
'task' => (string) $notification->params['task_title'],
'number' => (string) $notification->params['job_number'],
'job' => (string) $notification->params['job_name'],
'date' => $this->formatDay((string) $notification->params['starts_on']),
]),
NotificationType::TaskAssigned => __('notifications.types.task_assigned.'.$notification->params['role'], [
'task' => (string) $notification->params['task_title'],
'number' => (string) $notification->params['job_number'],
'job' => (string) $notification->params['job_name'],
]),
NotificationType::JobAssigned => __('notifications.types.job_assigned', [
'number' => (string) $notification->params['job_number'],
'job' => (string) $notification->params['job_name'],
'roles' => $this->roleList((string) $notification->params['roles']),
]),
};
}
public function url(NotificationSummary $notification): string
{
return match ($notification->type) {
NotificationType::TaskStartsSoon, NotificationType::TaskAssigned => route('jobs.show', (int) $notification->params['job_id']).'#task-'.$notification->params['task_id'],
NotificationType::JobAssigned => route('jobs.show', (int) $notification->params['job_id']),
};
}
/**
* "vastuuhenkilöksi ja jäseneksi" from "responsible,member".
*/
private function roleList(string $roles): string
{
$names = array_map(fn (string $role): string => __('notifications.job_roles.'.$role), array_filter(explode(',', $roles)));
$last = array_pop($names);
return $names === [] ? (string) $last : implode(', ', $names).' '.__('notifications.and').' '.$last;
}
/**
* "25.11.2026 klo 14.05" from "2026-11-25 14:05".
*/
public function formatTime(string $moment): string
{
$time = CarbonImmutable::createFromFormat('Y-m-d H:i', $moment);
return __('notifications.at', ['date' => $time->format('j.n.Y'), 'time' => $time->format('H.i')]);
}
/**
* "ma 30.11.2026" from "2026-11-30".
*/
private function formatDay(string $date): string
{
$day = CarbonImmutable::createFromFormat('Y-m-d', $date);
return __('holidays.weekdays.'.$day->dayOfWeekIso).' '.$day->format('j.n.Y');
}
private function since(): DateTimeImmutable
{
return CarbonImmutable::now()->subDays(NotifyTasksStartingSoon::KEEP_DAYS)->toDateTimeImmutable();
}
};
?>
{{--
A disclosure like the profile menu (not role="menu"): the bell's name stays "Ilmoitukset", and the number of
unseen notifications is visible text that the button refers to with aria-describedby. Opening the panel marks
them seen. Escape, a click outside and moving focus out close it.
--}}
<div
class="relative"
x-data="{ open: false }"
x-on:keydown.escape="if (open) { open = false; $refs.trigger.focus() }"
x-on:click.outside="open = false"
x-on:focusout="if (open && $event.relatedTarget && ! $el.contains($event.relatedTarget)) open = false"
wire:poll.300s.visible
>
@php($count = $this->unseenCount)
<button
type="button"
x-ref="trigger"
x-on:click="open = ! open; if (open) $wire.open()"
x-bind:aria-expanded="open.toString()"
aria-expanded="false"
aria-controls="notifications-panel"
aria-describedby="notifications-count"
data-notifications-bell
class="relative flex min-h-9 items-center gap-1 rounded-md px-3 py-2 text-sm font-medium text-zinc-200 hover:bg-zinc-700 hover:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-400"
>
<x-icon name="bell" class="size-5" aria-hidden="true" />
<span class="sr-only">{{ __('notifications.title') }}</span>
@if ($count > 0)
<span aria-hidden="true" class="min-w-5 rounded-full bg-red-700 px-1.5 text-center text-xs font-bold leading-5 text-white">{{ $count > 99 ? '99+' : $count }}</span>
@endif
</button>
<span id="notifications-count" class="sr-only">{{ trans_choice('notifications.unseen', $count, ['count' => $count]) }}</span>
<div
id="notifications-panel"
x-show="open"
x-cloak
class="z-40 mt-1 w-96 max-w-[calc(100vw-2rem)] rounded-md border border-zinc-600 bg-zinc-800 p-4 text-sm text-zinc-100 shadow-lg sm:absolute sm:end-0"
>
{{-- Not a heading: the header comes before the page's h1. --}}
<p id="notifications-panel-title" class="mb-2 font-semibold">{{ __('notifications.recent') }}</p>
@if ($this->notifications === [])
<p class="text-zinc-300">{{ __('notifications.empty') }}</p>
@else
<ul aria-labelledby="notifications-panel-title" class="max-h-[60vh] space-y-1 overflow-y-auto">
@foreach ($this->notifications as $notification)
<li wire:key="notification-{{ $notification->id }}">
<a
href="{{ $this->url($notification) }}"
wire:navigate
class="block rounded-md px-2 py-2 hover:bg-zinc-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-400"
>
@if (in_array($notification->id, $newIds, true))
<span class="me-1 rounded bg-zinc-100 px-1.5 py-0.5 text-xs font-semibold text-zinc-900">{{ __('notifications.new') }}</span>
@endif
<span class="break-words">{{ $this->text($notification) }}</span>
<span class="mt-0.5 block text-xs text-zinc-300">{{ $this->formatTime($notification->createdAt) }}</span>
</a>
</li>
@endforeach
</ul>
@endif
</div>
</div>