Skip to content

Commit

Permalink
migrate to filament v3.x, add rating & ip columns and other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fordiquez committed Aug 21, 2023
1 parent 69e51d7 commit 67388ac
Show file tree
Hide file tree
Showing 108 changed files with 3,459 additions and 2,529 deletions.
2 changes: 1 addition & 1 deletion app/Filament/Forms/Components/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function isClearable(): bool
return $this->evaluate($this->clearable);
}

public function getRefId(string $prefix, ?string $suffix = null): string
public function getRefId(string $prefix, string $suffix = null): string
{
return sprintf('%s_%s%s', str_replace('.', '_', $this->getId()), $prefix, $suffix ? '_' . $suffix : '');
}
Expand Down
71 changes: 35 additions & 36 deletions app/Filament/Pages/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace App\Filament\Pages;

use App\Models\City;
use App\Models\Country;
use App\Models\State;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Arr;

Expand All @@ -33,14 +36,14 @@ public function getCancelButtonUrlProperty(): string
return static::getUrl();
}

protected function getBreadcrumbs(): array
public function getBreadcrumbs(): array
{
return [
url()->current() => 'Addresses',
];
}

public function mount()
public function mount(): void
{
$this->form->fill([
'addresses' => auth()->user()->addresses->toArray(),
Expand Down Expand Up @@ -75,44 +78,40 @@ public function submit()
}
}

$this->notify('success', 'Your addresses has been updated.');
Notification::make()->title('Your addresses has been updated.')->success()->send();
}

protected function getFormSchema(): array
{
return [
Repeater::make('addresses')
->schema([
TextInput::make('title')->required()->maxLength(255),
Toggle::make('is_main')->inline()->default(false),
Select::make('country_id')
->options(Country::all()->pluck('name', 'id')->toArray())
->required()
->reactive()
->afterStateUpdated(fn (callable $set) => $set('state_id', null)),
Select::make('state_id')
->options(fn (callable $get) => $get('country_id') ? Country::find($get('country_id'))->states->pluck('name', 'id') : [])
->required()
->reactive()
->disabled(fn (callable $get) => !$get('country_id'))
->afterStateUpdated(fn (callable $set) => $set('city_id', null)),
Select::make('city_id')
->options(fn (callable $get) => $get('state_id') ? State::find($get('state_id'))->cities->pluck('name', 'id') : [])
->required()
->reactive()
->disabled(fn (callable $get) => !$get('state_id'))
->afterStateUpdated(fn (callable $set) => $set('street_id', null)),
Select::make('street_id')
->options(fn (callable $get) => $get('city_id') ? City::find($get('city_id'))->streets->pluck('name', 'id') : [])
->required()
->reactive()
->disabled(fn (callable $get) => !$get('city_id')),
TextInput::make('house')->required(),
TextInput::make('flat')->nullable(),
TextInput::make('postal_code')->numeric()->nullable(),
])->columns()
->lazy()
->itemLabel(fn (array $state): ?string => $state['title'] ?? null),
Section::make()->schema([
Repeater::make('addresses')
->schema([
TextInput::make('title')->required()->maxLength(255),
Toggle::make('is_main')->inline()->default(false),
Select::make('country_id')
->options(Country::all()->pluck('name', 'id')->toArray())
->required()
->reactive()
->afterStateUpdated(fn (callable $set) => $set('state_id', null)),
Select::make('state_id')
->options(fn (Get $get) => $get('country_id') ? Country::find($get('country_id'))->states->pluck('name', 'id') : [])
->required()
->reactive()
->disabled(fn (Get $get) => !$get('country_id'))
->afterStateUpdated(fn (Set $set) => $set('city_id', null)),
Select::make('city_id')
->options(fn (Get $get) => $get('state_id') ? State::find($get('state_id'))->cities->pluck('name', 'id') : [])
->required()
->reactive()
->disabled(fn (Get $get) => !$get('state_id')),
TextInput::make('house')->required(),
TextInput::make('flat')->nullable(),
TextInput::make('postal_code')->numeric()->nullable(),
])->columns()
->lazy()
->itemLabel(fn (array $state): ?string => $state['title'] ?? null),
]),
];
}
}
24 changes: 14 additions & 10 deletions app/Filament/Pages/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Hash;
use Phpsa\FilamentPasswordReveal\Password;
use Ysfkaya\FilamentPhoneInput\PhoneInput;
use Ysfkaya\FilamentPhoneInput\Forms\PhoneInput;
use Ysfkaya\FilamentPhoneInput\PhoneInputNumberType;

class Profile extends Page implements HasForms
Expand Down Expand Up @@ -51,7 +52,7 @@ class Profile extends Page implements HasForms

public ?string $new_password_confirmation = null;

public function mount()
public function mount(): void
{
$this->form->fill([
'first_name' => auth()->user()->first_name,
Expand All @@ -64,7 +65,7 @@ public function mount()
]);
}

