Skip to content

Commit

Permalink
add tags to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopolani committed Feb 20, 2024
1 parent 4b8e18d commit 4de0326
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
27 changes: 27 additions & 0 deletions database/migrations/9_add_tags_to_templates_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('templates', function (Blueprint $table) {
$table->json('tags')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('templates', function (Blueprint $table) {
$table->dropColumn('tags');
});
}
};
1 change: 1 addition & 0 deletions src/MailCarrierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function configurePackage(Package $package): void
'6_transform_logs_cc_bcc_array',
'7_add_tries_to_logs_table',
'8_add_tags_metadata_to_logs_table',
'9_add_tags_to_templates_table',
])
->runsMigrations();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @property string $name
* @property string $slug
* @property string $content
* @property array|null $tags
* @property-read \MailCarrier\Models\User|null $user
* @property-read \MailCarrier\Models\Layout|null $layout
*/
Expand All @@ -34,6 +35,7 @@ class Template extends Model
'name',
'slug',
'content',
'tags',
];

/**
Expand All @@ -50,6 +52,7 @@ class Template extends Model
*/
protected $casts = [
'is_locked' => 'boolean',
'tags' => 'array',
];

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Resources/LogResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Filament\Support\Enums\Alignment;
use Filament\Tables;
use Filament\Tables\Actions\Action as TablesAction;
use Filament\Tables\Enums\FiltersLayout;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
Expand Down Expand Up @@ -90,7 +89,7 @@ public static function table(Tables\Table $table): Tables\Table
])
->poll(Config::get('mailcarrier.logs.table_refresh_poll', '5s'))
->recordAction('details')
->filters(static::getTableFilters(), layout: FiltersLayout::Modal)
->filters(static::getTableFilters())
->filtersTriggerAction(
fn (TablesAction $action) => $action
->button()
Expand Down
40 changes: 31 additions & 9 deletions src/Resources/TemplateResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\HtmlString;
use MailCarrier\Actions\Templates\GenerateSlug;
use MailCarrier\Forms\Components\CodeEditor;
use MailCarrier\Models\Layout;
use MailCarrier\Models\Template;
use MailCarrier\Models\User;
use MailCarrier\Resources\TemplateResource\Pages;
use RalphJSmit\Filament\Components\Forms\Timestamps;

Expand Down Expand Up @@ -47,18 +46,14 @@ public static function table(Tables\Table $table): Tables\Table
->searchable()
->sortable(),

Tables\Columns\TextColumn::make('layout')
->label('Layout')
->formatStateUsing(fn (?Layout $state) => $state?->name ?: '-'),

Tables\Columns\TextColumn::make('user')
->label('User')
->formatStateUsing(fn (?User $state) => $state?->getFilamentName() ?: '-'),
Tables\Columns\TextColumn::make('tags')
->badge(),

Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable(),
])
->filters(static::getTableFilters())
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\ReplicateAction::make()
Expand Down Expand Up @@ -101,6 +96,31 @@ public static function getPages(): array
];
}

/**
* Get the table filters.
*/
protected static function getTableFilters(): array
{
return [
Tables\Filters\SelectFilter::make('tags')
->searchable()
->options(
Template::query()
->pluck('tags')
->unique()
->flatten()
->filter()
->mapWithKeys(fn (string $tag) => [$tag => $tag])
)
->query(function (Builder $query, array $data): Builder {
return $query->when(
!is_null($data['value'] ?? null),
fn (Builder $query) => $query->whereJsonContains('tags', $data['value'])
);
}),
];
}

/**
* Get the form content.
*/
Expand Down Expand Up @@ -175,6 +195,8 @@ protected static function getFormSidebar(): Forms\Components\Section
HTML);
}),

Forms\Components\TagsInput::make('tags'),

Forms\Components\Placeholder::make('Separator')
->label('')
->content(new HtmlString('<div class="h-1 border-b border-slate-100 dark:border-slate-700"></div>')),
Expand Down

0 comments on commit 4de0326

Please sign in to comment.