Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/filacms 13 improve author ux of filament form #17

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Kashkin marked this conversation as resolved.
Show resolved Hide resolved
"tabWidth": 4,
"useTabs": false
}
8 changes: 8 additions & 0 deletions resources/views/components/forms/status-badge.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<x-dynamic-component class="inline-block" :component="$getFieldWrapperView()" :field="$field">
<div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
<!-- Interact with the `state` property in Alpine.js -->
<x-filament::badge :color="$getColor()" :text="$getState()">
{{ $getState() }}
</x-filament::badge>
</div>
</x-dynamic-component>
6 changes: 5 additions & 1 deletion src/Events/ContentUpdating.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Portable\FilaCms\Models\AbstractContentModel;

class ContentUpdating
Expand All @@ -20,9 +21,12 @@ class ContentUpdating
public function __construct(public AbstractContentModel $page)
{
$page->updated_user_id = auth()->user() ? auth()->user()->id : $page->created_user_id;
if(!$page->is_draft && is_null($page->publish_at)) {
if (!$page->is_draft && is_null($page->publish_at)) {
$page->publish_at = Carbon::now()->subMinute();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add/adjust tests that cover this functionality

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

if ($page->slug === null) {
$page->slug = Str::slug($page->title);
}
}
}
19 changes: 19 additions & 0 deletions src/Filament/Forms/Components/StatusBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Portable\FilaCms\Filament\Forms\Components;

use Filament\Forms\Components\Field;
use Filament\Support\Concerns\HasBadge;
use Filament\Support\Concerns\HasColor;
use Illuminate\Database\Eloquent\Model;

class StatusBadge extends Field
{

use HasBadge;
use HasColor;

// protected string $view = 'forms.components.status-badge';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope! Fixed.


public const BADGE_VIEW = 'forms.components.status-badge';
}
107 changes: 88 additions & 19 deletions src/Filament/Resources/AbstractContentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@

use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\View;
use Filament\Forms\Form;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
use Portable\FilaCms\Filament\Forms\Components\StatusBadge;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\RelationManagers;
use Portable\FilaCms\Filament\Traits\IsProtectedResource;
Expand Down Expand Up @@ -44,31 +52,92 @@ public static function getEloquentQuery(): Builder
public static function form(Form $form): Form
{
$fields = [
Toggle::make('is_draft')
->label('Draft?')
->offIcon('heroicon-m-eye')
->onIcon('heroicon-m-eye-slash')->columnSpanFull(),
TextInput::make('title')
->required(),
Select::make('author_id')
->label('Author')
->options(Author::all()->pluck('display_name', 'id'))
->searchable(),
DatePicker::make('publish_at')
->label('Publish Date'),
DatePicker::make('expire_at')
->label('Expiry Date'),
static::tiptapEditor(),
Group::make()
->schema([
Tabs::make()
->tabs([
Tabs\Tab::make('Content')
->schema([

TextInput::make('title')
->columnSpanFull()
->required(),
static::tiptapEditor(),
]),
Tabs\Tab::make('Taxonomies')
->schema([
...static::getTaxonomyFields(),
]),
]),
])
->columnSpan(2),
Group::make()
->schema([
Section::make()
->schema([
TextInput::make('slug')
->maxLength(255),
Toggle::make('is_draft')
->label('Draft?')
->offIcon('heroicon-m-eye')
->onIcon('heroicon-m-eye-slash')->columnSpanFull(),
Select::make('author_id')
->label('Author')
->options(Author::all()->pluck('display_name', 'id'))
->searchable(),
View::make('fila-cms::components.hr'),
DatePicker::make('publish_at')
->label('Publish Date')
->live(),
DatePicker::make('expire_at')
->label('Expiry Date'),
])
->columns(1),
Fieldset::make()
->schema([
Placeholder::make('publish_at_view')
->label('Published')
->visible(fn (?Model $record): bool => $record && $record->status === 'Published')
->content(function (?Model $record): string {
return $record->publish_at ?? '?';
}),
Placeholder::make('created_at_view')
->label('Created')
->visible(fn (?Model $record): bool => $record !== null)
->content(function (?Model $record): string {
return $record->created_at ?? '?';
}),
StatusBadge::make('status')
->live()
->badge()
->color(fn (string $state): string => match ($state) {
'Draft' => 'info',
'Pending' => 'warning',
'Published' => 'success',
'Expired' => 'danger',
})
->default('Draft'),

])
->columns(1)
])
->columnSpan(1),
];

TaxonomyResource::where('resource_class', static::class)->get()->each(function (TaxonomyResource $taxonomyResource) use (&$fields) {
return $form->schema($fields)->columns(['lg' => 3]);
}

public static function getTaxonomyFields(): array
{
$taxonomyFields = [];
TaxonomyResource::where('resource_class', static::class)->get()->each(function (TaxonomyResource $taxonomyResource) use (&$taxonomyFields) {
$fieldName = Str::slug(Str::plural($taxonomyResource->taxonomy->name), '_');
$fields[] = CheckboxList::make($fieldName.'_ids')
$taxonomyFields[] = CheckboxList::make($fieldName . '_ids')
->label($taxonomyResource->taxonomy->name)
->options($taxonomyResource->taxonomy->terms->pluck('name', 'id'));
});

return $form->schema($fields);
return $taxonomyFields;
}

public static function tiptapEditor($name = 'contents'): TiptapEditor
Expand All @@ -85,7 +154,7 @@ public static function table(Table $table): Table
return $table
->columns([
TextColumn::make('title')
->description(fn (Page $page): string => substr($page->contents, 0, 50).'...')
->description(fn (Page $page): string => substr($page->contents, 0, 50) . '...')
->sortable(),
TextColumn::make('author.display_name')->label('Author')
->sortable(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
namespace Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages;

use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Str;
use Portable\FilaCms\Filament\Resources\AbstractContentResource;

class CreateAbstractContentResource extends CreateRecord
{
protected static string $resource = AbstractContentResource::class;

protected function mutateFormDataBeforeCreate(array $data): array
{
$data['slug'] = $data['slug'] ? Str::slug($data['slug']) : null;

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Str;
use Portable\FilaCms\Filament\Resources\AbstractContentResource;

class EditAbstractContentResource extends EditRecord
Expand All @@ -16,4 +17,16 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function mutateFormDataBeforeSave(array $data): array
{
$data['slug'] = $data['slug'] ? Str::slug($data['slug']) : null;

return $data;
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('edit', ['record' => $this->record]);
}
}
1 change: 1 addition & 0 deletions views/components/hr.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<hr class="border-0 border-gray-100 dark:border-gray-700 bt-1">
Loading