Skip to content

Commit 981e0a4

Browse files
authored
Incident Pagination (#55)
1 parent a90eddc commit 981e0a4

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

resources/lang/en.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
":incidents Incidents": ":incidents Incidents",
23
"Admin": "Admin",
34
"Always Hidden": "Always Hidden",
45
"Always collapsed": "Always collapsed",
@@ -14,7 +15,6 @@
1415
"Custom Header HTML": "Custom Header HTML",
1516
"Fixed": "Fixed",
1617
"Guests": "Guests",
17-
":incidents Incidents": ":incidents Incidents",
1818
"Identified": "Identified",
1919
"In Progress": "In Progress",
2020
"Investigating": "Investigating",
@@ -23,16 +23,21 @@
2323
"Major Outage": "Major Outage",
2424
"Metric Type": "Metric Type",
2525
"Month": "Month",
26+
"Newer Incidents": "Newer Incidents",
2627
"No incidents reported.": "No incidents reported.",
28+
"No incidents reported between :from and :to": "No incidents reported between :from and :to",
2729
"Notified Subscribers": "Notified Subscribers",
2830
"Operational": "Operational",
2931
"Partial Outage": "Partial Outage",
32+
"Past Incidents": "Past Incidents",
3033
"Performance Issues": "Performance Issues",
34+
"Previous Incidents": "Previous Incidents",
3135
"Record Update": "Record Update",
3236
"Resources": "Resources",
3337
"Send notifications to subscribers.": "Send notifications to subscribers.",
3438
"Settings": "Settings",
3539
"Setup Cachet": "Setup Cachet",
40+
"Showing :from - :to": "Showing :from - :to",
3641
"Status Page": "Status Page",
3742
"Subscriber": "Subscriber",
3843
"Sum": "Sum",
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
<div class="flex flex-col gap-6">
2-
<div class="border-b py-2 dark:border-zinc-700">
3-
<h2 class="text-2xl font-semibold">Past Incidents</h2>
1+
<div class="flex flex-col gap-8">
2+
<div class="border-b py-2 dark:border-zinc-700 flex justify-between flex-col md:flex-row md:items-center">
3+
<div>
4+
<h2 class="text-2xl font-semibold">{{ __('Past Incidents') }}</h2>
5+
</div>
6+
<div class="text-sm text-zinc-500 dark:text-zinc-400">
7+
{{ __('Showing :from - :to', ['from' => $from, 'to' => $to]) }}
8+
</div>
49
</div>
510

611
<div class="flex flex-col gap-14 w-full">
7-
@foreach ($incidents as $date => $incident)
12+
@forelse ($incidents as $date => $incident)
813
<x-cachet::incident :date="$date" :incidents="$incident" />
9-
@endforeach
14+
@empty
15+
<div class="text-zinc-500 dark:text-zinc-400 text-center">
16+
{{ __('No incidents reported between :from and :to.', ['from' => $from, 'to' => $to]) }}
17+
</div>
18+
@endforelse
19+
</div>
20+
21+
<div class="flex justify-between">
22+
<a href="{{ route('cachet.status-page', ['from' => $nextPeriodFrom]) }}" class="text-zinc-500 dark:text-zinc-400 hover:underline text-sm">
23+
{{ __('Previous Incidents') }}
24+
</a>
25+
26+
@if($canPageForward)
27+
<a href="{{ route('cachet.status-page', ['from' => $nextPeriodTo]) }}" class="text-zinc-500 dark:text-zinc-400 hover:underline text-sm">
28+
{{ __('Newer Incidents') }}
29+
</a>
30+
@endif
1031
</div>
1132
</div>

src/View/Components/Incidents.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cachet\Models\Incident;
66
use Cachet\Settings\AppSettings;
77
use Illuminate\Contracts\View\View;
8+
use Illuminate\Support\Carbon;
89
use Illuminate\Support\Collection;
910
use Illuminate\View\Component;
1011

@@ -17,33 +18,46 @@ public function __construct(private AppSettings $appSettings)
1718

1819
public function render(): View
1920
{
21+
$incidentDays = $this->appSettings->incident_days - 1;
22+
$startDate = Carbon::createFromFormat('Y-m-d', request('from', now()->toDateString()));
23+
$endDate = $startDate->clone()->subDays($incidentDays);
24+
2025
return view('cachet::components.incidents', [
21-
'incidents' => $this->incidents(),
26+
'incidents' => $this->incidents(
27+
$startDate,
28+
$endDate,
29+
$this->appSettings->only_disrupted_days
30+
),
31+
'from' => $startDate->toDateString(),
32+
'to' => $endDate->toDateString(),
33+
'nextPeriodFrom' => $startDate->clone()->subDays($incidentDays + 1)->toDateString(),
34+
'nextPeriodTo' => $startDate->clone()->addDays($incidentDays + 1)->toDateString(),
35+
'canPageForward' => $startDate->clone()->isBefore(now()),
2236
]);
2337
}
2438

25-
private function incidents(): Collection
39+
private function incidents(Carbon $startDate, Carbon $endDate, bool $onlyDisruptedDays = false): Collection
2640
{
27-
$startDate = now()->subDays($this->appSettings->incident_days - 1)->startOfDay();
28-
$endDate = now()->endOfDay();
29-
3041
return Incident::query()
3142
->with([
3243
'components',
3344
'incidentUpdates' => fn ($query) => $query->orderByDesc('created_at'),
3445
])
3546
->where('visible', '>=', ! auth()->check())
36-
->whereBetween('occurred_at', [$startDate->toDateTimeString(), $endDate->toDateTimeString()])
47+
->whereBetween('occurred_at', [
48+
$endDate->startOfDay()->toDateTimeString(),
49+
$startDate->endofDay()->toDateTimeString(),
50+
])
3751
->orderBy('occurred_at', 'desc')
3852
->get()
3953
->groupBy(fn (Incident $incident) => $incident->occurred_at?->toDateString())
4054
->union(
4155
// Back-fill any missing dates...
42-
collect($startDate->toPeriod($endDate))
56+
collect($endDate->toPeriod($startDate))
4357
->keyBy(fn ($period) => $period->toDateString())
4458
->map(fn ($period) => collect())
4559
)
46-
->when($this->appSettings->only_disrupted_days, fn ($collection) => $collection->filter(fn ($incidents) => $incidents->isNotEmpty()))
60+
->when($onlyDisruptedDays, fn ($collection) => $collection->filter(fn ($incidents) => $incidents->isNotEmpty()))
4761
->sortKeysDesc();
4862
}
4963
}

0 commit comments

Comments
 (0)