diff --git a/app/tests/Feature/ComponentControllerTest.php b/app/tests/Feature/ComponentControllerTest.php index 1b3f3e4..866fe16 100644 --- a/app/tests/Feature/ComponentControllerTest.php +++ b/app/tests/Feature/ComponentControllerTest.php @@ -21,7 +21,7 @@ private function dataForComponent(Component $component, array $with = []): array 'template_hash' => md5('hash'), 'original_url' => url('/change-blade-prop'), 'original_verb' => 'GET', - ], $with)); + ], $with), true); } /** @test */ diff --git a/lib/BladeHelpers.js b/lib/BladeHelpers.js index de90b65..df91141 100644 --- a/lib/BladeHelpers.js +++ b/lib/BladeHelpers.js @@ -59,17 +59,17 @@ function asyncComponentMethod(method, componentStateRef) { } async function executeComponentMethod(method, componentStateRef, ...data) { - const vueBridge = componentStateRef.value; + const spladeBridge = componentStateRef.value; const promise = Axios.post( - vueBridge.invoke_url, + spladeBridge.invoke_url, { - instance: vueBridge.instance, - signature: vueBridge.signature, - original_url: vueBridge.original_url, - original_verb: vueBridge.original_verb, - template_hash: vueBridge.template_hash, - props: componentStateRef.value.data, + instance: spladeBridge.instance, + signature: spladeBridge.signature, + original_url: spladeBridge.original_url, + original_verb: spladeBridge.original_verb, + template_hash: spladeBridge.template_hash, + props: spladeBridge.data, method, data, }, @@ -106,12 +106,12 @@ function uuidv4() { } function refreshComponent(componentStateRef, spladeTemplateBus) { - const vueBridge = componentStateRef.value; + const spladeBridge = componentStateRef.value; - const promise = Axios.get(vueBridge.original_url, { + const promise = Axios.get(spladeBridge.original_url, { headers: { "X-Requested-With": "XMLHttpRequest", - "X-Splade-Component-Refresh": `${vueBridge.template_hash}`, + "X-Splade-Component-Refresh": `${spladeBridge.template_hash}`, "X-Splade-Request-Hash": uuidv4(), Accept: "text/html, application/xhtml+xml", }, diff --git a/src/ComponentSerializer.php b/src/ComponentSerializer.php index 3edccbc..3f047d5 100644 --- a/src/ComponentSerializer.php +++ b/src/ComponentSerializer.php @@ -65,13 +65,13 @@ public static function getDataWithSignature(array $data): array /** * Serializes the component to an array with a signature. */ - public function toArray(array $with = []): array + public function toArray(array $with = [], bool $resolveImmediately = false): array { return static::getDataWithSignature( array_merge([ - 'data' => $this->getDataFromProperties(), - 'props' => $this->getPropsFromComponent(), - 'functions' => $this->getFunctionsFromComponentClass(get_class($this->component)), + 'data' => ResolveOnce::make(fn () => $this->getDataFromProperties())->resolveWhen($resolveImmediately), + 'props' => ResolveOnce::make(fn () => $this->getPropsFromComponent())->resolveWhen($resolveImmediately), + 'functions' => ResolveOnce::make(fn () => $this->getFunctionsFromComponentClass(get_class($this->component)))->resolveWhen($resolveImmediately), 'instance' => $this->getSerializedComponent(), 'invoke_url' => route('splade-core.invoke-component'), 'original_url' => null, diff --git a/src/Http/InvokeComponentController.php b/src/Http/InvokeComponentController.php index f03be51..4392712 100644 --- a/src/Http/InvokeComponentController.php +++ b/src/Http/InvokeComponentController.php @@ -57,7 +57,7 @@ public function __invoke(Request $request, ComponentMiddleware $middleware): Jso 'original_url' => $validated['original_url'], 'original_verb' => $validated['original_verb'], 'template_hash' => $validated['template_hash'], - ]); + ], true); return response()->json($data); } diff --git a/src/ResolveOnce.php b/src/ResolveOnce.php index 60fe66c..3ec6ffb 100644 --- a/src/ResolveOnce.php +++ b/src/ResolveOnce.php @@ -21,7 +21,16 @@ public static function make(callable $callback): self return new static($callback); } - private function resolve(): array + public function resolveWhen(bool $value) + { + if ($value) { + return $this->resolve(); + } + + return $this; + } + + private function resolve() { if ($this->resolved) { return $this->result; @@ -33,7 +42,7 @@ private function resolve(): array }); } - public function __invoke(): array + public function __invoke() { return $this->resolve(); } diff --git a/src/View/Factory.php b/src/View/Factory.php index eb6826e..3a7a7b3 100644 --- a/src/View/Factory.php +++ b/src/View/Factory.php @@ -2,12 +2,14 @@ namespace ProtoneMedia\SpladeCore\View; +use Illuminate\Support\Arr; use Illuminate\Support\Js; use Illuminate\Support\Str; use Illuminate\View\Component; use Illuminate\View\ComponentAttributeBag; use Illuminate\View\Factory as BaseFactory; use ProtoneMedia\SpladeCore\AddSpladeToComponentData; +use ProtoneMedia\SpladeCore\ResolveOnce; class Factory extends BaseFactory { @@ -151,12 +153,24 @@ public function renderComponent() $this->pushSpladeTemplate($templateId, $output); foreach (['data', 'props', 'functions'] as $key) { - if (is_callable($spladeBridge[$key])) { + if ($spladeBridge[$key] instanceof ResolveOnce) { $spladeBridge[$key] = $spladeBridge[$key](); } } - $spladeBridgeHtml = Js::from($spladeBridge)->toHtml(); + $spladeBridgeHtml = Js::from(Arr::only($spladeBridge, [ + 'instance', + 'invoke_url', + 'original_url', + 'original_verb', + 'signature', + 'tag', + 'template_hash', + 'data', + // 'props', + // 'functions', + // 'response', + ]))->toHtml(); collect($spladeBridge['props'])->each(function ($specs, $key) use ($attributes) { if (! str_starts_with($key, 'v-bind:')) {