Browse Source

Refactor comments

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

17
assets/controllers/comments_mercure_controller.js

@ -7,7 +7,6 @@ export default class extends Controller {
connect() { connect() {
this._debounceId = null; this._debounceId = null;
this._liveRoot = this._findLiveRoot(); this._liveRoot = this._findLiveRoot();
console.log(this._liveRoot);
// If the live controller isn't ready yet, wait for it. // If the live controller isn't ready yet, wait for it.
if (!this._getLiveController()) { if (!this._getLiveController()) {
@ -36,7 +35,15 @@ export default class extends Controller {
this.eventSource = new EventSource(url.toString()); this.eventSource = new EventSource(url.toString());
this.eventSource.onopen = () => this._debouncedRefresh(50); this.eventSource.onopen = () => this._debouncedRefresh(50);
this.eventSource.onerror = (e) => console.warn("[comments-mercure] EventSource error", e); this.eventSource.onerror = (e) => console.warn("[comments-mercure] EventSource error", e);
this.eventSource.onmessage = () => this._debouncedRefresh(); this.eventSource.onmessage = (event) => {
console.log("[comments-mercure] Received update", event.data);
const live = this._getLiveController();
if (!live) return;
// Send the Mercure payload to the component
// LiveComponent will re-render after the action resolves
live.action('ingest', { payload: event.data });
};
} }
disconnect() { disconnect() {
@ -58,11 +65,7 @@ export default class extends Controller {
_getLiveController() { _getLiveController() {
if (!this._liveRoot) return null; if (!this._liveRoot) return null;
// Try both identifiers return this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live');
return (
this.application.getControllerForElementAndIdentifier(this._liveRoot, 'live') ||
this.application.getControllerForElementAndIdentifier(this._liveRoot, 'symfony--ux-live-component--live')
);
} }
_debouncedRefresh(delay = 150) { _debouncedRefresh(delay = 150) {

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

@ -8,6 +8,8 @@ use App\Service\RedisCacheService;
use Symfony\Component\Messenger\Exception\ExceptionInterface; 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\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp; use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\UX\LiveComponent\DefaultActionTrait;
@ -51,6 +53,23 @@ final class Comments
} }
} }
#[LiveAction]
public function ingest(#[LiveArg('payload')] string $json): void
{
$data = json_decode($json, true) ?: [];
// Validate/normalize as needed
$this->list = $data['comments'] ?? [];
$this->authorsMetadata = $data['profiles'] ?? [];
// If you send these in the event:
$this->zappers = $data['zappers'] ?? [];
$this->zapAmounts = $data['zapAmounts'] ?? [];
$this->commentLinks = $data['commentLinks'] ?? [];
$this->loading = false;
}
/** Expose a view model to the template; keeps all parsing server-side */ /** Expose a view model to the template; keeps all parsing server-side */
public function getPayload(): array public function getPayload(): array
{ {

Loading…
Cancel
Save