Skip to content

Commit b06b2f3

Browse files
committed
wip
1 parent 58fb0a0 commit b06b2f3

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/Response.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,10 @@ protected function getMergePropsForRequest(Request $request): Collection
453453

454454
return collect($this->props)
455455
->filter(fn ($prop) => $prop instanceof Mergeable)
456-
->filter(fn ($prop) => $prop->shouldMerge())
457-
->reject(fn ($_, $key) => in_array($key, $resetProps))
458-
->filter(fn ($_, $key) => count($onlyProps) === 0 || in_array($key, $onlyProps))
459-
->reject(fn ($_, $key) => in_array($key, $exceptProps));
456+
->filter(fn (Mergeable $prop) => $prop->shouldMerge())
457+
->reject(fn ($_, string $key) => in_array($key, $resetProps))
458+
->filter(fn ($_, string $key) => count($onlyProps) === 0 || in_array($key, $onlyProps))
459+
->reject(fn ($_, string $key) => in_array($key, $exceptProps));
460460
}
461461

462462
/**
@@ -467,8 +467,8 @@ protected function getMergePropsForRequest(Request $request): Collection
467467
public function resolveScrollProps(Request $request): array
468468
{
469469
$scrollProps = $this->getMergePropsForRequest($request)
470-
->filter(fn ($prop) => $prop instanceof ScrollProp)
471-
->each(fn (ScrollProp $prop) => $prop->setMergeStrategy($request))
470+
->filter(fn (Mergeable $prop) => $prop instanceof ScrollProp)
471+
->each(fn (ScrollProp $prop) => $prop->configureMergeDirection($request))
472472
->mapWithKeys(fn (ScrollProp $prop, string $key) => [$key => $prop->meta()]);
473473

474474
return $scrollProps->isNotEmpty() ? ['scrollProps' => $scrollProps->toArray()] : [];
@@ -483,9 +483,13 @@ public function resolveMergeProps(Request $request): array
483483
{
484484
$mergeProps = $this->getMergePropsForRequest($request);
485485

486+
$mergePropsWithAppendOrPrependPath = $mergeProps
487+
->filter(fn (Mergeable $prop) => $prop->hasAppendPaths() || $prop->hasPrependPaths())
488+
->keys();
489+
486490
$deepMergeProps = $mergeProps
487-
->reject(fn ($prop) => $prop->hasAppendPaths() || $prop->hasPrependPaths())
488-
->filter(fn ($prop) => $prop->shouldDeepMerge())
491+
->reject(fn ($_, string $key) => $mergePropsWithAppendOrPrependPath->contains($key))
492+
->filter(fn (Mergeable $prop) => $prop->shouldDeepMerge())
489493
->keys();
490494

491495
$matchPropsOn = $mergeProps
@@ -498,12 +502,12 @@ public function resolveMergeProps(Request $request): array
498502
->values();
499503

500504
$prependProps = $mergeProps
501-
->reject(fn ($prop) => $prop->hasAppendPaths() || $prop->hasPrependPaths())
505+
->reject(fn ($_, string $key) => $mergePropsWithAppendOrPrependPath->contains($key))
502506
->filter(fn (Mergeable $prop) => ! $prop->shouldAppend() && ! $prop->shouldDeepMerge())
503507
->keys();
504508

505509
$mergeProps = $mergeProps
506-
->reject(fn ($prop) => $prop->hasAppendPaths() || $prop->hasPrependPaths())
510+
->reject(fn ($_, string $key) => $mergePropsWithAppendOrPrependPath->contains($key))
507511
->filter(fn (Mergeable $prop) => $prop->shouldAppend() && ! $prop->shouldDeepMerge())
508512
->keys();
509513

src/ScrollProp.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ public function __construct($value, string $wrapper = 'data', callable|array|nul
6363
}
6464

6565
/**
66-
* Set the merge strategy for the paginated data.
66+
* Configure the merge strategy based on the scroll direction header.
67+
*
68+
* If the "X-Inertia-Scroll-Direction" header is set to "up", the items
69+
* will be prepended; otherwise, it will be appended.
6770
*/
68-
public function setMergeStrategy(?Request $request = null): self
71+
public function configureMergeDirection(?Request $request = null): self
6972
{
7073
$request ??= request();
7174

tests/ScrollPropTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ public function test_can_set_the_merge_strategy_based_on_the_scroll_direction_he
5757

5858
// Test append strategy without header
5959
$appendProp = new ScrollProp($users);
60-
$appendProp->setMergeStrategy();
60+
$appendProp->configureMergeDirection();
6161
$this->assertContains('data', $appendProp->appendPaths());
6262
$this->assertEmpty($appendProp->prependPaths());
6363

6464
// Test append strategy with header set to 'down'
6565
request()->headers->set(Header::SCROLL_DIRECTION, 'down');
6666
$appendProp = new ScrollProp($users);
67-
$appendProp->setMergeStrategy();
67+
$appendProp->configureMergeDirection();
6868
$this->assertContains('data', $appendProp->appendPaths());
6969
$this->assertEmpty($appendProp->prependPaths());
7070

7171
// Test prepend strategy
7272
request()->headers->set(Header::SCROLL_DIRECTION, 'up');
7373
$prependProp = new ScrollProp($users);
74-
$prependProp->setMergeStrategy();
74+
$prependProp->configureMergeDirection();
7575
$this->assertContains('data', $prependProp->prependPaths());
7676
$this->assertEmpty($prependProp->appendPaths());
7777
}

0 commit comments

Comments
 (0)