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

Implement brand #13

Merged
merged 2 commits into from
Dec 22, 2024
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
31 changes: 31 additions & 0 deletions ShopFlow/app/Enums/BrandStatusEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use App\Traits\HasOptions;

enum BrandStatusEnum: int
{
use HasOptions;

case ACTIVE = 1;
case INACTIVE = 2;

public function label(): string
{
return match ($this) {
self::ACTIVE => 'Active',
self::INACTIVE => 'Inactive',
};
}

public function color(): string
{
return match ($this) {
self::ACTIVE => 'success',
self::INACTIVE => 'danger',
};
}
}
137 changes: 137 additions & 0 deletions ShopFlow/app/Filament/Resources/BrandResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources;

use App\Enums\BrandStatusEnum;
use App\Filament\Resources\BrandResource\Pages;
use App\Models\Brand;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use FilamentTiptapEditor\Enums\TiptapOutput;
use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Support\Str;

class BrandResource extends Resource
{
protected static ?string $model = Brand::class;

protected static ?string $navigationGroup = 'Product';

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

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('heading')
->required()
->live(onBlur: true)
->maxLength(255)
->afterStateUpdated(function (string $operation, ?string $state, Forms\Set $set): void {
if ($operation === 'create') {
$set('slug', Str::slug($state));
}
}),
Forms\Components\TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->maxLength(255)
->unique(Brand::class, 'slug', ignoreRecord: true),

TiptapEditor::make('content')
->output(TiptapOutput::Html) // optional, change the format for saved data, default is html
->columnSpanFull()
->extraInputAttributes(['style' => 'min-height: 12rem;']),
Forms\Components\TextInput::make('title')
->maxLength(255),
Forms\Components\TextInput::make('description')
->maxLength(255),
Forms\Components\Toggle::make('no_index')
->required(),
Forms\Components\TextInput::make('canonical')
->maxLength(255),
Forms\Components\Fieldset::make('image')
->relationship('image')
->schema([
Forms\Components\FileUpload::make('path')
->nullable()
->columns(1)
->columnSpanFull(),
])
->mutateRelationshipDataBeforeSaveUsing(function (array $data, Brand $record) {
if ($data['path'] === null) {
$record->image->delete();
}

return $data;
})
->columnSpanFull(),
Forms\Components\Select::make('status')
->required()
->options(BrandStatusEnum::options())
->default(BrandStatusEnum::ACTIVE->value),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('heading')
->limit(30)
->wrap(),
Tables\Columns\TextColumn::make('slug')
->limit(30)
->wrap(),
Tables\Columns\TextColumn::make('title')
->limit(30)
->wrap(),
Tables\Columns\TextColumn::make('description')
->limit(30)
->wrap(),
Tables\Columns\IconColumn::make('no_index')
->boolean(),
Tables\Columns\ImageColumn::make('image.path'),
Tables\Columns\TextColumn::make('status')
->getStateUsing(fn (Brand $record) => $record->status->label())
->color(fn (Brand $record): string => $record->status->color())
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
]);
}

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

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

declare(strict_types=1);

namespace App\Filament\Resources\BrandResource\Pages;

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

class CreateBrand extends CreateRecord
{
protected static string $resource = BrandResource::class;

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
26 changes: 26 additions & 0 deletions ShopFlow/app/Filament/Resources/BrandResource/Pages/EditBrand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\BrandResource\Pages;

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

class EditBrand extends EditRecord
{
protected static string $resource = BrandResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
21 changes: 21 additions & 0 deletions ShopFlow/app/Filament/Resources/BrandResource/Pages/ListBrands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\BrandResource\Pages;

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

class ListBrands extends ListRecords
{
protected static string $resource = BrandResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
59 changes: 39 additions & 20 deletions ShopFlow/app/Filament/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use App\Enums\CategoryStatusEnum;
use App\Filament\Resources\CategoryResource\Pages;
use App\Models\Category;
use App\Models\Image;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use FilamentTiptapEditor\Enums\TiptapOutput;
use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Support\Str;

class CategoryResource extends Resource
{
Expand All @@ -28,10 +30,22 @@ public static function form(Form $form): Form
return $form
->schema([
Forms\Components\TextInput::make('heading')
->required(),
->required()
->live(onBlur: true)
->maxLength(255)
->afterStateUpdated(function (string $operation, ?string $state, Forms\Set $set): void {
if ($operation === 'create') {
$set('slug', Str::slug($state));
}
}),
Forms\Components\TextInput::make('slug')
->required(),
Forms\Components\TextInput::make('title'),
->disabled()
->dehydrated()
->required()
->maxLength(255)
->unique(Category::class, 'slug', ignoreRecord: true),
Forms\Components\TextInput::make('title')
->maxLength(255),
TiptapEditor::make('content')
->output(TiptapOutput::Html) // optional, change the format for saved data, default is html
->columnSpanFull()
Expand All @@ -40,26 +54,36 @@ public static function form(Form $form): Form
Forms\Components\Textarea::make('description')
->maxLength(255)
->columnSpanFull(),
Forms\Components\TextInput::make('canonical'),
Forms\Components\TextInput::make('canonical')
->maxLength(255),
Forms\Components\Select::make('parent_id')
->options(function () {
return Category::active()
->get()
->pluck('heading', 'id');
}),
Forms\Components\Toggle::make('no_index'),

Forms\Components\Repeater::make('images')
->relationship()
->maxItems(1)
Forms\Components\Toggle::make('no_index')
->required(),
Forms\Components\Fieldset::make('image')
->relationship('image')
->schema([
Forms\Components\FileUpload::make('path')
->previewable(),
->nullable()
->columns(1)
->columnSpanFull(),
])
->columns(1)
->mutateRelationshipDataBeforeSaveUsing(function (array $data, Category $record) {
if ($data['path'] === null) {
$record->image->delete();
}

return $data;
})
->columnSpanFull(),
Forms\Components\Select::make('status')
->options(CategoryStatusEnum::options()),
->required()
->options(CategoryStatusEnum::options())
->default(CategoryStatusEnum::ACTIVE->value),
]);
}

Expand Down Expand Up @@ -97,10 +121,10 @@ public static function table(Table $table): Table
->sortable(),
Tables\Columns\ImageColumn::make('images')
->getStateUsing(function (Category $record) {
/** @var \Illuminate\Database\Eloquent\Collection|\App\Models\Image[] $images */
$images = $record->images;
/** @var Image|null $image */
$image = $record->image;

return $images->first()?->path;
return $image?->path;
}),

Tables\Columns\TextColumn::make('created_at')
Expand All @@ -117,11 +141,6 @@ public static function table(Table $table): Table
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
class CreateCategory extends CreateRecord
{
protected static string $resource = CategoryResource::class;

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
Loading
Loading