Skip to content

Commit

Permalink
refactor(filament): moved pivot fields to a proper function on relati…
Browse files Browse the repository at this point in the history
…on managers (#752)
  • Loading branch information
Kyrch authored Oct 8, 2024
1 parent 08459d8 commit fe9a241
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 116 deletions.
13 changes: 11 additions & 2 deletions app/Actions/Discord/DiscordThreadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ public function handle(Anime $anime, array $fields): ?Exception

$anime->name = Arr::get($fields, 'name');

/** @var \Illuminate\Filesystem\FilesystemAdapter */
/** @var \Illuminate\Filesystem\FilesystemAdapter $fs */
$fs = Storage::disk(Config::get('image.disk'));

$anime->images->each(fn ($image) => Arr::set($image, 'link', $fs->url($image->path)));

$animeArray = $anime->toArray();

Arr::set($animeArray, Anime::ATTRIBUTE_SEASON, $anime->season->localize());
Arr::set($animeArray, Anime::ATTRIBUTE_MEDIA_FORMAT, $anime->media_format->localize());

foreach ($animeArray['images'] as $key => $image) {
Arr::set($animeArray, "images.$key.facet", $anime->images->get($key)->facet->localize());
}

$response = Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')])
->acceptJson()
->post(Config::get('services.discord.api_url') . '/thread', $anime->toArray())
->post(Config::get('services.discord.api_url') . '/thread', $animeArray)
->throw()
->json();

Expand Down
15 changes: 8 additions & 7 deletions app/Actions/Discord/DiscordVideoNotificationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ class DiscordVideoNotificationAction
*
* @param Collection<int, Video> $videos
* @param array $fields
*
* @return void
*/
public function handle(Collection $videos, array $fields): void
{
$type = Arr::get($fields, 'notification-type');
$shouldForce = Arr::get($fields, 'should-force-thread');

/** @var \Illuminate\Filesystem\FilesystemAdapter */
/** @var \Illuminate\Filesystem\FilesystemAdapter $fs */
$fs = Storage::disk(Config::get('image.disk'));

$newVideos = [];
Expand All @@ -56,15 +55,17 @@ public function handle(Collection $videos, array $fields): void
$anime->load('discordthread');
}

Arr::set($video, 'source_name', $video->source->localize());
Arr::set($video, 'overlap_name', $video->overlap->localize());
Arr::set($theme, 'type_name', $theme->type->localize());

$anime->images->each(function (Image $image) use ($fs) {
Arr::set($image, 'link', $fs->url($image->path));
});

$newVideos[] = $video;
$videoArray = $video->toArray();

Arr::set($videoArray, Video::ATTRIBUTE_SOURCE, $video->source->localize());
Arr::set($videoArray, Video::ATTRIBUTE_OVERLAP, $video->overlap->localize());
Arr::set($videoArray, 'animethemeentries.0.animetheme.type', $theme->type->localize());

$newVideos = $videoArray;
}

Http::withHeaders(['x-api-key' => Config::get('services.discord.api_key')])
Expand Down
49 changes: 4 additions & 45 deletions app/Filament/Actions/Base/AttachAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
use App\Concerns\Filament\Actions\HasPivotActionLogs;
use App\Filament\Components\Fields\Select;
use App\Filament\RelationManagers\BaseRelationManager;
use App\Filament\RelationManagers\Wiki\ResourceRelationManager;
use App\Filament\Resources\Wiki\Artist\RelationManagers\GroupArtistRelationManager;
use App\Filament\Resources\Wiki\Artist\RelationManagers\MemberArtistRelationManager;
use App\Filament\Resources\Wiki\Artist\RelationManagers\SongArtistRelationManager;
use App\Filament\Resources\Wiki\ExternalResource\RelationManagers\AnimeResourceRelationManager;
use App\Filament\Resources\Wiki\ExternalResource\RelationManagers\ArtistResourceRelationManager;
use App\Filament\Resources\Wiki\ExternalResource\RelationManagers\SongResourceRelationManager;
use App\Filament\Resources\Wiki\ExternalResource\RelationManagers\StudioResourceRelationManager;
use App\Filament\Resources\Wiki\Song\RelationManagers\ArtistSongRelationManager;
use App\Pivots\Wiki\AnimeResource;
use App\Pivots\Wiki\ArtistSong;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Actions\AttachAction as DefaultAttachAction;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

