Skip to content

Commit 1bbbc9c

Browse files
committed
refactor: remove duplicated code across resources, traits and jobs
1 parent 073b21d commit 1bbbc9c

34 files changed

Lines changed: 883 additions & 1637 deletions

app/Actions/Socialstream/CreateUserWithTeamsFromProvider.php

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,9 @@
1010
use JoelButcher\Socialstream\Socialstream;
1111
use Laravel\Socialite\Contracts\User as ProviderUserContract;
1212

13-
class CreateUserWithTeamsFromProvider implements CreatesUserFromProvider
13+
class CreateUserWithTeamsFromProvider extends CreateUserFromProvider
1414
{
15-
/**
16-
* Create a new action instance.
17-
*/
18-
public function __construct(
19-
/**
20-
* The creates connected accounts instance.
21-
*/
22-
public CreatesConnectedAccounts $createsConnectedAccounts
23-
)
24-
{
25-
}
26-
27-
/**
28-
* Create a new user from a social provider user.
29-
*/
30-
public function create(string $provider, ProviderUserContract $providerUser): User
31-
{
32-
return DB::transaction(fn() => tap(User::create([
33-
'name' => $providerUser->getName(),
34-
'email' => $providerUser->getEmail(),
35-
]), function (User $user) use ($provider, $providerUser): void {
36-
$user->markEmailAsVerified();
37-
38-
if (Socialstream::hasProviderAvatarsFeature() && $providerUser->getAvatar()) {
39-
$user->setProfilePhotoFromUrl($providerUser->getAvatar());
40-
}
41-
42-
$this->createsConnectedAccounts->create($user, $provider, $providerUser);
43-
44-
$this->createTeam($user);
45-
}));
46-
}
47-
48-
/**
49-
* Create a personal team for the user.
50-
*/
51-
protected function createTeam(User $user): void
52-
{
53-
$user->ownedTeams()->save(Team::forceCreate([
54-
'user_id' => $user->id,
55-
'name' => explode(' ', $user->name, 2)[0]."'s Team",
56-
'personal_team' => true,
57-
]));
58-
}
15+
// Intentionally empty – the base class already implements the full logic.
16+
// Keeping this subclass allows the configuration that expects this
17+
// class name to continue functioning without change.
5918
}

