Skip to content

Commit

Permalink
Merge branch 'pr/78'
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Jan 26, 2024
2 parents 7e657d2 + a28125a commit 4994093
Show file tree
Hide file tree
Showing 39 changed files with 259 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function form(Form $form): Form
Forms\Components\Select::make('country')
->searchable()
->getSearchResultsUsing(fn (string $query) => Country::where('name', 'like', "%{$query}%")->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Country::find($value)?->getAttribute('name')),
->getOptionLabelUsing(fn ($value): ?string => Country::firstWhere('id', $value)?->getAttribute('name')),
]);
}

Expand Down
6 changes: 5 additions & 1 deletion app/Filament/Clusters/Products/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,17 @@ public static function getGlobalSearchResultDetails(Model $record): array
];
}

/** @return Builder<Product> */
public static function getGlobalSearchEloquentQuery(): Builder
{
return parent::getGlobalSearchEloquentQuery()->with(['brand']);
}

public static function getNavigationBadge(): ?string
{
return static::$model::whereColumn('qty', '<', 'security_stock')->count();
/** @var class-string<Model> $modelClass */
$modelClass = static::$model;

return (string) $modelClass::whereColumn('qty', '<', 'security_stock')->count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Clusters\Products\Resources\ProductResource\RelationManagers;

use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components\IconEntry;
Expand Down Expand Up @@ -79,11 +80,14 @@ public function table(Table $table): Table
->headerActions([
Tables\Actions\CreateAction::make()
->after(function ($record) {
/** @var User $user */
$user = auth()->user();

Notification::make()
->title('New comment')
->icon('heroicon-o-chat-bubble-bottom-center-text')
->body("**{$record->customer->name} commented on product ({$record->commentable->name}).**")
->sendToDatabase(auth()->user());
->sendToDatabase($user);
}),
])
->actions([
Expand Down
4 changes: 3 additions & 1 deletion app/Filament/Resources/Blog/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public static function table(Table $table): Table
->sortable()
->toggleable(),

Tables\Columns\BadgeColumn::make('status')
Tables\Columns\TextColumn::make('status')
->badge()
->getStateUsing(fn (Post $record): string => $record->published_at?->isPast() ? 'Published' : 'Draft')
->colors([
'success' => 'Published',
Expand Down Expand Up @@ -241,6 +242,7 @@ public static function getPages(): array
];
}

/** @return Builder<Post> */
public static function getGlobalSearchEloquentQuery(): Builder
{
return parent::getGlobalSearchEloquentQuery()->with(['author', 'category']);
Expand Down
6 changes: 5 additions & 1 deletion app/Filament/Resources/Blog/PostResource/Pages/EditPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Resources\Blog\PostResource\Pages;

use App\Filament\Resources\Blog\PostResource;
use App\Models\Blog\Post;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Contracts\Support\Htmlable;
Expand All @@ -13,7 +14,10 @@ class EditPost extends EditRecord

public function getTitle(): string | Htmlable
{
return $this->record->title;
/** @var Post */
$record = $this->getRecord();

return $record->title;
}

protected function getActions(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class ManagePostComments extends ManageRelatedRecords

public function getTitle(): string | Htmlable
{
return "Manage {$this->getRecordTitle()} Comments";
$recordTitle = $this->getRecordTitle();

$recordTitle = $recordTitle instanceof Htmlable ? $recordTitle->toHtml() : $recordTitle;

return "Manage {$recordTitle} Comments";
}

public function getBreadcrumb(): string
Expand Down
6 changes: 5 additions & 1 deletion app/Filament/Resources/Blog/PostResource/Pages/ViewPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Resources\Blog\PostResource\Pages;

use App\Filament\Resources\Blog\PostResource;
use App\Models\Blog\Post;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Contracts\Support\Htmlable;

Expand All @@ -12,7 +13,10 @@ class ViewPost extends ViewRecord

public function getTitle(): string | Htmlable
{
return $this->record->title;
/** @var Post */
$record = $this->getRecord();

return $record->title;
}

protected function getActions(): array
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Resources/Shop/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static function table(Table $table): Table
]);
}

/** @return Builder<Customer> */
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with('addresses')->withoutGlobalScope(SoftDeletingScope::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function form(Form $form): Form
Forms\Components\Select::make('country')
->searchable()
->getSearchResultsUsing(fn (string $query) => Country::where('name', 'like', "%{$query}%")->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Country::find($value)?->getAttribute('name')),
->getOptionLabelUsing(fn ($value): ?string => Country::firstWhere('id', $value)?->getAttribute('name')),
]);
}