Expand Down Expand Up @@ -48,42 +36,13 @@ protected function setUp(): void
$title = $livewire->getTable()->getRecordTitle(new $model);
return Select::make('recordId')
->label($title)
->useScout($model);
->useScout($model)
->required();
});

$this->form(fn (AttachAction $action): array => [
$this->form(fn (AttachAction $action, BaseRelationManager $livewire): array => [
$action->getRecordSelect(),

TextInput::make(AnimeResource::ATTRIBUTE_AS)
->label(__('filament.fields.anime.resources.as.name'))
->helperText(__('filament.fields.anime.resources.as.help'))
->visibleOn([
AnimeResourceRelationManager::class,
ArtistResourceRelationManager::class,
SongResourceRelationManager::class,
StudioResourceRelationManager::class,
ResourceRelationManager::class,
]),

TextInput::make(ArtistSong::ATTRIBUTE_AS)
->label(__('filament.fields.artist.songs.as.name'))
->helperText(__('filament.fields.artist.songs.as.help'))
->visibleOn([
ArtistSongRelationManager::class,
MemberArtistRelationManager::class,
GroupArtistRelationManager::class,
SongArtistRelationManager::class,
]),

TextInput::make(ArtistSong::ATTRIBUTE_ALIAS)
->label(__('filament.fields.artist.songs.alias.name'))
->helperText(__('filament.fields.artist.songs.alias.help'))
->visibleOn([
ArtistSongRelationManager::class,
MemberArtistRelationManager::class,
GroupArtistRelationManager::class,
SongArtistRelationManager::class,
]),
...$livewire->getPivotFields(),
]);

$this->after(fn ($livewire, $record) => $this->pivotActionLog('Attach', $livewire, $record));
Expand Down
6 changes: 6 additions & 0 deletions app/Filament/Actions/Base/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Enums\Auth\Role;
use App\Filament\RelationManagers\BaseRelationManager;
use App\Filament\RelationManagers\Wiki\ResourceRelationManager;
use Filament\Forms\Form;
use Filament\Tables\Actions\CreateAction as DefaultCreateAction;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand All @@ -30,6 +31,11 @@ protected function setUp(): void
{
parent::setUp();

$this->form(fn (Form $form, BaseRelationManager $livewire) => [
...$livewire->form($form)->getComponents(),
...$livewire->getPivotFields(),
]);

$this->after(function ($livewire, $record) {
if ($livewire instanceof BaseRelationManager) {
$relationship = $livewire->getRelationship();
Expand Down
6 changes: 6 additions & 0 deletions app/Filament/Actions/Base/EditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Concerns\Filament\Actions\HasPivotActionLogs;
use App\Filament\RelationManagers\BaseRelationManager;
use Filament\Forms\Form;
use Filament\Tables\Actions\EditAction as DefaultEditAction;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

Expand All @@ -27,6 +28,11 @@ protected function setUp(): void

$this->label(__('filament.actions.base.edit'));

$this->form(fn (Form $form, BaseRelationManager $livewire) => [
...$livewire->form($form)->getComponents(),
...$livewire->getPivotFields(),
]);

$this->after(function ($livewire, $record) {
if ($livewire instanceof BaseRelationManager) {
if ($livewire->getRelationship() instanceof BelongsToMany) {
Expand Down
12 changes: 11 additions & 1 deletion app/Filament/RelationManagers/BaseRelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ abstract class BaseRelationManager extends RelationManager

protected $listeners = ['updateAllRelationManager' => '$refresh'];

/**
* Get the pivot fields of the relation.
*
* @return array<Component>
*/
public function getPivotFields(): array

Check failure on line 33 in app/Filament/RelationManagers/BaseRelationManager.php

View workflow job for this annotation

GitHub Actions / static-analysis

Method App\Filament\RelationManagers\BaseRelationManager::getPivotFields() has invalid return type App\Filament\RelationManagers\Component.
{
return [];
}

/**
* The index page of the relation resource.
*
Expand Down Expand Up @@ -97,7 +107,7 @@ public static function getActions(): array

/**
* Get the bulk actions available for the relation.
*
*
* @return array
*
* @noinspection PhpMissingParentCallCommonInspection
Expand Down
16 changes: 16 additions & 0 deletions app/Filament/RelationManagers/Wiki/ResourceRelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Filament\RelationManagers\BaseRelationManager;
use App\Filament\Resources\Wiki\ExternalResource as ExternalResourceResource;
use App\Models\Wiki\ExternalResource;
use App\Pivots\Wiki\AnimeResource;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Tables\Table;

Expand All @@ -15,6 +17,20 @@
*/
abstract class ResourceRelationManager extends BaseRelationManager
{
/**
* Get the pivot fields of the relation.
*
* @return array<Component>
*/
public function getPivotFields(): array

Check failure on line 25 in app/Filament/RelationManagers/Wiki/ResourceRelationManager.php

View workflow job for this annotation

GitHub Actions / static-analysis

Method App\Filament\RelationManagers\Wiki\ResourceRelationManager::getPivotFields() has invalid return type App\Filament\RelationManagers\Wiki\Component.
{
return [

Check failure on line 27 in app/Filament/RelationManagers/Wiki/ResourceRelationManager.php

View workflow job for this annotation

GitHub Actions / static-analysis

Method App\Filament\RelationManagers\Wiki\ResourceRelationManager::getPivotFields() should return array<App\Filament\RelationManagers\Wiki\Component> but returns array<int, Filament\Forms\Components\TextInput>.
TextInput::make(AnimeResource::ATTRIBUTE_AS)
->label(__('filament.fields.anime.resources.as.name'))
->helperText(__('filament.fields.anime.resources.as.help')),
];
}

/**
* The form to the actions.
*
Expand Down
6 changes: 0 additions & 6 deletions app/Filament/Resources/Wiki/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,6 @@ public static function form(Form $form): Form
->columnSpan(2)
->maxLength(65535)
->rules('max:65535'),

TextInput::make(AnimeResource::ATTRIBUTE_AS)
->label(__('filament.fields.anime.resources.as.name'))
->helperText(__('filament.fields.anime.resources.as.help'))
->visibleOn(AnimeResourceRelationManager::class)
->placeholder('-'),
])
->columns(2);
}
Expand Down
8 changes: 7 additions & 1 deletion app/Filament/Resources/Wiki/Anime/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ public static function form(Form $form): Form
TextInput::make(ArtistSong::ATTRIBUTE_AS)
->label(__('filament.fields.artist.songs.as.name'))
->helperText(__('filament.fields.artist.songs.as.help')),

TextInput::make(ArtistSong::ATTRIBUTE_ALIAS)
->label(__('filament.fields.artist.songs.alias.name'))
->helperText(__('filament.fields.artist.songs.alias.help')),
])
->formatStateUsing(function (?array $state, Get $get) {
/** @var Song|null $song */
Expand All @@ -249,6 +253,7 @@ public static function form(Form $form): Form
foreach ($song->artists()->get() as $artist) {
$artists[] = [
Artist::ATTRIBUTE_ID => $artist->getKey(),
ArtistSong::ATTRIBUTE_ALIAS => Arr::get($artist, 'artistsong.alias'),
ArtistSong::ATTRIBUTE_AS => Arr::get($artist, 'artistsong.as'),
];
}
Expand All @@ -262,13 +267,14 @@ public static function form(Form $form): Form
$artists = [];
foreach ($state as $artist) {
$artists[Arr::get($artist, Artist::ATTRIBUTE_ID)] = [
ArtistSong::ATTRIBUTE_ALIAS => Arr::get($artist, ArtistSong::ATTRIBUTE_ALIAS),
ArtistSong::ATTRIBUTE_AS => Arr::get($artist, ArtistSong::ATTRIBUTE_AS)
];
}

$song->artists()->sync($artists);
})
->columns(2),
->columns(3),
]),

Tab::make('entries')
Expand Down
16 changes: 0 additions & 16 deletions app/Filament/Resources/Wiki/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use App\Filament\Resources\Wiki\ExternalResource\RelationManagers\ArtistResourceRelationManager;
use App\Filament\Resources\Wiki\Song\RelationManagers\ArtistSongRelationManager;
use App\Models\Wiki\Artist as ArtistModel;
use App\Pivots\Wiki\ArtistMember;
use App\Pivots\Wiki\ArtistResource;
use App\Pivots\Wiki\ArtistSong;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -155,21 +154,6 @@ public static function form(Form $form): Form
Slug::make(ArtistModel::ATTRIBUTE_SLUG)
->label(__('filament.fields.artist.slug.name'))
->helperText(__('filament.fields.artist.slug.help')),

TextInput::make(ArtistResource::ATTRIBUTE_AS)
->label(__('filament.fields.artist.resources.as.name'))
->helperText(__('filament.fields.artist.resources.as.help'))
->visibleOn(ArtistResourceRelationManager::class),

TextInput::make(ArtistSong::ATTRIBUTE_AS)
->label(__('filament.fields.artist.songs.as.name'))
->helperText(__('filament.fields.artist.songs.as.help'))
->visibleOn([ArtistSongRelationManager::class, MemberArtistRelationManager::class, GroupArtistRelationManager::class]),

TextInput::make(ArtistSong::ATTRIBUTE_ALIAS)
->label(__('filament.fields.artist.songs.alias.name'))
->helperText(__('filament.fields.artist.songs.alias.help'))
->visibleOn([ArtistSongRelationManager::class, MemberArtistRelationManager::class, GroupArtistRelationManager::class]),
])
->columns(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,34 @@

use App\Filament\RelationManagers\Wiki\ArtistRelationManager;
use App\Models\Wiki\Artist;
use App\Pivots\Wiki\ArtistMember;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Table;

/**
* Class GroupArtistRelationManager.
*/
class GroupArtistRelationManager extends ArtistRelationManager
{
/**
* Get the pivot fields of the relation.
*
* @return array<int, Component>
*/
public function getPivotFields(): array
{
return [
TextInput::make(ArtistMember::ATTRIBUTE_AS)
->label(__('filament.fields.artist.members.as.name'))
->helperText(__('filament.fields.artist.members.as.help')),

TextInput::make(ArtistMember::ATTRIBUTE_ALIAS)
->label(__('filament.fields.artist.members.alias.name'))
->helperText(__('filament.fields.artist.members.alias.help')),
];
}

/**
* The relationship the relation manager corresponds to.
*
Expand Down Expand Up @@ -70,7 +91,7 @@ public static function getActions(): array

/**
* Get the bulk actions available for the relation.
*
*
* @return array
*
* @noinspection PhpMissingParentCallCommonInspection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,34 @@

use App\Filament\RelationManagers\Wiki\ArtistRelationManager;
use App\Models\Wiki\Artist;
use App\Pivots\Wiki\ArtistMember;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Table;

/**
* Class MemberArtistRelationManager.
*/
class MemberArtistRelationManager extends ArtistRelationManager
{
/**
* Get the pivot fields of the relation.
*
* @return array<int, Component>
*/
public function getPivotFields(): array
{
return [
TextInput::make(ArtistMember::ATTRIBUTE_AS)
->label(__('filament.fields.artist.members.as.name'))
->helperText(__('filament.fields.artist.members.as.help')),

TextInput::make(ArtistMember::ATTRIBUTE_ALIAS)
->label(__('filament.fields.artist.members.alias.name'))
->helperText(__('filament.fields.artist.members.alias.help')),
];
}

/**
* The relationship the relation manager corresponds to.
*
Expand Down Expand Up @@ -70,7 +91,7 @@ public static function getActions(): array

/**
* Get the bulk actions available for the relation.
*
*
* @return array
*
* @noinspection PhpMissingParentCallCommonInspection
Expand Down
Loading

0 comments on commit fe9a241

Please sign in to comment.