Skip to content

Commit

Permalink
Merge pull request #1534 from moonshine-software/save-query-state
Browse files Browse the repository at this point in the history
fix: SaveQueryState
  • Loading branch information
lee-to authored Feb 12, 2025
2 parents fd537db + 39912e6 commit 1904635
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Laravel/src/Forms/FiltersForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function __invoke(): FormBuilderContract
})
->submit(__('moonshine::ui.search'), ['class' => 'btn-primary'])
->when(
request()->has('filter'),
$resource->getFilterParams() !== [],
fn (FormBuilderContract $form): FormBuilderContract => $form->buttons([
ActionButton::make(
__('moonshine::ui.reset'),
Expand Down
2 changes: 2 additions & 0 deletions src/Laravel/src/Traits/Resource/ResourceModelQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ protected function withFilters(): static
}

$filters->each(function (Field $filter): void {
$filter->onRequestValue(fn(mixed $value) => $value === false ? $filter->getValue() : $value);

if ($filter->getRequestValue() === false) {
return;
}
Expand Down
14 changes: 6 additions & 8 deletions src/Laravel/src/Traits/Resource/ResourceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected function getPaginatorPage(): int
{
$page = $this->paginatorPage ?? (int)$this->getQueryParams()->get('page');

if ($this->isSaveQueryState() && ! $this->getQueryParams()->has('reset')) {
if ($this->isSaveQueryState() && ! moonshineRequest()->has('reset')) {
return (int)data_get(
moonshineCache()->get($this->getQueryCacheKey(), []),
'page',
Expand Down Expand Up @@ -320,7 +320,7 @@ protected function withCachedQueryParams(): static
return $this;
}

if ($this->getQueryParams()->has('reset')) {
if (moonshineRequest()->has('reset')) {
moonshineCache()->forget($this->getQueryCacheKey());

return $this;
Expand All @@ -345,10 +345,8 @@ protected function getQueryCacheKey(): string
protected function withCache(): static
{
if ($this->isSaveQueryState()
&& ! $this->getQueryParams()->hasAny([
...$this->getCachedRequestKeys(),
'reset',
])
&& ! moonshineRequest()->has('reset')
&& ! $this->getQueryParams()->hasAny($this->getCachedRequestKeys())
) {
$this->setQueryParams(
$this->getQueryParams()->merge(
Expand All @@ -369,7 +367,7 @@ public function getFilterParams(): array
{
$default = $this->getQueryParams()->get('filter', []);

if ($this->isSaveQueryState()) {
if ($this->isSaveQueryState() && !moonshineRequest()->has('reset')) {
return data_get(
moonshineCache()->get($this->getQueryCacheKey(), []),
'filter',
Expand All @@ -392,7 +390,7 @@ protected function prepareFilters(): ?Fields

$filters->fill(
$params,
$this->getCaster()->cast($this->getDataInstance()),
$this->getCaster()->cast($params),
);

return $filters;
Expand Down

0 comments on commit 1904635

Please sign in to comment.