needsOnboarding = $tracker === null; if ($tracker !== null) { $this->syncFrom($tracker); } } public function initialise(): void { $this->validate(); $tracker = Tracker::current() ?? Tracker::create([ 'label' => 'Counter', 'unit' => 'units', 'count' => $this->value, ]); $this->syncFrom($tracker); $this->needsOnboarding = false; } public function increment(): void { $tracker = Tracker::current(); if (! $tracker || $tracker->count >= self::MAX_COUNT) { return; } $tracker->increment('count'); $this->syncFrom($tracker->refresh()); } public function edit(): void { $this->value = $this->count; $this->resetValidation(); $this->editing = true; } public function save(): void { $this->validate(); $tracker = Tracker::current(); if (! $tracker) { return; } $tracker->update(['count' => $this->value]); $this->syncFrom($tracker); $this->editing = false; } public function cancel(): void { $this->value = $this->count; $this->resetValidation(); $this->editing = false; } /** * The tracker is the source of truth; the form input always mirrors it. */ private function syncFrom(Tracker $tracker): void { $this->count = $tracker->count; $this->value = $this->count; } public function render(): View { return view('livewire.counter')->layout('layouts.app'); } }