@@ -26,11 +26,7 @@ public function bootWithScroll(): void
26
26
public function mountWithScroll (): void
27
27
{
28
28
if ($ this ->items ->isEmpty ()) {
29
- // Make sure to be back on the first page
30
- $ this ->clear ();
31
-
32
- // Fetch the first page
33
- $ this ->fetch ();
29
+ $ this ->fillPageItems ();
34
30
}
35
31
}
36
32
@@ -53,7 +49,7 @@ public function fetch(): void
53
49
54
50
$ this ->nextPage ();
55
51
56
- $ this ->fillPageItems ();
52
+ $ this ->fillCurrentPageItems ();
57
53
}
58
54
59
55
public function clear (): void
@@ -82,17 +78,33 @@ public function onLastPage(): bool
82
78
83
79
protected function fillPageItems (): void
84
80
{
85
- $ range = range (1 , $ this ->getScrollLimit ());
81
+ $ range = range (1 , $ this ->getPageFillLimit ());
86
82
87
83
foreach ($ range as $ page ) {
88
84
$ items = $ this ->getPageItems ($ page );
89
85
90
- if ($ items ->isNotEmpty ()) {
91
- $ this ->models = $ this ->models ->merge ($ items ->all ())->unique ('id ' );
86
+ $ this ->mergePageItems ($ items );
87
+
88
+ Sleep::for (100 )->milliseconds ();
89
+ }
90
+ }
91
+
92
+ protected function fillCurrentPageItems (): void
93
+ {
94
+ $ items = $ this ->getPageItems ();
95
+
96
+ $ this ->mergePageItems ($ items );
97
+ }
92
98
93
- Sleep::for (100 )->milliseconds ();
94
- }
99
+ protected function mergePageItems (Paginator |Collection $ items ): void
100
+ {
101
+ if ($ items ->isEmpty ()) {
102
+ return ;
95
103
}
104
+
105
+ $ this ->models = $ this ->models
106
+ ->merge ($ items ->filter ()->all ())
107
+ ->unique ($ this ->getPageItemKey ());
96
108
}
97
109
98
110
protected function getPageItems (?int $ page = null ): Paginator |LengthAwarePaginator
@@ -101,13 +113,18 @@ protected function getPageItems(?int $page = null): Paginator|LengthAwarePaginat
101
113
102
114
return $ this
103
115
->getQuery ()
104
- ->simplePaginate (perPage: 12 , page: $ page );
116
+ ->simplePaginate (perPage: 16 , page: $ page );
105
117
}
106
118
107
- protected function getScrollLimit (?int $ page = null ): int
119
+ protected function getPageFillLimit (?int $ page = null ): int
108
120
{
109
121
$ page ??= $ this ->getPage () ?? 1 ;
110
122
111
123
return Number::clamp ($ page , min: 1 , max: 12 );
112
124
}
125
+
126
+ protected function getPageItemKey (): ?string
127
+ {
128
+ return 'id ' ;
129
+ }
113
130
}
0 commit comments