public function submit()
public function submit(): void
{
$this->form->getState();

Expand Down Expand Up @@ -94,10 +95,13 @@ public function submit()
}

$this->reset(['current_password', 'new_password', 'new_password_confirmation']);
$this->notify('success', 'Your profile has been updated.');
Notification::make()
->title('Your profile has been updated.')
->success()
->send();
}

protected function updateSessionPassword(User $user)
protected function updateSessionPassword(User $user): void
{
request()->session()->put([
'password_hash_' . auth()->getDefaultDriver() => $user->getAuthPassword(),
Expand All @@ -109,7 +113,7 @@ public function getCancelButtonUrlProperty(): string
return static::getUrl();
}

protected function getBreadcrumbs(): array
public function getBreadcrumbs(): array
{
return [
url()->current() => 'Profile',
Expand All @@ -134,7 +138,7 @@ protected function getFormSchema(): array
]),
Section::make('Details')->columns()
->schema([
DatePicker::make('birth_date')->maxDate(now()),
DatePicker::make('birth_date')->native(false)->maxDate(now()),
Select::make('gender')->options(UserGender::asSelectArray()),
SpatieMediaLibraryFileUpload::make('avatar')->collection('avatars')->columnSpanFull(),
]),
Expand All @@ -147,14 +151,14 @@ protected function getFormSchema(): array
->autocomplete('off')
->columnSpan(1)
->revealable()
->copyable(),
->copyable(!app()->isLocal()),
Grid::make()->schema([
Password::make('new_password')
->password()
->rule('confirmed')
->autocomplete('new-password')
->revealable()
->copyable()
->copyable(!app()->isLocal())
->generatable(),
Password::make('new_password_confirmation')
->password()
Expand All @@ -163,7 +167,7 @@ protected function getFormSchema(): array
->currentPassword()
->autocomplete('new-password')
->revealable()
->copyable()
->copyable(!app()->isLocal())
->generatable(),
]),
]),
Expand Down
44 changes: 25 additions & 19 deletions app/Filament/Resources/BrandResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use App\Filament\Resources\BrandResource\Pages;
use App\Filament\Resources\BrandResource\RelationManagers\GoodsRelationManager;
use App\Models\Brand;
use Camya\Filament\Forms\Components\TitleWithSlugInput;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Str;

class BrandResource extends Resource
{
Expand All @@ -28,21 +28,25 @@ public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Card::make()->schema([
TitleWithSlugInput::make(
fieldTitle: 'name',
fieldSlug: 'slug',
urlVisitLinkVisible: false,
titleLabel: 'Name',
titleRules: ['required', 'max:100'],
slugLabel: 'Slug',
slugRules: ['required', 'max:100', 'alpha_dash'],
slugRuleUniqueParameters: [
'table' => 'brands',
'column' => 'slug',
'ignoreRecord' => true,
]
),
Forms\Components\Section::make()->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(100)
->autofocus()
->live(debounce: 500)
->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $state) {
if (!$get('is_slug_changed_manually')) {
$set('slug', Str::slug($state));
}
}),
Forms\Components\TextInput::make('slug')
->rule('alpha_dash:ascii')
->unique('brands', 'slug', ignoreRecord: true)
->afterStateUpdated(function (Forms\Set $set) {
$set('is_slug_changed_manually', true);
})
->required()
->maxLength(100),
Forms\Components\TextInput::make('url')->maxLength(255),
Forms\Components\SpatieMediaLibraryFileUpload::make('logo')->collection('brands'),
]),
Expand All @@ -61,6 +65,8 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('name')->sortable()->searchable(),
Tables\Columns\TextColumn::make('slug')->sortable()->searchable()->toggleable(),
Tables\Columns\TextColumn::make('url')->toggleable(),
Tables\Columns\TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
Expand All @@ -74,7 +80,7 @@ public static function table(Table $table): Table
]);
}

protected static function getNavigationBadge(): ?string
public static function getNavigationBadge(): ?string
{
return static::$model::count();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/BrandResource/Pages/CreateBrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class CreateBrand extends CreateRecord

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
return $this->previousUrl ?? $this->getResource()::getUrl('index');
}
}
4 changes: 2 additions & 2 deletions app/Filament/Resources/BrandResource/Pages/EditBrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Filament\Resources\BrandResource\Pages;

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

class EditBrand extends EditRecord
Expand All @@ -19,6 +19,6 @@ protected function getActions(): array

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
return $this->previousUrl ?? $this->getResource()::getUrl('index');
}
}
2 changes: 1 addition & 1 deletion app/Filament/Resources/BrandResource/Pages/ListBrands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Filament\Resources\BrandResource\Pages;

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

class ListBrands extends ListRecords
Expand Down
Loading

0 comments on commit 67388ac

Please sign in to comment.