Skip to content

Commit

Permalink
Display short date in user grid in administrator panel
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Nov 27, 2024
1 parent 1aaaa7e commit 471a6f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/Http/Grids/Adm/UsersGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public function buildGrid()
])
->addColumn('created_at', [
'title' => 'Rejestracja',
'decorators' => [new FormatDateRelative('nigdy')],
'decorators' => [new FormatDateRelative('nigdy', shortDate:true)],
])
->addColumn('visited_at', [
'title' => 'Ostatnia wizyta',
'sortable' => true,
'decorators' => [new FormatDateRelative('nigdy')],
'decorators' => [new FormatDateRelative('nigdy', shortDate:true)],
])
->addColumn('is_active', [
'title' => 'Aktywny',
Expand Down
15 changes: 10 additions & 5 deletions grid/Decorators/FormatDateRelative.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

namespace Boduch\Grid\Decorators;

use Boduch\Grid\Cell;
use Carbon\Carbon;

class FormatDateRelative extends Decorator
{
public function __construct(private string $default)
{
}
public function __construct(private string $default, private bool $shortDate = false) {}

public function decorate(Cell $cell): void
{
Expand All @@ -27,8 +24,16 @@ protected function formatDateTime(string $dateTime): string
$now = Carbon::now();
$diff = $now->diffForHumans($date, syntax:true);
if ($date->isBefore($now)) {
return "$diff temu";
return $this->formatBefore($diff);
}
return "za $diff";
}

private function formatBefore(string $differance): string
{
if ($this->shortDate) {
return $differance;
}
return "$differance temu";
}
}

0 comments on commit 471a6f7

Please sign in to comment.