Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/Livewire/Timetable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Livewire\Attributes\On;
use Carbon\Carbon;
use Carbon\CarbonImmutable;

use App\Models\Reservations\ReservableItem;
use App\Models\Reservations\Reservation;

Expand Down Expand Up @@ -148,6 +147,7 @@ class Timetable extends Component
* The last day of the span displayed (inclusive).
*/
public Carbon $lastDay;
public string $firstDayYYYYMMDD;
/**
* The first hour to be displayed in the table, inclusive (defaults to 0).
*/
Expand Down Expand Up @@ -218,7 +218,7 @@ private static function listOfBlocks(ReservableItem $item, CarbonImmutable $from

$currentStart = $from;
$i = 0;
while($i < count($reservations)) {
while ($i < count($reservations)) {
if ($isForReservation) {
$reservation = $reservations[$i];
$blocks[] = new Block(
Expand Down Expand Up @@ -274,7 +274,7 @@ private static function splitBlocks(array $blocks): array
$block = $blocks[$i];

// this has to be modifiable
$splittingPointAfter = Carbon::make($block->getFrom());
$splittingPointAfter = Carbon::make(Carbon::createFromImmutable($block->getFrom()));
if ($block->isFree()) {
$splittingPointAfter->minute = 0;
$splittingPointAfter->addHours(1);
Expand Down Expand Up @@ -326,6 +326,7 @@ public function mount(
} else {
$this->firstDay = Carbon::today();
}
$this->firstDayYYYYMMDD = $this->firstDay->isoFormat('YYYY-MM-DD');
$this->lastDay = $this->firstDay->copy()->addDays($days - 1);

$this->firstHour = $firstHour;
Expand All @@ -350,7 +351,10 @@ public function render()
*/
public function step(int $days): void
{
$this->firstDay->setTimezone(config('app.timezone'));
$this->firstDay->addDays($days);
$this->firstDayYYYYMMDD = $this->firstDay->isoFormat('YYYY-MM-DD');
$this->lastDay->setTimezone(config('app.timezone'));
$this->lastDay->addDays($days);
}

Expand All @@ -362,7 +366,10 @@ public function step(int $days): void
public function firstDayUpdated(string $firstDay): void
{
$oldFirstDay = $this->firstDay;
$this->firstDay->setTimezone(config('app.timezone'));
$this->firstDay = Carbon::make($firstDay);
$this->firstDayYYYYMMDD = $this->firstDay->isoFormat('YYYY-MM-DD');
$this->lastDay->setTimezone(config('app.timezone'));
$this->lastDay->addDays($oldFirstDay->diffInDays($this->firstDay));
}
}
31 changes: 16 additions & 15 deletions resources/views/livewire/timetable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@

@push('scripts')
<script>

document.addEventListener('DOMContentLoaded', function() {
var elem = document.getElementById('firstDay');
M.Datepicker.init(elem, {
format: 'yyyy-mm-dd',
firstDay: 1,
showClearBtn: false,
onClose: () => @this.firstDayUpdated(elem.value)
const firstDay = document.getElementById("firstDay");
firstDay.addEventListener('click', function(event) {
event.target.showPicker();
});
firstDay.addEventListener('change', function(event) {
@this.firstDayUpdated(event.target.value);
});
setTimeout(function(){
firstDay.value = "{{$firstDayYYYYMMDD}}";
}, 50); //FIXME, it is a terrible hack
Comment on lines +20 to +31

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Address the acknowledged setTimeout hack for proper implementation.

I must say, whilst the implementation achieves the desired functionality, the setTimeout hack on lines 29-31 is rather concerning. The explicit "FIXME" comment indicates this is a temporary solution that warrants proper resolution.

The 50ms delay suggests timing issues with Livewire's reactive updates. Consider implementing a more robust solution using Livewire's lifecycle hooks or dispatching custom events.

-        setTimeout(function(){
-            firstDay.value = "{{$firstDayYYYYMMDD}}";
-        }, 50); //FIXME, it is a terrible hack
+        // Wait for Livewire to fully initialize before setting value
+        document.addEventListener('livewire:initialized', function() {
+            firstDay.value = "{{$firstDayYYYYMMDD}}";
+        });

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In resources/views/livewire/timetable.blade.php around lines 20 to 31, replace
the setTimeout hack used to set the firstDay input value with a more reliable
approach by leveraging Livewire's lifecycle hooks or event listeners. Instead of
delaying with setTimeout, use Livewire's hook such as the 'livewire:load' event
or dispatch a custom event from Livewire to set the input value once the
component is fully initialized, ensuring synchronization without arbitrary
delays.


window.stepDays = function(days) {
@this.step(days);
const elem = document.getElementById('firstDay');
let date = new Date(elem.value);
date.setDate(date.getDate() + days);
// HACK: This is the easiest way to get yyyy-mm-dd.
elem.value = date.toISOString().slice(0, 10);
};
});
</script>
@endpush


<div>
{{-- navigation buttons --}}
@if($isPrintVersion)
Expand All @@ -51,10 +51,11 @@
<div class="col s4 left-align">
<x-input.button floating onclick="stepDays({{ -1 * $dayCount }})" icon="chevron_left" />
</div>
<div class="col s4 center-align" wire:ignore>
<input type="text" class="datepicker validate" id="firstDay" value="{{$firstDay->format('Y-m-d')}}"
style="color:#b38f2f; text-decoration: underline; border: none; box-shadow: none;
text-align: center; font-size: 1.2em; cursor: pointer" >
<div class="col s4 center-align">
<input type="date"
style="color:#b38f2f; text-decoration: underline; border: none; box-shadow: none;
text-align: center; font-size: 1.2em; cursor: pointer"
id="firstDay" wire:model="firstDayYYYYMMDD">
</div>
<div class="col s4 right-align">
<x-input.button floating onclick="stepDays({{ $dayCount }})" icon="chevron_right" />
Expand Down