Vouchers

How do I add to the voucher activity timeline?

To add something to the voucher activity timeline you must create a class that extends \Aero\Admin\Activity:

class CommentActivity extends \Aero\Admin\Activities\Activity
{
    protected $comment;

    protected $view = 'admin::activity.comment';

    protected $key = 'comment-activity';

    protected $name = 'Comments';

    public static function create(\Carbon\Carbon $time, \Aero\Admin\Models\Comment $comment, ?\Aero\Admin\Models\Admin $admin = null): self
    {
        return (new self($time))->admin($admin)->comment($comment);
    }

    public function comment(\Aero\Admin\Models\Comment $comment): self
    {
        $this->comment = $comment;

        return $this;
    }

    public function viewData(): array
    {
        return [
            'time' => $this->getTime(),
            'admin' => $this->admin,
            'adminName' => $this->getAdminName(),
            'comment' => $this->comment,
        ];
    }

    protected function adminNameResolverData(): array
    {
        return array_merge(parent::adminNameResolverData(), [
            'comment' => $this->comment,
        ]);
    }
}

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">
                            &bull;&nbsp;
                            @include('admin::icons.flag', [
                                'color' => $change->flag->color
                            ])
                            @if($state === \Aero\Admin\Models\FlagHistory::ADDED)
                                <span>&nbsp;{{ $change->flag->name }} @if($change->new_value)({{ $change->new_value }})@endif</span>
                            @elseif($state === \Aero\Admin\Models\FlagHistory::UPDATED)
                                <span>&nbsp;{{ $change->flag->name }} ({{ $change->old_value ?? view('admin::resource-lists.placeholder') }} => {{ $change->new_value }})</span>
                            @else
                                <span>&nbsp;{{ $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\Voucher to add this activity:

\Aero\Cart\Models\Voucher::extendActivity(function (\Aero\Cart\Models\Voucher $voucher) {
    $voucher->loadMissing('comments.admin');
    
    $voucher->comments->each(function (Comment $comment) use ($voucher) {
        $voucher->addActivity(\Aero\Admin\Activities\CommentActivity::create(
            $comment->created_at,
            $comment,
            $comment->admin
        ));
    });
});