Skip to content

Commit

Permalink
fix entangle detail with cache (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev authored Sep 26, 2023
1 parent 8b46816 commit 38f421c
Show file tree
Hide file tree
Showing 2 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 @@ -340,6 +340,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);

/** @phpstan-ignore-next-line */
$this->totalCurrentPage = method_exists($data, 'items') ? count($data->items()) : $data->count();

Expand Down
30 changes: 1 addition & 29 deletions src/ProcessDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ private function processCollection(mixed $datasource): \Illuminate\Pagination\Le

$paginated = Collection::paginate($results, intval(data_get($this->component->setUp, 'footer.perPage')));
$results = $paginated->setCollection($this->transform($paginated->getCollection()));
}

self::resolveDetailRow($results);
};

return $results;
}
Expand Down Expand Up @@ -124,8 +122,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 @@ -225,30 +221,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

0 comments on commit 38f421c

Please sign in to comment.