Skip to content

fix: [LAR-130] relation on ArticleForm #250

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

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
81 changes: 42 additions & 39 deletions app/Livewire/Components/Slideovers/ArticleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\HtmlString;
Expand All @@ -37,17 +36,15 @@ final class ArticleForm extends SlideOverComponent implements HasForms

public function mount(?int $articleId = null): void
{
/** @var Article $article */
$article = $articleId
// @phpstan-ignore-next-line
$this->article = $articleId
? Article::query()->findOrFail($articleId)
: new Article;

$this->form->fill(array_merge($article->toArray(), [
'is_draft' => ! $article->published_at,
'published_at' => $article->published_at,
$this->form->fill(array_merge($this->article->toArray(), [
'is_draft' => ! $this->article->published_at, // @phpstan-ignore-line
'published_at' => $this->article->published_at, // @phpstan-ignore-line
]));

$this->article = $article;
}

public static function panelMaxWidth(): string
Expand Down Expand Up @@ -78,55 +75,61 @@ public function form(Form $form): Form
->live(onBlur: true)
->afterStateUpdated(fn ($state, Forms\Set $set) => $set('slug', Str::slug($state))),
Forms\Components\Hidden::make('slug'),
Forms\Components\TextInput::make('canonical_url')
->label(__('pages/article.form.canonical_url'))
->helperText(__('pages/article.canonical_help')),
Forms\Components\Grid::make()
->schema([
Forms\Components\Toggle::make('is_draft')
->label(__('pages/article.form.draft'))
->live()
->offIcon('untitledui-check')
->onColor('success')
->onIcon('untitledui-pencil-line')
->helperText(__('pages/article.draft_help')),
Forms\Components\DatePicker::make('published_at')
->label(__('pages/article.form.published_at'))
->minDate(now())
->prefixIcon('untitledui-calendar-date')
->native(false)
->visible(fn (Forms\Get $get): bool => $get('is_draft') === false)
->required(fn (Forms\Get $get): bool => $get('is_draft') === false),
]),
])
->columnSpan(2),
Forms\Components\Group::make()
->schema([
Forms\Components\SpatieMediaLibraryFileUpload::make('media')
->collection('media')
->label(__('pages/article.form.cover'))
->maxSize(1024),
Forms\Components\Select::make('tags')
->label(__('Tags'))
->multiple()
->relationship(
name: 'tags',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query): Builder => $query->whereJsonContains('concerns', ['post'])
modifyQueryUsing: fn ($query) => $query->whereJsonContains('concerns', 'post')
)
->searchable()
->preload()
->multiple()
->required()
->minItems(1)
->maxItems(3),
])
->columnSpan(1),
Forms\Components\Group::make()
->schema([
Forms\Components\MarkdownEditor::make('body')
->label(__('validation.attributes.content'))
->fileAttachmentsDisk('public')
->minLength(10)
->minHeight('20.25rem')
->maxHeight('32.75rem')
->maxHeight('20.25rem')
->required(),
Forms\Components\Placeholder::make('')
->content(fn () => new HtmlString(Blade::render(<<<'Blade'
<x-torchlight />
Blade))),
])
->columnSpan(2),
Forms\Components\Group::make()
->schema([
Forms\Components\SpatieMediaLibraryFileUpload::make('media')
->collection('media')
->label(__('pages/article.form.cover'))
->maxSize(1024),
Forms\Components\Toggle::make('is_draft')
->label(__('pages/article.form.draft'))
->live()
->offIcon('untitledui-check')
->onColor('success')
->onIcon('untitledui-pencil-line')
->helperText(__('pages/article.draft_help')),
Forms\Components\DatePicker::make('published_at')
->label(__('pages/article.form.published_at'))
->minDate(now())
->native(false)
->visible(fn (Forms\Get $get): bool => $get('is_draft') === false)
->required(fn (Forms\Get $get): bool => $get('is_draft') === false),
Forms\Components\TextInput::make('canonical_url')
->label(__('pages/article.form.canonical_url'))
->helperText(__('pages/article.canonical_help')),
])
->columnSpan(1),
->columnSpanFull(),
])
->columns(3)
->statePath('data')
Expand Down
5 changes: 3 additions & 2 deletions app/Livewire/Pages/Account/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public function mount(User $user): void
public function render(): View
{
return view('livewire.pages.account.profile', [
'articles' => $this->user->articles()
->scopes(['recent', 'published'])
'articles' => $this->user->articles() // @phpstan-ignore-line
->recent()
->published()
->limit(5)
->get(),
'threads' => $this->user->threads()
Expand Down
12 changes: 1 addition & 11 deletions app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use App\Traits\RecordsActivity;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -42,6 +41,7 @@
* @property \Illuminate\Support\Carbon | null $sponsored_at
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
* @property \Illuminate\Database\Eloquent\Collection | Tag[] $tags
*/
#[ScopedBy([LocaleScope::class])]
final class Article extends Model implements HasMedia, ReactableInterface, Viewable
Expand Down Expand Up @@ -236,14 +236,4 @@ public function delete(): ?bool

return parent::delete();
}

public function scopePublished(): Builder
{
return self::published();
}

public function scopeRecent(): Builder
{
return self::recent();
}
}
1 change: 0 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function boot(): void
FilamentColor::register([
'primary' => Color::Emerald,
'danger' => Color::Red,
'gray' => Color::Gray,
'info' => Color::Blue,
'success' => Color::Green,
'warning' => Color::Amber,
Expand Down
2 changes: 1 addition & 1 deletion lang/fr/pages/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'published_at' => 'Date de publication',
'canonical_url' => 'URL Canonique',
],
'canonical_help' => 'Modifiez si l\'article a été publié pour la première fois ailleurs (comme sur votre propre blog).',
'canonical_help' => 'Précisez si l\'article a été publié pour la première fois ailleurs (comme sur votre propre blog).',
'draft_help' => 'Mettre en article en brouillon vous donne la possibilité de le modifier plus tard',
'unpublished' => 'Cet article n\'a pas encore été publié.',

Expand Down
Loading