app/Filament/App/Resources/AuthorResource.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
class AuthorResource extends Resource
2121
{
22+
use NameDescriptionActiveResourceTrait;
23+
2224
protected static ?string $model = Author::class;
2325

2426
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';
@@ -29,52 +31,13 @@ class AuthorResource extends Resource
2931

3032
public static function form(Schema $schema): Schema
3133
{
32-
return $schema
33-
->components([
34-
TextInput::make('name')
35-
->required()
36-
->maxLength(255),
37-
TextInput::make('description')
38-
->required()
39-
->maxLength(255),
40-
TextInput::make('is_active')
41-
->required()
42-
->numeric(),
43-
]);
34+
return static::baseForm($schema);
4435
}
4536

4637
#[Override]
4738
public static function table(Table $table): Table
4839
{
49-
return $table
50-
->columns([
51-
TextColumn::make('name')
52-
->searchable(),
53-
TextColumn::make('description')
54-
->searchable(),
55-
TextColumn::make('is_active')
56-
->numeric()
57-
->sortable(),
58-
TextColumn::make('created_at')
59-
->dateTime()
60-
->sortable()
61-
->toggleable(isToggledHiddenByDefault: true),
62-
TextColumn::make('updated_at')
63-
->dateTime()
64-
->sortable()
65-
->toggleable(isToggledHiddenByDefault: true),
66-
])
67-
->filters([
68-
//
69-
])
70-
->recordActions([
71-
EditAction::make(),
72-
])
73-
->toolbarActions([
74-
BulkActionGroup::make([
75-
DeleteBulkAction::make(),
76-
]),
77-
]);
40+
return static::baseTable($table);
7841
}
7942

8043
#[Override]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Filament\App\Resources;
4+
5+
use Filament\Forms\Components\TextInput;
6+
use Filament\Tables\Columns\TextColumn;
7+
8+
trait BasicResourceTrait
9+
{
10+
/**
11+
* Shared form components for resources that have group, gid and name.
12+
*/
13+
public static function basicFormFields(): array
14+
{
15+
return [
16+
TextInput::make('group')->maxLength(255),
17+
TextInput::make('gid')->numeric(),
18+
TextInput::make('name')->maxLength(255),
19+
];
20+
}
21+
22+
/**
23+
* Shared table columns for group, gid and name.
24+
*/
25+
public static function basicTableColumns(): array
26+
{
27+
return [
28+
TextColumn::make('group')->searchable(),
29+
TextColumn::make('gid')->numeric()->sortable(),
30+
TextColumn::make('name')->searchable(),
31+
];
32+
}
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Filament\App\Resources;
4+
5+
use Filament\Forms\Components\TextInput;
6+
use Filament\Forms\Components\Textarea;
7+
use Filament\Tables\Columns\TextColumn;
8+
9+
trait EventResourceTrait
10+
{
11+
public static function eventFormFields(): array
12+
{
13+
return [
14+
TextInput::make('converted_date')->maxLength(255),
15+
TextInput::make('year')->numeric(),
16+
TextInput::make('month')->numeric(),
17+
TextInput::make('day')->numeric(),
18+
TextInput::make('type')->maxLength(255),
19+
Textarea::make('description')->maxLength(255),
20+
TextInput::make('plac')->maxLength(255),
21+
TextInput::make('addr_id')->numeric(),
22+
TextInput::make('phon')->maxLength(255),
23+
Textarea::make('caus')->maxLength(65535)->columnSpanFull(),
24+
TextInput::make('age')->maxLength(255),
25+
TextInput::make('agnc')->maxLength(255),
26+
TextInput::make('title')->maxLength(255),
27+
TextInput::make('date')->maxLength(255),
28+
TextInput::make('places_id')->numeric(),
29+
];
30+
}
31+
32+
public static function eventTableColumns(): array
33+
{
34+
return [
35+
TextColumn::make('converted_date')->searchable(),
36+
TextColumn::make('year')->numeric()->sortable(),
37+
TextColumn::make('month')->numeric()->sortable(),
38+
TextColumn::make('day')->numeric()->sortable(),
39+
TextColumn::make('type')->searchable(),
40+
TextColumn::make('plac')->searchable(),
41+
TextColumn::make('addr_id')->numeric()->sortable(),
42+
TextColumn::make('phon')->searchable(),
43+
TextColumn::make('age')->searchable(),
44+
TextColumn::make('agnc')->searchable(),
45+
TextColumn::make('title')->searchable(),
46+
TextColumn::make('date')->searchable(),
47+
TextColumn::make('description')->searchable(),
48+
TextColumn::make('places_id')->numeric()->sortable(),
49+
TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
50+
TextColumn::make('updated_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
51+
TextColumn::make('deleted_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
52+
];
53+
}
54+
}

app/Filament/App/Resources/FamilyEventResource.php

Lines changed: 25 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
class FamilyEventResource extends Resource
2828
{
29+
use EventResourceTrait;
30+
2931
protected static ?string $model = FamilyEvent::class;
3032

3133
protected static ?string $navigationLabel = 'Family Events';
@@ -38,117 +40,35 @@ class FamilyEventResource extends Resource
3840
public static function form(Schema $schema): Schema
3941
{
4042
return $schema
41-
->components([
42-
TextInput::make('family_id')
43-
->required()
44-
->numeric(),
45-
TextInput::make('places_id')
46-
->numeric(),
47-
Textarea::make('date')
48-
->maxLength(65535)
49-
->columnSpanFull(),
50-
TextInput::make('title')
51-
->maxLength(255),
52-
Textarea::make('description')
53-
->maxLength(65535)
54-
->columnSpanFull(),
55-
TextInput::make('converted_date')
56-
->maxLength(255),
57-
TextInput::make('year')
58-
->numeric(),
59-
TextInput::make('month')
60-
->numeric(),
61-
TextInput::make('day')
62-
->numeric(),
63-
TextInput::make('type')
64-
->maxLength(255),
65-
TextInput::make('plac')
66-
->maxLength(255),
67-
TextInput::make('addr_id')
68-
->numeric(),
69-
TextInput::make('phon')
70-
->maxLength(255),
71-
Textarea::make('caus')
72-
->maxLength(65535)
73-
->columnSpanFull(),
74-
TextInput::make('age')
75-
->maxLength(255),
76-
TextInput::make('agnc')
77-
->maxLength(255),
78-
TextInput::make('husb')
79-
->numeric(),
80-
TextInput::make('wife')
81-
->numeric(),
82-
]);
43+
->components(array_merge(
44+
[TextInput::make('family_id')->required()->numeric()],
45+
static::eventFormFields(),
46+
[
47+
TextInput::make('places_id')->numeric(),
48+
TextInput::make('husb')->numeric(),
49+
TextInput::make('wife')->numeric(),
50+
]
51+
));
8352
}
8453

8554
#[Override]
8655
public static function table(Table $table): Table
8756
{
8857
return $table
89-
->columns([
90-
TextColumn::make('family_id')
91-
->numeric()
92-
->sortable(),
93-
TextColumn::make('places_id')
94-
->numeric()
95-
->sortable(),
96-
TextColumn::make('title')
97-
->searchable(),
98-
TextColumn::make('converted_date')
99-
->searchable(),
100-
TextColumn::make('created_at')
101-
->dateTime()
102-
->sortable()
103-
->toggleable(isToggledHiddenByDefault: true),
104-
TextColumn::make('updated_at')
105-
->dateTime()
106-
->sortable()
107-
->toggleable(isToggledHiddenByDefault: true),
108-
TextColumn::make('deleted_at')
109-
->dateTime()
110-
->sortable()
111-
->toggleable(isToggledHiddenByDefault: true),
112-
TextColumn::make('year')
113-
->numeric()
114-
->sortable(),
115-
TextColumn::make('month')
116-
->numeric()
117-
->sortable(),
118-
TextColumn::make('day')
119-
->numeric()
120-
->sortable(),
121-
TextColumn::make('type')
122-
->searchable(),
123-
TextColumn::make('plac')
124-
->searchable(),
125-
TextColumn::make('addr_id')
126-
->numeric()
127-
->sortable(),
128-
TextColumn::make('phon')
129-
->searchable(),
130-
TextColumn::make('age')
131-
->searchable(),
132-
TextColumn::make('agnc')
133-
->searchable(),
134-
TextColumn::make('husb')
135-
->numeric()
136-
->sortable(),
137-
TextColumn::make('wife')
138-
->numeric()
139-
->sortable(),
140-
])
141-
->filters([
142-
//
143-
])
144-
->recordActions([
145-
EditAction::make(),
146-
])
147-
->toolbarActions([
148-
BulkActionGroup::make([
149-
DeleteBulkAction::make(),
150-
]),
151-
]);
58+
->columns(array_merge(
59+
[
60+
TextColumn::make('family_id')->numeric()->sortable(),
61+
TextColumn::make('places_id')->numeric()->sortable(),
62+
],
63+
static::eventTableColumns(),
64+
[
65+
TextColumn::make('husb')->numeric()->sortable(),
66+
TextColumn::make('wife')->numeric()->sortable(),
67+
]
68+
))
69+
->filters([])
70+
->recordActions([EditAction::make()])
71+
->toolbarActions([BulkActionGroup::make([DeleteBulkAction::make()])]);
15272
}
15373

15474
#[Override]

0 commit comments

Comments
 (0)