Skip to content

Commit feae698

Browse files
authored
fix: [LAR-130] relation on ArticleForm (#250)
1 parent 3d709cb commit feae698

File tree

5 files changed

+47
-54
lines changed

5 files changed

+47
-54
lines changed

app/Livewire/Components/Slideovers/ArticleForm.php

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Filament\Forms\Form;
1717
use Filament\Notifications\Notification;
1818
use Illuminate\Contracts\View\View;
19-
use Illuminate\Database\Eloquent\Builder;
2019
use Illuminate\Support\Facades\Auth;
2120
use Illuminate\Support\Facades\Blade;
2221
use Illuminate\Support\HtmlString;
@@ -37,17 +36,15 @@ final class ArticleForm extends SlideOverComponent implements HasForms
3736

3837
public function mount(?int $articleId = null): void
3938
{
40-
/** @var Article $article */
41-
$article = $articleId
39+
// @phpstan-ignore-next-line
40+
$this->article = $articleId
4241
? Article::query()->findOrFail($articleId)
4342
: new Article;
4443

45-
$this->form->fill(array_merge($article->toArray(), [
46-
'is_draft' => ! $article->published_at,
47-
'published_at' => $article->published_at,
44+
$this->form->fill(array_merge($this->article->toArray(), [
45+
'is_draft' => ! $this->article->published_at, // @phpstan-ignore-line
46+
'published_at' => $this->article->published_at, // @phpstan-ignore-line
4847
]));
49-
50-
$this->article = $article;
5148
}
5249

5350
public static function panelMaxWidth(): string
@@ -78,55 +75,61 @@ public function form(Form $form): Form
7875
->live(onBlur: true)
7976
->afterStateUpdated(fn ($state, Forms\Set $set) => $set('slug', Str::slug($state))),
8077
Forms\Components\Hidden::make('slug'),
78+
Forms\Components\TextInput::make('canonical_url')
79+
->label(__('pages/article.form.canonical_url'))
80+
->helperText(__('pages/article.canonical_help')),
81+
Forms\Components\Grid::make()
82+
->schema([
83+
Forms\Components\Toggle::make('is_draft')
84+
->label(__('pages/article.form.draft'))
85+
->live()
86+
->offIcon('untitledui-check')
87+
->onColor('success')
88+
->onIcon('untitledui-pencil-line')
89+
->helperText(__('pages/article.draft_help')),
90+
Forms\Components\DatePicker::make('published_at')
91+
->label(__('pages/article.form.published_at'))
92+
->minDate(now())
93+
->prefixIcon('untitledui-calendar-date')
94+
->native(false)
95+
->visible(fn (Forms\Get $get): bool => $get('is_draft') === false)
96+
->required(fn (Forms\Get $get): bool => $get('is_draft') === false),
97+
]),
98+
])
99+
->columnSpan(2),
100+
Forms\Components\Group::make()
101+
->schema([
102+
Forms\Components\SpatieMediaLibraryFileUpload::make('media')
103+
->collection('media')
104+
->label(__('pages/article.form.cover'))
105+
->maxSize(1024),
81106
Forms\Components\Select::make('tags')
82-
->label(__('Tags'))
107+
->multiple()
83108
->relationship(
109+
name: 'tags',
84110
titleAttribute: 'name',
85-
modifyQueryUsing: fn (Builder $query): Builder => $query->whereJsonContains('concerns', ['post'])
111+
modifyQueryUsing: fn ($query) => $query->whereJsonContains('concerns', 'post')
86112
)
87-
->searchable()
88113
->preload()
89-
->multiple()
90114
->required()
91115
->minItems(1)
92116
->maxItems(3),
117+
])
118+
->columnSpan(1),
119+
Forms\Components\Group::make()
120+
->schema([
93121
Forms\Components\MarkdownEditor::make('body')
94122
->label(__('validation.attributes.content'))
95123
->fileAttachmentsDisk('public')
96124
->minLength(10)
97-
->minHeight('20.25rem')
98-
->maxHeight('32.75rem')
125+
->maxHeight('20.25rem')
99126
->required(),
100127
Forms\Components\Placeholder::make('')
101128
->content(fn () => new HtmlString(Blade::render(<<<'Blade'
102129
<x-torchlight />
103130
Blade))),
104131
])
105-
->columnSpan(2),
106-
Forms\Components\Group::make()
107-
->schema([
108-
Forms\Components\SpatieMediaLibraryFileUpload::make('media')
109-
->collection('media')
110-
->label(__('pages/article.form.cover'))
111-
->maxSize(1024),
112-
Forms\Components\Toggle::make('is_draft')
113-
->label(__('pages/article.form.draft'))
114-
->live()
115-
->offIcon('untitledui-check')
116-
->onColor('success')
117-
->onIcon('untitledui-pencil-line')
118-
->helperText(__('pages/article.draft_help')),
119-
Forms\Components\DatePicker::make('published_at')
120-
->label(__('pages/article.form.published_at'))
121-
->minDate(now())
122-
->native(false)
123-
->visible(fn (Forms\Get $get): bool => $get('is_draft') === false)
124-
->required(fn (Forms\Get $get): bool => $get('is_draft') === false),
125-
Forms\Components\TextInput::make('canonical_url')
126-
->label(__('pages/article.form.canonical_url'))
127-
->helperText(__('pages/article.canonical_help')),
128-
])
129-
->columnSpan(1),
132+
->columnSpanFull(),
130133
])
131134
->columns(3)
132135
->statePath('data')