Expand Down
12 changes: 9 additions & 3 deletions app/Filament/Resources/Shop/OrderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public static function getPages(): array
];
}

/** @return Builder<Order> */
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->withoutGlobalScope(SoftDeletingScope::class);
Expand All @@ -213,16 +214,21 @@ public static function getGlobalSearchResultDetails(Model $record): array
];
}

/** @return Builder<Order> */
public static function getGlobalSearchEloquentQuery(): Builder
{
return parent::getGlobalSearchEloquentQuery()->with(['customer', 'items']);
}

public static function getNavigationBadge(): ?string
{
return static::$model::where('status', 'new')->count();
/** @var class-string<Model> $modelClass */
$modelClass = static::$model;

return (string) $modelClass::where('status', 'new')->count();
}

/** @return Forms\Components\Component[] */
public static function getDetailsFormSchema(): array
{
return [
Expand Down Expand Up @@ -265,7 +271,7 @@ public static function getDetailsFormSchema(): array
->createOptionAction(function (Action $action) {
return $action
->modalHeading('Create customer')
->modalButton('Create customer')
->modalSubmitActionLabel('Create customer')
->modalWidth('lg');
}),

Expand All @@ -277,7 +283,7 @@ public static function getDetailsFormSchema(): array
Forms\Components\Select::make('currency')
->searchable()
->getSearchResultsUsing(fn (string $query) => Currency::where('name', 'like', "%{$query}%")->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Currency::find($value)?->getAttribute('name'))
->getOptionLabelUsing(fn ($value): ?string => Currency::firstWhere('id', $value)?->getAttribute('name'))
->required(),

AddressForm::make('address')
Expand Down
11 changes: 9 additions & 2 deletions app/Filament/Resources/Shop/OrderResource/Pages/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Filament\Resources\Shop\OrderResource\Pages;

use App\Filament\Resources\Shop\OrderResource;
use App\Models\Shop\Order;
use App\Models\User;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Wizard;
use Filament\Forms\Components\Wizard\Step;
Expand Down Expand Up @@ -34,19 +36,24 @@ public function form(Form $form): Form

protected function afterCreate(): void
{
/** @var Order $order */
$order = $this->record;

/** @var User $user */
$user = auth()->user();

Notification::make()
->title('New order')
->icon('heroicon-o-shopping-bag')
->body("**{$order->customer->name} ordered {$order->items->count()} products.**")
->body("**{$order->customer?->name} ordered {$order->items->count()} products.**")
->actions([
Action::make('View')
->url(OrderResource::getUrl('edit', ['record' => $order])),
])
->sendToDatabase(auth()->user());
->sendToDatabase($user);
}

/** @return Step[] */
protected function getSteps(): array
{
return [
Expand Down
13 changes: 7 additions & 6 deletions app/Filament/Resources/Shop/OrderResource/Pages/ListOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Filament\Resources\Shop\OrderResource;
use Filament\Actions;
use Filament\Pages\Concerns\ExposesTableToWidgets;
use Filament\Resources\Components\Tab;
use Filament\Resources\Pages\ListRecords;

class ListOrders extends ListRecords
Expand All @@ -28,12 +29,12 @@ protected function getHeaderWidgets(): array
public function getTabs(): array
{
return [
null => ListRecords\Tab::make('All'),
'new' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'new')),
'processing' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'processing')),
'shipped' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'shipped')),
'delivered' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'delivered')),
'cancelled' => ListRecords\Tab::make()->query(fn ($query) => $query->where('status', 'cancelled')),
null => Tab::make('All'),
'new' => Tab::make()->query(fn ($query) => $query->where('status', 'new')),
'processing' => Tab::make()->query(fn ($query) => $query->where('status', 'processing')),
'shipped' => Tab::make()->query(fn ($query) => $query->where('status', 'shipped')),
'delivered' => Tab::make()->query(fn ($query) => $query->where('status', 'delivered')),
'cancelled' => Tab::make()->query(fn ($query) => $query->where('status', 'cancelled')),
];
}
}
13 changes: 7 additions & 6 deletions app/Filament/Widgets/StatsOverviewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class StatsOverviewWidget extends BaseWidget

