Skip to content

Commit

Permalink
Merge branch 'senaste-nytt-komponent'
Browse files Browse the repository at this point in the history
  • Loading branch information
bonny committed May 9, 2024
2 parents 33c09bb + b1526d8 commit 947e911
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 41 deletions.
9 changes: 6 additions & 3 deletions app/CrimeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ public function getParsedDateAsCarbon() {
return Carbon::createFromTimestamp(strtotime($date));
}

public function getParsedDateInFormat($dateFormat = '%A %d %B %H:%M') {
/**
* Datum för händelsen i formatet "Lördag 15 December 2018 13:20" eller i valfritt format.
*/
public function getParsedDateInFormat($dateFormat = '%A %d %B %H:%M'): string {
$carbonDate = $this->getParsedDateAsCarbon();
$formattedDate = $carbonDate->formatLocalized($dateFormat);
return $formattedDate;
Expand Down Expand Up @@ -906,7 +909,7 @@ public function getSingleEventTitleEvenShorter() {

$titleParts[] = $this->getDescriptionAsPlainText();

return implode(", ", $titleParts);
return trim(implode(", ", $titleParts));
}

/**
Expand Down Expand Up @@ -1277,7 +1280,7 @@ public function getHeadline(): string {
$headline = $this->getSingleEventTitleEvenShorter();
}

return $headline;
return trim($headline);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions app/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public static function getStatsChartHtml($lan) {
<tr>
<th>' . $a_start . $date_day . $a_end . '</th>
<td style="--size: calc(' . $statRow->count . ' / ' . $maxValue . ')">' .
'<span class="data">' . $statRow->count . '</span>' .
$a_start . $a_end .
'</td>
'<span class="data">' . $statRow->count . '</span>' .
$a_start . $a_end .
'</td>
</tr>
';
}
Expand Down Expand Up @@ -1104,13 +1104,16 @@ public static function getLatestEvents(int $count = 5) {
return $events;
}

public static function getLatestEventsByParsedDate(int $count = 5) {
/**
* Hämta de senaste händelserna enligt händelsedatum.
*/
public static function getLatestEventsByParsedDate(int $count = 5): Collection {
$cacheKey = __METHOD__ . ":{$count}";

// Get date in format 2024-12-31 00:07:00
$date = Carbon::now();
$date = $date->format('Y-m-d H:i:s');

$events = Cache::remember($cacheKey, 2 * 60, function () use ($count, $date) {
$events = CrimeEvent::orderBy("parsed_date", "desc")
->where('parsed_date', '<', $date)
Expand Down
28 changes: 28 additions & 0 deletions app/View/Components/LatestEventsBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\View\Components;

use App\Helper;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class LatestEventsBox extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{

}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
$latestEvents = Helper::getLatestEventsByParsedDate(5);
return view('components.latest-events-box')->with('latestEvents', $latestEvents);
}
}
123 changes: 123 additions & 0 deletions resources/views/components/latest-events-box.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
@once
<style>
.Timeline {
--badge-size: 9px;
background: white;
padding: var(--default-margin);
padding-bottom: var(--default-margin-half);
}
.Timeline-title {
}
.Timeline-items {
list-style: none;
padding: 0;
font-size: 1rem;
}
.Timeline-item {
position: relative;
}
.Timeline-title,
.Timeline-titleLink,
.Timeline-item,
.Timeline-itemTime,
.Timeline-itemLink,
.Timeline-itemTitle {
font-size: inherit;
margin: 0;
line-height: 1;
color: #1f2328;
}
.Timeline-itemTime {
font-size: var(--font-size-small);
}
.Timeline-itemTitle {
text-wrap: pretty;
}
.Timeline-itemLink {
position: relative;
z-index: 5;
display: flex;
color: inherit;
padding-bottom: var(--default-margin);
}
.Timeline-itemLink:hover {
/* background-color: var(--color-grey-light); */
}
.Timeline-itemBadge {
flex: 0 0 auto;
flex-basis: 30px;
}
.Timeline-itemContent {
display: flex;
flex-direction: column;
gap: var(--default-margin-third);
}
.Timeline-itemMoreLink {
font-size: var(--font-size-small);
padding-top: var(--default-margin);
}
.Timeline-item::before {
position: absolute;
z-index: 1;
content: '';
top: 0;
left: calc(var(--badge-size) / 2 + -1px);
bottom: 0;
width: 1px;
background: var(--color-gray-1);
}
.Timeline-itemBadge-circle {
display: block;
position: relative;
z-index: 2;
width: var(--badge-size);
height: var(--badge-size);
border-radius: 50%;
background-color: var(--color-red-2);
}
</style>
@endonce

<div {{ $attributes->merge(['class' => 'Timeline']) }}>
{{ $slot }}

<h2 class="Timeline-title"><a class="Timeline-titleLink" href="{{ route('handelser') }}">Senaste händelserna</a></h2>

<ul class='Timeline-items'>
@foreach ($latestEvents as $crimeEvent)
<li class="Timeline-item">
<a href="{{ $crimeEvent->getPermalink() }}" class="Timeline-itemLink">
<div class="Timeline-itemBadge">
<span class="Timeline-itemBadge-circle"></span>
</div>
<div class="Timeline-itemContent">
<p class="Timeline-itemTime">{{ $crimeEvent->getParsedDateInFormat('%H:%M') }}</p>
<h3 class="Timeline-itemTitle">{{ $crimeEvent->getHeadline() }}</h3>
</div>
</a>
</li>
@endforeach

<li class="Timeline-item">
<a href="{{ route('handelser') }}" class="Timeline-itemLink">
<div class="Timeline-itemBadge"></div>
<div class="Timeline-itemContent">
<o class="Timeline-itemMoreLink">Visa fler händelser →</o>
</div>
</a>
</li>
</ul>
</div>
Loading

0 comments on commit 947e911

Please sign in to comment.