app/Livewire/Pages/Account/Profile.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public function mount(User $user): void
2727
public function render(): View
2828
{
2929
return view('livewire.pages.account.profile', [
30-
'articles' => $this->user->articles()
31-
->scopes(['recent', 'published'])
30+
'articles' => $this->user->articles() // @phpstan-ignore-line
31+
->recent()
32+
->published()
3233
->limit(5)
3334
->get(),
3435
'threads' => $this->user->threads()

app/Models/Article.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use App\Traits\RecordsActivity;
1515
use CyrildeWit\EloquentViewable\Contracts\Viewable;
1616
use CyrildeWit\EloquentViewable\InteractsWithViews;
17-
use Illuminate\Contracts\Database\Eloquent\Builder;
1817
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
1918
use Illuminate\Database\Eloquent\Factories\HasFactory;
2019
use Illuminate\Database\Eloquent\Model;
@@ -42,6 +41,7 @@
4241
* @property \Illuminate\Support\Carbon | null $sponsored_at
4342
* @property \Illuminate\Support\Carbon $created_at
4443
* @property \Illuminate\Support\Carbon $updated_at
44+
* @property \Illuminate\Database\Eloquent\Collection | Tag[] $tags
4545
*/
4646
#[ScopedBy([LocaleScope::class])]
4747
final class Article extends Model implements HasMedia, ReactableInterface, Viewable
@@ -236,14 +236,4 @@ public function delete(): ?bool
236236

237237
return parent::delete();
238238
}
239-
240-
public function scopePublished(): Builder
241-
{
242-
return self::published();
243-
}
244-
245-
public function scopeRecent(): Builder
246-
{
247-
return self::recent();
248-
}
249239
}

app/Providers/AppServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function boot(): void
4343
FilamentColor::register([
4444
'primary' => Color::Emerald,
4545
'danger' => Color::Red,
46-
'gray' => Color::Gray,
4746
'info' => Color::Blue,
4847
'success' => Color::Green,
4948
'warning' => Color::Amber,

lang/fr/pages/article.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
'published_at' => 'Date de publication',
2525
'canonical_url' => 'URL Canonique',
2626
],
27-
'canonical_help' => 'Modifiez si l\'article a été publié pour la première fois ailleurs (comme sur votre propre blog).',
27+
'canonical_help' => 'Précisez si l\'article a été publié pour la première fois ailleurs (comme sur votre propre blog).',
2828
'draft_help' => 'Mettre en article en brouillon vous donne la possibilité de le modifier plus tard',
2929
'unpublished' => 'Cet article n\'a pas encore été publié.',
3030

0 commit comments

Comments
 (0)