Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrudolph committed May 15, 2024
1 parent 08d92a7 commit 46f8f86
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 21 deletions.
16 changes: 16 additions & 0 deletions resources/views/components/custom-item.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@props([
'dto'
])

<li class="relative flex gap-x-4">
@unless($isLast)
<div class="absolute left-0 top-0 flex w-6 justify-center -bottom-6">
<div class="w-px bg-gray-200"></div>
</div>
@endUnless

<!-- take the array from the DTO and pass it in here. but also look up what the ComponentAttributeBag function is actually called in blade -->
<x-dynamic-component :component="$dto->component" :attributes="new ComponentAttributeBag($dto->props)" />

<x-some-specified-comonent prop1="value1" prop2="value2" />
</li>
16 changes: 11 additions & 5 deletions resources/views/components/feed.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
<div>
<ul role="list" class="space-y-6">
@foreach ($state->getHistory() as $history_item)
<x-verbs::item
:text="$history_item->message"
:time="$history_item->human_time"
:isLast="$loop->last"
/>
@if($history_item->component)
<x-verbs::custom-item
:dto="$history_item"
/>
@else
<x-verbs::item
:text="$history_item->message"
:time="$history_item->humanTime()"
:isLast="$loop->last"
/>
@endif
@endforeach
</ul>
</div>
16 changes: 16 additions & 0 deletions src/States/DTOs/HistoryComponentDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Thunk\VerbsHistory\States\DTOs;
use Thunk\Verbs\SerializedByVerbs;
use Thunk\Verbs\Support\Normalization\NormalizeToPropertiesAndClassName;

class HistoryComponentDto implements SerializedByVerbs
{
use NormalizeToPropertiesAndClassName;

public function __construct(
public string $component,
public array $props,
) {
}
}
24 changes: 24 additions & 0 deletions src/States/DTOs/HistoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Thunk\VerbsHistory\States\DTOs;
use Illuminate\Support\Carbon;
use Thunk\Verbs\SerializedByVerbs;
use Thunk\VerbsHistory\States\DTOs\HistoryComponentDto;
use Thunk\Verbs\Support\Normalization\NormalizeToPropertiesAndClassName;

class HistoryItem implements SerializedByVerbs
{
use NormalizeToPropertiesAndClassName;

public function __construct(
public Carbon $date_time,
public ?HistoryComponentDto $component = null,
public ?string $message = null,
) {
}

public function humanTime()
{
return $this->date_time->diffForHumans();
}
}
4 changes: 3 additions & 1 deletion src/States/Interfaces/ExposesHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Thunk\VerbsHistory\States\Interfaces;

use Thunk\VerbsHistory\States\DTOs\HistoryComponentDto;

interface ExposesHistory
{
public function getHistoryMessage(): array|string;
public function asHistory(): array|string|HistoryComponentDto;
}
48 changes: 33 additions & 15 deletions src/States/Traits/HasHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Thunk\VerbsHistory\States\Traits;

use Thunk\Verbs\Event;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Thunk\Verbs\Event;
use Thunk\VerbsHistory\Facades\History;
use Thunk\VerbsHistory\States\DTOs\HistoryItem;
use Thunk\VerbsHistory\States\DTOs\HistoryComponentDto;
use Thunk\VerbsHistory\States\Interfaces\ExposesHistory;

trait HasHistory
Expand All @@ -22,35 +25,50 @@ public function applyHistoryEvent(ExposesHistory $event)
return $this->history;
}

// really we should be doing the message stuff below here instead
// if we have a message, pass it in. If we have a component, pass it in
array_unshift(
$this->history,
[
'message' => $event->getHistoryMessage(),
'datetime' => now()->toDateTimeString(),
]
new HistoryItem(
date_time: Carbon::now(),
component: $event->asHistory(),
message:
)
);

dump($this->history);
}

public function getHistory(?string $sub_history = null): array
{
return collect($this->history)
->map(
function ($item) use ($sub_history) {
$message = match (gettype($item['message'])) {
'array' => Arr::get($item, "message.$sub_history") ?? Arr::get($item, 'message.default'),
'string' => $item['message'],
};
// $value = match (gettype($item['value'])) {
// 'array' => Arr::get($item, "value.$sub_history") ?? Arr::get($item, 'value.default'),
// 'string' => $item['value'],
// 'object' => $item['value'],
// };

$message = gettype($item['value'] === 'string'
? $item['value']
: null
);

$component = is_a($item['value'], HistoryComponentDto::class)
? $item['value']
: null;

$datetime = Carbon::parse($item['datetime']);

return (object) [
'message' => $message,
'datetime' => $datetime,
'human_time' => $datetime->diffForHumans(),
];
return new HistoryItem(
date_time: $datetime,
message: $message,
component: $component,
);
}
)
->filter(fn ($item) => $item->message)
->filter()
->values()
->toArray();
}
Expand Down

0 comments on commit 46f8f86

Please sign in to comment.