|
5 | 5 | namespace App\Livewire\Components\Slideovers;
|
6 | 6 |
|
7 | 7 | use App\Actions\Article\CreateArticleAction;
|
8 |
| -use App\Data\CreateArticleData; |
| 8 | +use App\Actions\Article\UpdateArticleAction; |
| 9 | +use App\Data\ArticleData; |
9 | 10 | use App\Exceptions\UnverifiedUserException;
|
10 | 11 | use App\Livewire\Traits\WithAuthenticatedUser;
|
11 | 12 | use App\Models\Article;
|
@@ -44,6 +45,7 @@ public function mount(?int $articleId = null): void
|
44 | 45 | $this->form->fill(array_merge($this->article->toArray(), [
|
45 | 46 | 'is_draft' => ! $this->article->published_at, // @phpstan-ignore-line
|
46 | 47 | 'published_at' => $this->article->published_at, // @phpstan-ignore-line
|
| 48 | + 'locale' => $this->article->locale ?? app()->getLocale(), |
47 | 49 | ]));
|
48 | 50 | }
|
49 | 51 |
|
@@ -114,6 +116,11 @@ public function form(Form $form): Form
|
114 | 116 | ->required()
|
115 | 117 | ->minItems(1)
|
116 | 118 | ->maxItems(3),
|
| 119 | + Forms\Components\ToggleButtons::make('locale') |
| 120 | + ->label(__('validation.attributes.locale')) |
| 121 | + ->options(['en' => 'En', 'fr' => 'Fr']) |
| 122 | + ->helperText(__('global.locale_help')) |
| 123 | + ->grouped(), |
117 | 124 | ])
|
118 | 125 | ->columnSpan(1),
|
119 | 126 | Forms\Components\Group::make()
|
@@ -163,42 +170,47 @@ public function save(): void
|
163 | 170 |
|
164 | 171 | $this->validate();
|
165 | 172 |
|
166 |
| - $validated = $this->form->getState(); |
| 173 | + $state = $this->form->getState(); |
| 174 | + |
| 175 | + $publishedFields = [ |
| 176 | + 'published_at' => data_get($state, 'published_at') |
| 177 | + ? new Carbon(data_get($state, 'published_at')) |
| 178 | + : null, |
| 179 | + 'submitted_at' => data_get($state, 'is_draft') ? null : now(), |
| 180 | + ]; |
167 | 181 |
|
168 | 182 | if ($this->article?->id) {
|
169 |
| - $this->article->update(array_merge($validated, [ |
170 |
| - 'submitted_at' => $validated['is_draft'] ? null : now(), |
171 |
| - ])); |
172 |
| - $this->form->model($this->article)->saveRelationships(); |
173 |
| - $this->article->fresh(); |
| 183 | + $article = app(UpdateArticleAction::class)->execute( |
| 184 | + articleData: ArticleData::from(array_merge($state, $publishedFields)), |
| 185 | + article: $this->article |
| 186 | + ); |
174 | 187 |
|
175 | 188 | Notification::make()
|
176 | 189 | ->title(
|
177 |
| - $this->article->submitted_at |
| 190 | + $article->submitted_at |
178 | 191 | ? __('notifications.article.submitted')
|
179 | 192 | : __('notifications.article.updated'),
|
180 | 193 | )
|
181 | 194 | ->success()
|
182 | 195 | ->send();
|
183 | 196 | } else {
|
184 |
| - $article = app(CreateArticleAction::class)->execute(CreateArticleData::from(array_merge($validated, [ |
185 |
| - 'published_at' => array_key_exists('published_at', $validated) |
186 |
| - ? new Carbon($validated['published_at']) |
187 |
| - : null, |
188 |
| - ]))); |
189 |
| - $this->form->model($article)->saveRelationships(); |
| 197 | + $article = app(CreateArticleAction::class)->execute( |
| 198 | + ArticleData::from(array_merge($state, $publishedFields)) |
| 199 | + ); |
190 | 200 |
|
191 | 201 | Notification::make()
|
192 | 202 | ->title(
|
193 |
| - $validated['is_draft'] === false |
| 203 | + data_get($state, 'is_draft') === false |
194 | 204 | ? __('notifications.article.submitted')
|
195 | 205 | : __('notifications.article.created'),
|
196 | 206 | )
|
197 | 207 | ->success()
|
198 | 208 | ->send();
|
199 | 209 | }
|
200 | 210 |
|
201 |
| - $this->redirect(route('articles.show', ['article' => $article ?? $this->article]), navigate: true); |
| 211 | + $this->form->model($article)->saveRelationships(); |
| 212 | + |
| 213 | + $this->redirect(route('articles.show', ['article' => $article]), navigate: true); |
202 | 214 | }
|
203 | 215 |
|
204 | 216 | public function render(): View
|
|
0 commit comments