Skip to content

Commit 800f5b7

Browse files
committed
Next
1 parent fa2e7f5 commit 800f5b7

File tree

5 files changed

+60
-20
lines changed

5 files changed

+60
-20
lines changed

src/Adapters/DefaultKanbanAdapter.php

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Support\Collection;
9+
use Livewire\Wireable;
910
use Relaticle\Flowforge\Contracts\IKanbanAdapter;
1011

11-
class DefaultKanbanAdapter implements IKanbanAdapter
12+
class DefaultKanbanAdapter implements IKanbanAdapter, Wireable
1213
{
1314
/**
1415
* The status field for the model.
@@ -196,28 +197,28 @@ public function createCard(array $attributes): ?Model
196197
{
197198
$modelClass = $this->getModel();
198199
$card = new $modelClass();
199-
200+
200201
// Set status if provided, otherwise use the first status as default
201202
$status = $attributes[$this->getStatusField()] ?? array_key_first($this->getStatusValues());
202203
$card->{$this->getStatusField()} = $status;
203-
204+
204205
// Set title
205206
if (isset($attributes[$this->getTitleAttribute()])) {
206207
$card->{$this->getTitleAttribute()} = $attributes[$this->getTitleAttribute()];
207208
}
208-
209+
209210
// Set description if the attribute exists
210211
if ($this->getDescriptionAttribute() && isset($attributes[$this->getDescriptionAttribute()])) {
211212
$card->{$this->getDescriptionAttribute()} = $attributes[$this->getDescriptionAttribute()];
212213
}
213-
214+
214215
// Set additional card attributes
215216
foreach ($this->getCardAttributes() as $attribute) {
216217
if (isset($attributes[$attribute])) {
217218
$card->{$attribute} = $attributes[$attribute];
218219
}
219220
}
220-
221+
221222
return $card->save() ? $card : null;
222223
}
223224

@@ -234,24 +235,24 @@ public function updateCard(Model $card, array $attributes): bool
234235
if (isset($attributes[$this->getStatusField()])) {
235236
$card->{$this->getStatusField()} = $attributes[$this->getStatusField()];
236237
}
237-
238+
238239
// Update title if provided
239240
if (isset($attributes[$this->getTitleAttribute()])) {
240241
$card->{$this->getTitleAttribute()} = $attributes[$this->getTitleAttribute()];
241242
}
242-
243+
243244
// Update description if provided and the attribute exists
244245
if ($this->getDescriptionAttribute() && isset($attributes[$this->getDescriptionAttribute()])) {
245246
$card->{$this->getDescriptionAttribute()} = $attributes[$this->getDescriptionAttribute()];
246247
}
247-
248+
248249
// Update additional card attributes
249250
foreach ($this->getCardAttributes() as $attribute) {
250251
if (isset($attributes[$attribute])) {
251252
$card->{$attribute} = $attributes[$attribute];
252253
}
253254
}
254-
255+
255256
return $card->save();
256257
}
257258

@@ -265,4 +266,39 @@ public function deleteCard(Model $card): bool
265266
{
266267
return $card->delete();
267268
}
269+
270+
/**
271+
* Convert the adapter to a Livewire-compatible array.
272+
*
273+
* @return array<string, mixed>
274+
*/
275+
public function toLivewire(): array
276+
{
277+
return [
278+
'model' => $this->getModel(),
279+
'statusField' => $this->getStatusField(),
280+
'statusValues' => $this->getStatusValues(),
281+
'titleAttribute' => $this->getTitleAttribute(),
282+
'descriptionAttribute' => $this->getDescriptionAttribute(),
283+
'cardAttributes' => $this->getCardAttributes(),
284+
];
285+
}
286+
287+
/**
288+
* Create a new adapter instance from a Livewire-compatible array.
289+
*
290+
* @param array<string, mixed> $value The Livewire-compatible array
291+
* @return static
292+
*/
293+
public static function fromLivewire($value)
294+
{
295+
return new static(
296+
$value['model'],
297+
$value['statusField'],
298+
$value['statusValues'],
299+
$value['titleAttribute'],
300+
$value['descriptionAttribute'] ?? null,
301+
$value['cardAttributes'] ?? []
302+
);
303+
}
268304
}

src/Contracts/IKanbanAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
/**
99
* Interface for Kanban board adapters.
10-
*
10+
*
1111
* Adapters are responsible for the interaction between the Kanban board and models.
1212
*/
1313
interface IKanbanAdapter
@@ -102,4 +102,4 @@ public function updateCard(Model $card, array $attributes): bool;
102102
* @return bool
103103
*/
104104
public function deleteCard(Model $card): bool;
105-
}
105+
}

src/Filament/Resources/Pages/KanbanBoard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Relaticle\Flowforge\Filament\Resources\Pages;
44

5+
use Exception;
56
use Filament\Resources\Pages\Page;
67
use Relaticle\Flowforge\Contracts\IKanbanAdapter;
78

@@ -131,6 +132,7 @@ public function adapter(IKanbanAdapter $adapter): static
131132
* Get the Kanban adapter.
132133
*
133134
* @return IKanbanAdapter
135+
* @throws Exception
134136
*/
135137
public function getAdapter(): IKanbanAdapter
136138
{
@@ -169,7 +171,7 @@ public function getAdapter(): IKanbanAdapter
169171
return $instance->getKanbanAdapter();
170172
}
171173

172-
throw new \Exception('Model does not use the HasKanbanBoard trait.');
174+
throw new Exception('Model does not use the HasKanbanBoard trait.');
173175
}
174176

175177
/**

src/Livewire/KanbanBoard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Collection;
88
use Illuminate\View\View;
9+
use Livewire\Attributes\Locked;
910
use Livewire\Component;
1011
use Relaticle\Flowforge\Contracts\IKanbanAdapter;
1112

@@ -14,7 +15,8 @@ class KanbanBoard extends Component
1415
/**
1516
* The Kanban board adapter.
1617
*/
17-
protected IKanbanAdapter $adapter;
18+
#[Locked]
19+
public IKanbanAdapter $adapter;
1820

1921
/**
2022
* The Kanban board configuration.

tests/Feature/KanbanBoardTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
class MockTask extends Model
1414
{
1515
use HasKanbanBoard;
16-
16+
1717
protected $fillable = ['title', 'description', 'status', 'priority', 'due_date'];
18-
18+
1919
protected $table = 'mock_tasks';
20-
20+
2121
public $timestamps = false;
22-
22+
2323
public static function getStatusOptions(): array
2424
{
2525
return [
@@ -34,11 +34,11 @@ public static function getStatusOptions(): array
3434
class KanbanBoardTest extends TestCase
3535
{
3636
use RefreshDatabase;
37-
37+
3838
protected function setUp(): void
3939
{
4040
parent::setUp();
41-
41+
4242
// Create the mock_tasks table
4343
$this->app['db']->connection()->getSchemaBuilder()->create('mock_tasks', function ($table) {
4444
$table->id();

0 commit comments

Comments
 (0)