Browse Source

Refactor comments, yet again

imwald
Nuša Pukšič 3 months ago
parent
commit
ea0b29b7ef
  1. 17
      assets/controllers/comments_mercure_controller.js
  2. 15
      src/Twig/Components/Organisms/Comments.php

17
assets/controllers/comments_mercure_controller.js

@ -8,13 +8,9 @@ export default class extends Controller {
async connect() { async connect() {
// this.element IS the Live root now // this.element IS the Live root now
this.component = await getComponent(this.element); this.component = await getComponent(this.element);
console.log("[comments_mercure] connected to Live Component:", this.component);
// Optional: hook for spinner polish
this.component.on('render:started', () => this._showLoading());
this.component.on('render:finished', () => this._hideLoading());
// Initial render from cache so UI isn’t empty // Initial render from cache so UI isn’t empty
this._showLoading();
await this.component.render(); await this.component.render();
// Subscribe to Mercure and re-render on each ping // Subscribe to Mercure and re-render on each ping
@ -26,8 +22,8 @@ export default class extends Controller {
this.es = new EventSource(url.toString()); this.es = new EventSource(url.toString());
this.es.onmessage = async (msg) => { this.es.onmessage = async (msg) => {
this._showLoading();
this.component.set('payloadJson', msg.data); this.component.set('payloadJson', msg.data);
this.component.action('loadComments', { arg1: msg.data });
await this.component.render(); await this.component.render();
}; };
} }
@ -35,13 +31,4 @@ export default class extends Controller {
disconnect() { disconnect() {
try { this.es?.close(); } catch {} try { this.es?.close(); } catch {}
} }
_showLoading() {
if (this.hasLoadingTarget) this.loadingTarget.style.display = '';
if (this.hasListTarget) this.listTarget.style.opacity = '0.6';
}
_hideLoading() {
if (this.hasLoadingTarget) this.loadingTarget.style.display = 'none';
if (this.hasListTarget) this.listTarget.style.opacity = '';
}
} }

15
src/Twig/Components/Organisms/Comments.php

@ -9,7 +9,6 @@ use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction; use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp; use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\UX\LiveComponent\DefaultActionTrait;
@ -57,18 +56,10 @@ final class Comments
} }
} }
/** Expose a view model to the template; keeps all parsing server-side */ #[LiveAction]
public function getPayload(): array public function loadComments($payload): array
{ {
$data = !empty($this->payloadJson) $data = json_decode($payload);
? (json_decode($this->payloadJson, true) ?: [])
: $this->redisCacheService->getCommentsPayload($this->current) ?? [
'comments' => [],
'profiles' => [],
'zappers' => [],
'zapAmounts' => [],
'commentLinks' => [],
];
// If your handler doesn’t compute zaps/links yet, reuse your helpers here: // If your handler doesn’t compute zaps/links yet, reuse your helpers here:
$this->list = $data['comments']; $this->list = $data['comments'];

Loading…
Cancel
Save