Skip to content

Commit

Permalink
fix detail entangle when using cache (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev authored Sep 26, 2023
1 parent f7c423e commit 0d8b330
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
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

0 comments on commit 0d8b330

Please sign in to comment.