protected function getStats(): array
{
$startDate = filled($this->filters['startDate'] ?? null) ?

$startDate = ! is_null($this->filters['startDate'] ?? null) ?
Carbon::parse($this->filters['startDate']) :
null;

$endDate = filled($this->filters['endDate'] ?? null) ?
$endDate = ! is_null($this->filters['endDate'] ?? null) ?
Carbon::parse($this->filters['endDate']) :
now();

Expand All @@ -33,13 +34,13 @@ protected function getStats(): array

$diffInDays = $startDate ? $startDate->diffInDays($endDate) : 0;

$revenue = ($startDate ? ($diffInDays * 137) : 192100) * $businessCustomerMultiplier;
$newCustomers = ($startDate ? ($diffInDays * 7) : 1340) * $businessCustomerMultiplier;
$newOrders = ($startDate ? ($diffInDays * 13) : 3543) * $businessCustomerMultiplier;
$revenue = (int) (($startDate ? ($diffInDays * 137) : 192100) * $businessCustomerMultiplier);
$newCustomers = (int) (($startDate ? ($diffInDays * 7) : 1340) * $businessCustomerMultiplier);
$newOrders = (int) (($startDate ? ($diffInDays * 13) : 3543) * $businessCustomerMultiplier);

$formatNumber = function (int $number): string {
if ($number < 1000) {
return Number::format($number, 0);
return (string) Number::format($number, 0);
}

if ($number < 1000000) {
Expand Down
5 changes: 3 additions & 2 deletions app/Forms/Components/AddressForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AddressForm extends Forms\Components\Field
{
protected string $view = 'filament-forms::components.group';

/** @var string|callable|null */
public $relationship = null;

public function relationship(string | callable $relationship): static
Expand All @@ -33,7 +34,7 @@ public function saveRelationships(): void
$relationship->updateOrCreate($state);
}

$record->touch();
$record?->touch();
}

public function getChildComponents(): array
Expand All @@ -44,7 +45,7 @@ public function getChildComponents(): array
Forms\Components\Select::make('country')
->searchable()
->getSearchResultsUsing(fn (string $query) => Country::where('name', 'like', "%{$query}%")->pluck('name', 'id'))
->getOptionLabelUsing(fn ($value): ?string => Country::find($value)?->getAttribute('name')),
->getOptionLabelUsing(fn ($value): ?string => Country::firstWhere('id', $value)?->getAttribute('name')),
]),
Forms\Components\TextInput::make('street')
->label('Street address')
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Team extends JsonResource
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
* @return array<string,mixed>|\Illuminate\Contracts\Support\Arrayable<string,mixed>|\JsonSerializable
*/
public function toArray($request)
{
Expand Down
12 changes: 9 additions & 3 deletions app/Livewire/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Illuminate\View\View;
use Livewire\Component;

/**
* @property-read Forms\Form $form
*/
class Form extends Component implements HasForms
{
use InteractsWithForms;

/** @var array<string, mixed> */
public $data = [];

public function mount()
public function mount(): void
{
if (! app()->environment('local')) {
abort(404);
Expand All @@ -22,6 +27,7 @@ public function mount()
$this->form->fill();
}

/** @return Forms\Components\Component[] */
protected function getFormSchema(): array
{
return [
Expand All @@ -39,7 +45,7 @@ protected function getFormSchema(): array
];
}

public function submit()
public function submit(): never
{
dd($this->form->getState());
}
Expand All @@ -49,7 +55,7 @@ protected function getFormStatePath(): ?string
return 'data';
}

public function render()
public function render(): View
{
return view('livewire.form');
}
Expand Down
3 changes: 2 additions & 1 deletion app/Livewire/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App\Livewire;

use Illuminate\View\View;
use Livewire\Component;

class Notifications extends Component
{
public function render()
public function render(): View
{
return view('livewire.notifications');
}
Expand Down
Loading

0 comments on commit 4994093

Please sign in to comment.