Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix detail entangle when using cache #1179

Merged
merged 1 commit into from
Sep 26, 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
27 changes: 27 additions & 0 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,31 @@ private function renderView(mixed $data): Application|Factory|View
]);
}

private function resolveDetailRow(mixed $results): void
{
if (!isset($this->setUp['detail'])) {
return;
}

$collection = $results;

if (!$results instanceof BaseCollection) {
/** @phpstan-ignore-next-line */
$collection = collect($results->items());
}

/** @phpstan-ignore-next-line */
$collection->each(function ($model) {
$id = strval($model->{$this->primaryKey});

data_set($this->setUp, 'detail', (array) $this->setUp['detail']);

$state = data_get($this->setUp, 'detail.state.' . $id, false);

data_set($this->setUp, 'detail.state.' . $id, $state);
});
}

/**
* @throws Exception|Throwable
*/
Expand All @@ -361,6 +386,8 @@ public function render(): Application|Factory|View

$data = $this->getCachedData();

$this->resolveDetailRow($data);

if (method_exists($this, 'initActions')) {
$this->initActions();

Expand Down
28 changes: 0 additions & 28 deletions src/ProcessDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ private function processCollection(mixed $datasource): \Illuminate\Pagination\Le
$results = $paginated->setCollection($this->transform($paginated->getCollection()));
}

self::resolveDetailRow($results);

return $results;
}

Expand Down Expand Up @@ -126,8 +124,6 @@ private function processModel(EloquentBuilder|MorphToMany|QueryBuilder|BaseColle

$results = $this->applyPerPage($results);

$this->resolveDetailRow($results);

$this->setTotalCount($results);

/** @phpstan-ignore-next-line */
Expand Down Expand Up @@ -234,30 +230,6 @@ private function applyPerPage(EloquentBuilder|QueryBuilder|MorphToMany $results)
return $results->$paginate($results->count());
}

private function resolveDetailRow(Paginator|LengthAwarePaginator|BaseCollection $results): void
{
if (!isset($this->component->setUp['detail'])) {
return;
}

$collection = $results;

if (!$results instanceof BaseCollection) {
$collection = collect($results->items());
}

/** @phpstan-ignore-next-line */
$collection->each(function ($model) {
$id = strval($model->{$this->component->primaryKey});

data_set($this->component->setUp, 'detail', (array) $this->component->setUp['detail']);

$state = data_get($this->component->setUp, 'detail.state.' . $id, false);

data_set($this->component->setUp, 'detail.state.' . $id, $state);
});
}

/**
* @throws \Exception
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Filters/FilterMultiSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
->assertSee('Name 3')
->assertDontSee('Name 2');
})->group('filters')
->with('filter_multi_select_themes_collection', 'themes with array table');
->with('filter_multi_select_themes_collection');

it('properly filter with category_id - Carnes and Peixe selected', function (string $component) {
$multiSelect = Filter::multiSelect('category_name', 'category_id')
Expand Down
Loading