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

feat(librarium): Announcement management page #896

Merged
merged 5 commits into from
Jan 7, 2025
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
198 changes: 198 additions & 0 deletions app/Filament/Resources/AnnouncementResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\AnnouncementResource\Pages;
use App\Models\Announcement;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Carbon;

class AnnouncementResource extends Resource
{
protected static ?string $model = Announcement::class;

protected static ?string $navigationIcon = 'heroicon-o-bell';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make('Announcement Content')
->columns(2)
->schema([
Forms\Components\TextInput::make('title_en')
->label('Title English')
->required()
->maxLength(255),
Forms\Components\TextInput::make('title_fr')
->label('Title French')
->required()
->maxLength(255),
Forms\Components\Textarea::make('text_en')
->label('Announcement English')
->autosize()
->required()
->maxLength(500),
Forms\Components\Textarea::make('text_fr')
->label('Announcement French')
->autosize()
->required()
->maxLength(500),
]),
Forms\Components\Section::make('Dates')
->description('Announcements times are in UTC')
->schema([
Forms\Components\DateTimePicker::make('start_at')
->label('Start Date')
->default(fn () => now())
->required()
->before('end_at'),
Forms\Components\DateTimePicker::make('end_at')
->label('End Date')
->default(fn () => now()->addDay()->endOfDay())
->required()
->after('start_at'),
]),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title_en')
->label('Title English')
->wrap()
->sortable(),
Tables\Columns\TextColumn::make('title_fr')
->label('Title French')
->wrap()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->label('Created (UTC)')
->datetime('M d, Y H:i'),
Tables\Columns\TextColumn::make('start_at')
->label('Start (UTC)')
->datetime('M d, Y H:i')
->sortable(),
Tables\Columns\TextColumn::make('end_at')
->label('End (UTC)')
->datetime('M d, Y H:i')
->sortable(),
Tables\Columns\TextColumn::make('status')
->badge(true)
->label('Status')
->getStateUsing(function (Announcement $record) {
$now = Carbon::now();
if ($record->start_at > $now) {
return 'Upcoming';
}
if ($record->start_at <= $now && $record->end_at >= $now) {
return 'Active';
}
if ($record->end_at < $now) {
return 'Passed';
}
})
->colors([
'warning' => 'Upcoming',
'success' => 'Active',
'info' => 'Passed',
]),
])
->defaultSort('start_at', 'desc')
->filters([
//
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Components\Section::make('Info')
->schema([
Components\Grid::make(2)
->schema([
Components\Group::make([
TextEntry::make('title_en')
->label('Title English'),
TextEntry::make('created_at')
->label('Created (UTC)')
->datetime('M d, Y H:i'),
TextEntry::make('start_at')
->label('Start (UTC)')
->datetime('M d, Y H:i'),
]),
Components\Group::make([
TextEntry::make('title_fr')
->label('Title French'),
TextEntry::make('status')
->badge()
->getStateUsing(function ($record) {
$now = Carbon::now();
if ($record->start_at > $now) {
return 'Upcoming';
}
if ($record->start_at <= $now && $record->end_at >= $now) {
return 'Active';
}
if ($record->end_at < $now) {
return 'Passed';
}
})
->colors([
'secondary' => 'Upcoming',
'success' => 'Active',
'danger' => 'Passed',
]),
TextEntry::make('end_at')
->label('End (UTC)')
->datetime('M d, Y H:i'),
])
->columns(1),
]),
]),
Components\Section::make('Message')
->schema([
Components\TextEntry::make('text_en')
->label('English'),
Components\TextEntry::make('text_fr')
->label('French'),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListAnnouncements::route('/'),
'create' => Pages\CreateAnnouncement::route('/create'),
'edit' => Pages\EditAnnouncement::route('/{record}/edit'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Filament\Resources\AnnouncementResource\Pages;

use App\Filament\Resources\AnnouncementResource;
use Filament\Resources\Pages\CreateRecord;

class CreateAnnouncement extends CreateRecord
{
protected static string $resource = AnnouncementResource::class;

protected static bool $canCreateAnother = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\AnnouncementResource\Pages;

use App\Filament\Resources\AnnouncementResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditAnnouncement extends EditRecord
{
protected static string $resource = AnnouncementResource::class;

// protected function getHeaderActions(): array
// {
// return [
// Actions\ViewAction::make(),
// ];
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Filament\Resources\AnnouncementResource\Pages;

use App\Filament\Resources\AnnouncementResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListAnnouncements extends ListRecords
{
protected static string $resource = AnnouncementResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->icon('heroicon-o-plus-circle'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\AnnouncementResource\Pages;

use App\Filament\Resources\AnnouncementResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewAnnouncement extends ViewRecord
{
protected static string $resource = AnnouncementResource::class;

protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
];
}
}
2 changes: 1 addition & 1 deletion app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationIcon = 'heroicon-o-users';

public static function form(Form $form): Form
{
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public function casts(): array
];
}

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title_en',
'title_fr',
'text_en',
'text_fr',
'start_at',
'end_at',
];

/**
* Return active announcements
*/
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading