How do I add to the order activity timeline?
To add something to the order activity timeline you must create a class that extends \Aero\Admin\Activity:
class OrderFlagsEditActivity extends \Aero\Admin\Activity
{
protected $view = 'admin::activity.edit-flags';
protected $key = 'order-flags-activity';
protected $name = 'Order Flag Edits';
protected $flags;
public static function create(\Carbon\Carbon $time, ?\Aero\Admin\Models\Admin $admin, \Illuminate\Support\Collection $flags): self
{
return (new self($time))->admin($admin)->flags($flags);
}
public function flags(\Illuminate\Support\Collection $flags): self
{
$this->flags = $flags;
return $this;
}
public function viewData(): array
{
$name = $this->getAdminName();
return [
'id' => Str::random(),
'time' => $this->getTime(),
'title' => $this->getTitle($name),
'admin' => $name,
'changes' => $this->getChanges(),
];
}
protected function getTitle($adminName): string
{
return $adminName ? "{$adminName} edited this order's flags" : "This order's flags were edited";
}
protected function getChanges()
{
return $this->flags->groupBy('state');
}
}
And a view to handle rendering the activity:
<div class="w-full [ aero-accordion ]">
<input type="checkbox" id="{{ $id }}Open" aria-hidden="true">
<div class="flex items-center">
<label class="aero-accordion-label aero-accordion-label--inline justify-start flex-1" for="{{ $id }}Open" aria-hidden="true">
<div class="flex items-center">
<div class="flex-1">
<div class="timeline-title">
{{ $title }}
</div>
</div>
</div>
</label>
@include('admin::activity.partials.time', compact('time'))
</div>
<div class="visually-hidden">
<div>
{{ $title }}
</div>
</div>
<div class="[ aero-accordion-content ]">
<div class="pl-6">
@foreach($changes as $state => $stateChanges)
<div class="py-2">
<div class="font-medium mb-2">{{ ucfirst($state) }}</div>
@foreach($stateChanges as $change)
<div class="flex items-center mt-1">
•
@include('admin::icons.flag', [
'color' => $change->flag->color
])
@if($state === \Aero\Admin\Models\FlagHistory::ADDED)
<span> {{ $change->flag->name }} @if($change->new_value)({{ $change->new_value }})@endif</span>
@elseif($state === \Aero\Admin\Models\FlagHistory::UPDATED)
<span> {{ $change->flag->name }} ({{ $change->old_value ?? view('admin::resource-lists.placeholder') }} => {{ $change->new_value }})</span>
@else
<span> {{ $change->flag->name }} @if($change->old_value)({{ $change->old_value }})@endif</span>
@endif
</div>
@endforeach
</div>
@endforeach
</div>
</div>
</div>
Finally, you must extend \Aero\Cart\Models\Order to add this activity:
\Aero\Cart\Models\Order::extendActivity(function (\Aero\Cart\Models\Order $order) {
$order->loadMissing('flagHistoryGroups.admin', 'flagHistoryGroups.histories.flag')->flagHistoryGroups->each(function (\Aero\Admin\Models\FlagHistoryGroup $group) use ($order) {
$order->addActivity(\Aero\Admin\Activities\OrderFlagsEditActivity::create(
$group->created_at,
$group->admin,
$group->histories
));
});
});