Skip to content

Commit

Permalink
Add ability to edit news
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 committed Jul 6, 2024
1 parent 492a027 commit 3c55d78
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions app/Filament/Widgets/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace App\Filament\Widgets;

use App\Events\NewsAdded;
use App\Events\NewsUpdated;
use App\Models\News as NewsModel;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Forms;
use Filament\Support\Enums\ActionSize;
use Filament\Support\Enums\FontWeight;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Support\Facades\Auth;
Expand All @@ -19,58 +20,77 @@ class News extends BaseWidget

protected static ?string $pollingInterval = null;

private function formContent(): array
{
return [
Forms\Components\TextInput::make('subject')
->string()
->required(),
Forms\Components\RichEditor::make('body')
->required(),
Forms\Components\Toggle::make('send_notifications')
->onColor('success')
->onIcon('heroicon-m-check-circle')
->offColor('danger')
->offIcon('heroicon-m-x-circle'),
];
}

public function table(Table $table): Table
{
return $table
->query(
NewsModel::orderBy('created_at', 'desc')
)
->columns([
Tables\Columns\Layout\Grid::make()->schema([
Tables\Columns\Layout\Stack::make([
Tables\Columns\TextColumn::make('subject')
->size(Tables\Columns\TextColumn\TextColumnSize::Large)
->weight(FontWeight::Bold),

Tables\Columns\TextColumn::make('created_at')
->icon('heroicon-m-trash')
->color('danger')
->size(Tables\Columns\TextColumn\TextColumnSize::Small)
->alignEnd()
->formatStateUsing(fn (string $state): string => 'Delete')
->action(
Action::make('delete')
->requiresConfirmation()
->color('danger')
->action(fn (NewsModel $record) => $record->delete())
),

Tables\Columns\TextColumn::make('body')
->columnSpan(2)
->color('gray')
->html(),

Tables\Columns\TextColumn::make('user.name')
->formatStateUsing(fn (NewsModel $record): string => $record->user->name.' - '.$record->created_at->diffForHumans())
->columnSpan(2)
->alignEnd(),
])->columns(2),
->alignEnd()
])
])
->actions([
Tables\Actions\ActionGroup::make([
Tables\Actions\EditAction::make()
->form($this->formContent())
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = Auth::id();

return $data;
})
->after(function (array $data, NewsModel $record) {
if (get_truth_state($data['send_notifications'])) {
event(new NewsUpdated($record));
}
}),

Tables\Actions\DeleteAction::make()
])
])
->headerActions([
Tables\Actions\CreateAction::make('create')
->label('Add News')
->icon('heroicon-o-plus-circle')
->size(ActionSize::Small)
->model(NewsModel::class)
->form([
Forms\Components\TextInput::make('subject')
->string()
->required(),
Forms\Components\RichEditor::make('body')
->required(),
])
->form($this->formContent())
->mutateFormDataUsing(function (array $data): array {
$data['user_id'] = Auth::id();

return $data;
})
->after(function (array $data, NewsModel $record) {
if (get_truth_state($data['send_notifications'])) {
event(new NewsAdded($record));
}
}),
]);
}
Expand Down

0 comments on commit 3c55d78

Please sign in to comment.