Skip to content

Commit

Permalink
fixing bug in ImageColumn, adding automated action modal resolver and…
Browse files Browse the repository at this point in the history
… adding lazyloading for ChartWidget
  • Loading branch information
matejkrenek committed Jun 25, 2023
1 parent 6b1f50f commit b7d5fa5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Tables/Columns/ImageColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function isCircular(): bool

public function getValue(Model $record)
{
$record = Str::of('avatar')->contains('.') ? Arr::get($record, Str::of('causer.avatar')->beforeLast('.')->toString()) : $record;
$record = Str::of($this->getName())->contains('.') ? Arr::get($record, Str::of($this->getName())->beforeLast('.')->toString()) : $record;

if (array_key_exists('Spatie\MediaLibrary\HasMedia', class_implements($record)) && array_key_exists('Spatie\MediaLibrary\InteractsWithMedia', class_uses($record))) {
return $record->getFirstMediaUrl(Str::of('avatar')->contains('.') ? Str::of($this->getName())->afterLast('avatar')->toString() : $this->getName());
return $record->getFirstMediaUrl(Str::of($this->getName())->contains('.') ? Str::of($this->getName())->afterLast('.')->toString() : $this->getName());
}

return null;
Expand Down
21 changes: 19 additions & 2 deletions src/Tables/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bengr\Admin\Tables;

use Bengr\Admin\Actions\Action;
use Bengr\Admin\Facades\Admin as BengrAdmin;
use Bengr\Admin\Http\Resources\ActionGroupResource;
use Bengr\Admin\Http\Resources\ActionResource;
use Bengr\Admin\Tables\Columns\Column;
Expand Down Expand Up @@ -99,16 +100,32 @@ public function getRecordsInColumns(): SupportCollection

if ($this->getActionOnClick()) {
$actionOnClick = (clone $this->getActionOnClick())->record($record);

if ($actionOnClick->getModalCodeId() && !$actionOnClick->getModalId()) {
$modal = collect(BengrAdmin::getCurrentPage()->getTransformedModals())->first(fn ($modal) => $modal->getCodeId() == $actionOnClick->getModalCodeId());

if ($modal) {
$actionOnClick->modal($modal->getId(), $actionOnClick->getModalEvent());
}
}

$record_in_column->put('actionOnClick', ActionResource::make($actionOnClick));
} else {
$record_in_column->put('actionOnClick', null);
}



$actions = collect($this->getActions())->map(function ($action) {
return clone $action;
})->each(function ($action) use ($record) {

if ($action->getModalCodeId() && !$action->getModalId()) {
$modal = collect(BengrAdmin::getCurrentPage()->getTransformedModals())->first(fn ($modal) => $modal->getCodeId() == $action->getModalCodeId());

if ($modal) {
$action->modal($modal->getId(), $action->getModalEvent());
}
}

$action->record($record);
});

Expand Down
10 changes: 5 additions & 5 deletions src/Widgets/ChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChartWidget extends Widget

protected ?string $type = null;

protected array $labels = [];
protected array | \Closure | null $labels = [];

protected array $datasets = [];

Expand Down Expand Up @@ -46,14 +46,14 @@ public function subheading(?string $subheading = null): self
return $this;
}

public function labels(array $labels): self
public function labels(array | \Closure | null $labels): self
{
$this->labels = $labels;

return $this;
}

public function dataset(string $label, array $data): self
public function dataset(string $label, array | \Closure | null $data): self
{
$this->datasets[] = [
'label' => $label,
Expand All @@ -75,12 +75,12 @@ public function getSubheading(): ?string

public function getLabels(): array
{
return $this->labels;
return $this->evaluate($this->labels);
}

public function getDatasets(): array
{
return $this->datasets;
return collect($this->datasets)->map(fn ($dataset) => ['label' => $dataset['label'], 'data' => $this->evaluate($dataset['data'])])->toArray();
}

public function getType(): ?string
Expand Down

0 comments on commit b7d5fa5

Please sign in to comment.