Skip to content

Commit a3bca9a

Browse files
committed
remove unique key & add validation to key with type/for & add empty state option on column
1 parent f0f7c41 commit a3bca9a

File tree

8 files changed

+82
-134
lines changed

8 files changed

+82
-134
lines changed

config/filament-types.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@
4848
* If you need to show the navigation menu for the types
4949
*/
5050
"show_navigation" => true,
51+
52+
/**
53+
* Empty State
54+
*
55+
* If type Column is Empty Put This Message
56+
*/
57+
"empty_state" => null,
5158
];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('types', function (Blueprint $table) {
17+
$table->dropUnique('types_key_unique');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('types', function (Blueprint $table) {
29+
$table->unique('key');
30+
});
31+
}
32+
};

resources/lang/ar/messages.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
return [
44
'title' => 'الانواع',
55
'single' => 'نوع',
6+
"exists" => "عذرا هذا النوع موجود بالفعل",
7+
"success" => "تم إنشاء النوع بنجاح",
68
'group' => 'الإعدادات',
79
'create' => 'إنشاء نوع',
810
'empty' => 'غير محدد',

resources/lang/en/messages.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
return [
44
'title' => 'Types',
55
'single' => 'Type',
6+
"exists" => "Sorry This Type is already exists",
7+
"success" => "Type Created Successfully",
68
'group' => 'Settings',
79
'create' => 'Create Type',
810
'empty' => 'Not Selected',

resources/views/columns/type-column.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
$icon = null;
1414
}
1515
@endphp
16+
@if($value || config('filament-types.empty_state'))
1617
<span style="{{ implode([
1718
"background-color: rgba(".$colorRGB[0].", ".$colorRGB[1].", ".$colorRGB[2].", 0.2);",
1819
"color: rgba(".$colorRGB[0].", ".$colorRGB[1].", ".$colorRGB[2].", 1);"
@@ -25,6 +26,7 @@
2526
@endif
2627

2728
<div>
28-
{{ $value }}
29+
{{ $value ?? config('filament-types.empty_state') }}
2930
</div>
3031
</span>
32+
@endif

src/Components/TypeColumn.php

Lines changed: 2 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -4,139 +4,14 @@
44

55
use Filament\Support\Concerns\HasLineClamp;
66
use Filament\Tables\Columns\Column;
7+
use Filament\Tables\Columns\TextColumn;
78
use Filament\Tables\Columns\TextColumn\TextColumnSize;
89
use Filament\Tables\Contracts\HasTable;
910
use Filament\Tables\Columns\Concerns;
1011

11-
class TypeColumn extends Column
12+
class TypeColumn extends TextColumn
1213
{
1314

14-
use Concerns\CanBeCopied;
15-
use Concerns\CanFormatState;
16-
use Concerns\HasColor;
17-
use Concerns\HasDescription;
18-
use Concerns\HasFontFamily;
19-
use Concerns\HasIcon;
20-
use Concerns\HasIconColor;
21-
use Concerns\HasWeight;
22-
use HasLineClamp;
23-
2415
protected string $view = 'filament-types::columns.type-column';
2516

26-
protected bool | Closure $canWrap = false;
27-
28-
protected bool | Closure $isBadge = false;
29-
30-
protected bool | Closure $isBulleted = false;
31-
32-
protected bool | Closure $isListWithLineBreaks = false;
33-
34-
protected int | Closure | null $listLimit = null;
35-
36-
protected TextColumnSize | string | Closure | null $size = null;
37-
38-
protected bool | Closure $isLimitedListExpandable = false;
39-
40-
public function badge(bool | Closure $condition = true): static
41-
{
42-
$this->isBadge = $condition;
43-
44-
return $this;
45-
}
46-
47-
public function bulleted(bool | Closure $condition = true): static
48-
{
49-
$this->isBulleted = $condition;
50-
51-
return $this;
52-
}
53-
54-
public function listWithLineBreaks(bool | Closure $condition = true): static
55-
{
56-
$this->isListWithLineBreaks = $condition;
57-
58-
return $this;
59-
}
60-
61-
public function limitList(int | Closure | null $limit = 3): static
62-
{
63-
$this->listLimit = $limit;
64-
65-
return $this;
66-
}
67-
68-
public function rowIndex(bool $isFromZero = false): static
69-
{
70-
$this->state(static function (HasTable $livewire, stdClass $rowLoop) use ($isFromZero): string {
71-
$rowIndex = $rowLoop->{$isFromZero ? 'index' : 'iteration'};
72-
73-
$recordsPerPage = $livewire->getTableRecordsPerPage();
74-
75-
if (! is_numeric($recordsPerPage)) {
76-
return (string) $rowIndex;
77-
}
78-
79-
return (string) ($rowIndex + ($recordsPerPage * ($livewire->getTablePage() - 1)));
80-
});
81-
82-
return $this;
83-
}
84-
85-
public function wrap(bool | Closure $condition = true): static
86-
{
87-
$this->canWrap = $condition;
88-
89-
return $this;
90-
}
91-
92-
public function size(TextColumnSize | string | Closure | null $size): static
93-
{
94-
$this->size = $size;
95-
96-
return $this;
97-
}
98-
99-
public function getSize(mixed $state): TextColumnSize | string | null
100-
{
101-
return $this->evaluate($this->size, [
102-
'state' => $state,
103-
]);
104-
}
105-
106-
public function canWrap(): bool
107-
{
108-
return (bool) $this->evaluate($this->canWrap);
109-
}
110-
111-
public function isBadge(): bool
112-
{
113-
return (bool) $this->evaluate($this->isBadge);
114-
}
115-
116-
public function isBulleted(): bool
117-
{
118-
return (bool) $this->evaluate($this->isBulleted);
119-
}
120-
121-
public function isListWithLineBreaks(): bool
122-
{
123-
return $this->evaluate($this->isListWithLineBreaks) || $this->isBulleted();
124-
}
125-
126-
public function getListLimit(): ?int
127-
{
128-
return $this->evaluate($this->listLimit);
129-
}
130-
131-
public function expandableLimitedList(bool | Closure $condition = true): static
132-
{
133-
$this->isLimitedListExpandable = $condition;
134-
135-
return $this;
136-
}
137-
138-
public function isLimitedListExpandable(): bool
139-
{
140-
return (bool) $this->evaluate($this->isLimitedListExpandable);
141-
}
14217
}

src/Resources/TypeResource.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,10 @@ public static function form(Form $form): Form
113113
Forms\Components\Select::make('parent_id')
114114
->label(trans('filament-types::messages.form.parent_id'))
115115
->columnSpan(2)
116-
->options(fn(Forms\Get $get) => Type::whereNull('parent_id')
117-
->where('for', $get('for'))
118-
->where('type', $get('type'))
116+
->options(Type::whereNull('parent_id')
119117
->get()
120118
->pluck('name', 'id')
121-
->toArray()
122-
)
119+
->toArray())
123120
->searchable()
124121
->live(),
125122
Forms\Components\TextInput::make('name')

src/Resources/TypeResource/Pages/ListTypes.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace TomatoPHP\FilamentTypes\Resources\TypeResource\Pages;
44

55
use Filament\Actions;
6+
use Filament\Notifications\Notification;
67
use Filament\Resources\Concerns\Translatable;
78
use Filament\Resources\Pages\ManageRecords;
9+
use Illuminate\Validation\ValidationException;
10+
use TomatoPHP\FilamentTypes\Models\Type;
811
use TomatoPHP\FilamentTypes\Resources\TypeResource;
912

1013
class ListTypes extends ManageRecords
@@ -29,7 +32,35 @@ public static function getTranslatableLocales(): array
2932
protected function getHeaderActions(): array
3033
{
3134
return [
32-
Actions\CreateAction::make()->label(trans('filament-types::messages.create')),
35+
Actions\CreateAction::make()
36+
->label(trans('filament-types::messages.create'))
37+
->using(function (array $data) {
38+
$checkExistsType = Type::query()
39+
->where('key', $data['key'])
40+
->where('for', $data['for'])
41+
->where('type', $data['type'])
42+
->first();
43+
44+
if($checkExistsType){
45+
Notification::make()
46+
->title(trans('filament-types::messages.exists'))
47+
->danger()
48+
->send();
49+
return $checkExistsType;
50+
51+
}
52+
else {
53+
$type = Type::create($data);
54+
55+
Notification::make()
56+
->title(trans('filament-types::messages.success'))
57+
->success()
58+
->send();
59+
60+
return $type;
61+
}
62+
})
63+
->successNotification(null),
3364
Actions\LocaleSwitcher::make()
3465
];
3566
}

0 commit comments

Comments
 (0)