Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Further SpladeBridge improvements #19

Merged
merged 1 commit into from
Dec 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion app/tests/Feature/ComponentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
22 changes: 11 additions & 11 deletions lib/BladeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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",
},
Expand Down
8 changes: 4 additions & 4 deletions src/ComponentSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Http/InvokeComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
13 changes: 11 additions & 2 deletions src/ResolveOnce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +42,7 @@ private function resolve(): array
});
}

public function __invoke(): array
public function __invoke()
{
return $this->resolve();
}
Expand Down
18 changes: 16 additions & 2 deletions src/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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:')) {
Expand Down
Loading