From 2391521ef742d3984f5b864b332b08352966cc7e Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Mon, 30 Dec 2024 10:58:46 +0000 Subject: [PATCH] Feature/issue 154 attach components to incidents on create (#158) Co-authored-by: AlexJump24 Co-authored-by: James Brooks --- resources/lang/en.json | 2 ++ src/Filament/Resources/IncidentResource.php | 25 ++++++++++++++++----- src/Models/Incident.php | 9 ++++++++ src/Models/IncidentComponent.php | 4 +++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/resources/lang/en.json b/resources/lang/en.json index 5900028e..420422e6 100644 --- a/resources/lang/en.json +++ b/resources/lang/en.json @@ -2,6 +2,7 @@ "+:count Update": "+:count Update|+:count Updates", ":count Incident": ":count Incident|:count Incidents", ":name RSS Feed": ":name RSS Feed", + "Add Component": "Add Component", "Admin": "Admin", "Always Hidden": "Always Hidden", "Always collapsed": "Always collapsed", @@ -12,6 +13,7 @@ "Complete Maintenance": "Complete Maintenance", "Complete": "Complete", "Component Group": "Component Group", + "Components": "Components", "Custom CSS": "Custom CSS", "Custom Footer HTML": "Custom Footer HTML", "Custom Header HTML": "Custom Header HTML", diff --git a/src/Filament/Resources/IncidentResource.php b/src/Filament/Resources/IncidentResource.php index 12a215dd..586a539c 100644 --- a/src/Filament/Resources/IncidentResource.php +++ b/src/Filament/Resources/IncidentResource.php @@ -4,6 +4,7 @@ use Cachet\Actions\Update\CreateUpdate as CreateIncidentUpdateAction; use Cachet\Data\IncidentUpdate\CreateIncidentUpdateData; +use Cachet\Enums\ComponentStatusEnum; use Cachet\Enums\IncidentStatusEnum; use Cachet\Enums\ResourceVisibilityEnum; use Cachet\Filament\Resources\IncidentResource\Pages; @@ -54,11 +55,25 @@ public static function form(Form $form): Form ->options(ResourceVisibilityEnum::class) ->default(ResourceVisibilityEnum::guest) ->required(), - // Forms\Components\Select::make('component') - // ->multiple() - // ->relationship('components', 'name') - // ->searchable() - // ->preload(), + Forms\Components\Repeater::make('incidentComponents') + ->visibleOn('create') + ->relationship() + ->defaultItems(0) + ->addActionLabel(__('Add Component')) + ->schema([ + Forms\Components\Select::make('component_id') + ->preload() + ->required() + ->relationship('component', 'name') + ->disableOptionsWhenSelectedInSiblingRepeaterItems() + ->label(__('Component')), + Forms\Components\ToggleButtons::make('component_status') + ->label(__('Status')) + ->inline() + ->options(ComponentStatusEnum::class) + ->required(), + ]) + ->label(__('Components')) ]) ->columnSpan(3), Section::make()->schema([ diff --git a/src/Models/Incident.php b/src/Models/Incident.php index f31abd5a..1953397c 100644 --- a/src/Models/Incident.php +++ b/src/Models/Incident.php @@ -19,6 +19,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Collection; @@ -117,6 +118,14 @@ public function components(): BelongsToMany ->withPivot(['component_status']); } + /** + * Get the impacted components for this incident. + */ + public function incidentComponents(): HasMany + { + return $this->hasMany(IncidentComponent::class); + } + /** * Get the updates for this incident. * diff --git a/src/Models/IncidentComponent.php b/src/Models/IncidentComponent.php index 85840839..e10f41e0 100644 --- a/src/Models/IncidentComponent.php +++ b/src/Models/IncidentComponent.php @@ -14,7 +14,7 @@ * @property int $id * @property int $incident_id * @property int $component_id - * @property ?ComponentStatusEnum $status + * @property ?ComponentStatusEnum $component_status * @property ?Carbon $created_at * @property ?Carbon $updated_at * @property Incident $incident @@ -24,6 +24,8 @@ */ class IncidentComponent extends Pivot { + protected $table = 'incident_components'; + /** @use HasFactory */ use HasFactory;