feat(map): listas de elementos e inspecciones con tablas Rappasoft
- FeatureTable e InspectionTable (DataTableComponent) sustituyen las tablas HTML de las pestañas "Elementos" e "Inspecciones" del mapa: búsqueda, orden, filtro (progreso) y acciones. - Selección de elemento e "ver inspección" se comunican al ProjectMap por eventos (map-select-feature / map-view-inspection, vía #[On]); seleccionar abre el panel de edición y centra el mapa igual que antes. - Las relaciones requieren sus FKs en additionalSelects (layer_id / feature_id, template_id, user_id) porque Rappasoft no selecciona '*'. Tests: MapTablesTest (3). Suite 74 passing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\Feature;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
|
||||
|
||||
class FeatureTable extends DataTableComponent
|
||||
{
|
||||
protected $model = Feature::class;
|
||||
|
||||
public int $projectId;
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('name', 'asc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setAdditionalSelects(['features.id as id', 'features.layer_id as layer_id']);
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
$user = Auth::user();
|
||||
abort_unless(
|
||||
$user->can('manage all') ||
|
||||
Project::whereKey($this->projectId)->whereHas('users', fn ($q) => $q->where('user_id', $user->id))->exists(),
|
||||
403
|
||||
);
|
||||
|
||||
return Feature::query()
|
||||
->whereHas('layer.phase', fn ($q) => $q->where('project_id', $this->projectId))
|
||||
->with(['layer.phase']);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Elemento', 'name')
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(fn ($value) => '<span class="font-medium">' . e($value) . '</span>')
|
||||
->html(),
|
||||
|
||||
Column::make('Capa')
|
||||
->label(fn ($row) => e($row->layer?->name ?? '—')),
|
||||
|
||||
Column::make('Fase')
|
||||
->label(fn ($row) => e($row->layer?->phase?->name ?? '—')),
|
||||
|
||||
Column::make('Progreso', 'progress')
|
||||
->sortable()
|
||||
->format(function ($value) {
|
||||
$cls = $value >= 100 ? 'badge-success' : ($value > 0 ? 'badge-warning' : 'badge-ghost');
|
||||
return '<span class="badge badge-sm ' . $cls . '">' . (int) $value . '%</span>';
|
||||
})
|
||||
->html(),
|
||||
|
||||
Column::make('Acciones')
|
||||
->label(fn ($row) =>
|
||||
'<div class="flex justify-end">
|
||||
<button wire:click="$dispatch(\'map-select-feature\', { id: ' . $row->id . ' })"
|
||||
class="btn btn-xs btn-primary gap-1" title="Editar elemento">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
|
||||
Abrir
|
||||
</button>
|
||||
</div>')
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
return [
|
||||
SelectFilter::make('Progreso', 'progress')
|
||||
->options(['' => 'Progreso: todos', 'pending' => 'Sin iniciar', 'in_progress' => 'En curso', 'done' => 'Completado'])
|
||||
->filter(function (Builder $query, string $value) {
|
||||
match ($value) {
|
||||
'pending' => $query->where('features.progress', '=', 0),
|
||||
'in_progress' => $query->where('features.progress', '>', 0)->where('features.progress', '<', 100),
|
||||
'done' => $query->where('features.progress', '>=', 100),
|
||||
default => null,
|
||||
};
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\Inspection;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
|
||||
class InspectionTable extends DataTableComponent
|
||||
{
|
||||
protected $model = Inspection::class;
|
||||
|
||||
public int $projectId;
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('inspections.created_at', 'desc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setAdditionalSelects([
|
||||
'inspections.id as id',
|
||||
'inspections.created_at as created_at',
|
||||
'inspections.feature_id as feature_id',
|
||||
'inspections.template_id as template_id',
|
||||
'inspections.user_id as user_id',
|
||||
]);
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
$user = Auth::user();
|
||||
abort_unless(
|
||||
$user->can('manage all') ||
|
||||
Project::whereKey($this->projectId)->whereHas('users', fn ($q) => $q->where('user_id', $user->id))->exists(),
|
||||
403
|
||||
);
|
||||
|
||||
return Inspection::query()
|
||||
->where('inspections.project_id', $this->projectId)
|
||||
->with(['feature', 'template', 'user']);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Fecha', 'created_at')
|
||||
->sortable()
|
||||
->format(fn ($value, $row) => $row->created_at?->format('d/m/Y') ?? '—'),
|
||||
|
||||
Column::make('Elemento')
|
||||
->label(fn ($row) => $row->feature?->name
|
||||
? '<span class="font-medium">' . e($row->feature->name) . '</span>'
|
||||
: '<span class="text-base-content/30 text-xs">—</span>')
|
||||
->html(),
|
||||
|
||||
Column::make('Plantilla')
|
||||
->label(fn ($row) => e($row->template?->name ?? '—')),
|
||||
|
||||
Column::make('Resultado', 'result')
|
||||
->sortable()
|
||||
->format(fn ($value) => $value
|
||||
? '<span class="badge badge-sm badge-outline">' . e($value) . '</span>'
|
||||
: '<span class="text-base-content/30 text-xs">—</span>')
|
||||
->html(),
|
||||
|
||||
Column::make('Usuario')
|
||||
->label(fn ($row) => e($row->user?->name ?? '—')),
|
||||
|
||||
Column::make('Acciones')
|
||||
->label(fn ($row) =>
|
||||
'<div class="flex justify-end">
|
||||
<button wire:click="$dispatch(\'map-view-inspection\', { id: ' . $row->id . ' })"
|
||||
class="btn btn-xs btn-ghost" title="Ver inspección">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
|
||||
</button>
|
||||
</div>')
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\Attributes\On;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\Project;
|
||||
use App\Models\Phase;
|
||||
@@ -207,6 +208,7 @@ class ProjectMap extends Component
|
||||
}
|
||||
}
|
||||
|
||||
#[On('map-select-feature')]
|
||||
public function selectFeature($featureId)
|
||||
{
|
||||
$this->selectedFeature = null;
|
||||
@@ -357,6 +359,7 @@ class ProjectMap extends Component
|
||||
|
||||
// ─── Inspection viewer ───────────────────────────────────────────────────────
|
||||
|
||||
#[On('map-view-inspection')]
|
||||
public function viewInspection($id)
|
||||
{
|
||||
$ins = Inspection::where('project_id', $this->project->id)
|
||||
|
||||
@@ -276,79 +276,11 @@
|
||||
</div>
|
||||
@endif
|
||||
@elseif($activeTab === 'features')
|
||||
<!-- Features Table -->
|
||||
@if($allFeatures->isNotEmpty())
|
||||
<div class="overflow-x-auto rounded-lg border border-base-300">
|
||||
<table class="table table-sm table-zebra table-pin-rows">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ __('Feature') }}</th>
|
||||
<th>{{ __('Layer') }}</th>
|
||||
<th>{{ __('Phase') }}</th>
|
||||
<th class="text-center">{{ __('Progress') }}</th>
|
||||
<th class="w-10"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($allFeatures as $feature)
|
||||
<tr class="hover cursor-pointer" wire:click="selectFeature({{ $feature->id }})" wire:key="feat-{{ $feature->id }}">
|
||||
<td class="font-medium">{{ $feature->name }}</td>
|
||||
<td>{{ $feature->layer?->name ?? '—' }}</td>
|
||||
<td>{{ $feature->layer?->phase?->name ?? '—' }}</td>
|
||||
<td class="text-center">
|
||||
<span class="badge badge-sm {{ $feature->progress >= 100 ? 'badge-success' : ($feature->progress > 0 ? 'badge-warning' : 'badge-ghost') }}">{{ $feature->progress }}%</span>
|
||||
</td>
|
||||
<td class="text-right">
|
||||
<x-heroicon-o-chevron-right class="w-4 h-4 opacity-40" />
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@else
|
||||
<div class="text-center text-gray-400 py-8">
|
||||
<x-heroicon-o-clipboard-document-list class="w-10 h-10 mx-auto opacity-30" />
|
||||
<p>{{ __('No elements in this project') }}</p>
|
||||
</div>
|
||||
@endif
|
||||
<!-- Elementos (tabla Rappasoft) -->
|
||||
<livewire:projects.feature-table :project-id="$project->id" :key="'feature-table-'.$project->id" />
|
||||
@elseif($activeTab === 'inspections')
|
||||
<!-- Inspections Table -->
|
||||
@if($allInspections->isNotEmpty())
|
||||
<div class="overflow-x-auto rounded-lg border border-base-300">
|
||||
<table class="table table-sm table-zebra table-pin-rows">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ __('Date') }}</th>
|
||||
<th>{{ __('Feature') }}</th>
|
||||
<th>{{ __('Template') }}</th>
|
||||
<th>{{ __('User') }}</th>
|
||||
<th class="w-10"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($allInspections as $inspection)
|
||||
<tr class="hover" wire:key="insp-{{ $inspection->id }}">
|
||||
<td class="whitespace-nowrap">{{ $inspection->created_at?->format('d/m/Y') ?? '—' }}</td>
|
||||
<td class="font-medium">{{ $inspection->feature?->name ?? '—' }}</td>
|
||||
<td>{{ $inspection->template?->name ?? '—' }}</td>
|
||||
<td>{{ $inspection->user?->name ?? '—' }}</td>
|
||||
<td class="text-right">
|
||||
<button wire:click="viewInspection({{ $inspection->id }})" class="btn btn-xs btn-ghost" title="{{ __('View') }}">
|
||||
<x-heroicon-o-eye class="w-4 h-4" />
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@else
|
||||
<div class="text-center text-gray-400 py-8">
|
||||
<x-heroicon-o-clipboard-document-list class="w-10 h-10 mx-auto opacity-30" />
|
||||
<p>{{ __('No inspections registered') }}</p>
|
||||
</div>
|
||||
@endif
|
||||
<!-- Inspecciones (tabla Rappasoft) -->
|
||||
<livewire:projects.inspection-table :project-id="$project->id" :key="'inspection-table-'.$project->id" />
|
||||
@elseif($activeTab === 'issues')
|
||||
<!-- Issues tab: render embedded IssueManager component -->
|
||||
@livewire('issues.issue-manager', ['project' => $project], key('issues-tab-' . $project->id))
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Livewire\Projects\FeatureTable;
|
||||
use App\Livewire\Projects\InspectionTable;
|
||||
use App\Models\Feature;
|
||||
use App\Models\Inspection;
|
||||
use App\Models\Layer;
|
||||
use App\Models\Phase;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class MapTablesTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $user;
|
||||
private Project $project;
|
||||
private Feature $feature;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = User::factory()->create();
|
||||
$this->project = Project::create([
|
||||
'reference' => 'MAP-1', 'name' => 'Proyecto Mapa', 'address' => 'x',
|
||||
'lat' => 40.0, 'lng' => -3.0, 'start_date' => now()->toDateString(),
|
||||
'end_date_estimated' => now()->addMonth()->toDateString(),
|
||||
'status' => 'in_progress', 'created_by' => $this->user->id,
|
||||
]);
|
||||
$this->project->users()->attach($this->user->id, ['role_in_project' => 'supervisor']);
|
||||
|
||||
$phase = Phase::create(['project_id' => $this->project->id, 'name' => 'Fase 1', 'order' => 1, 'color' => '#3b82f6', 'progress_percent' => 0]);
|
||||
$layer = Layer::create(['project_id' => $this->project->id, 'phase_id' => $phase->id, 'name' => 'Capa A', 'color' => '#10b981', 'uploaded_by' => $this->user->id]);
|
||||
$this->feature = Feature::create([
|
||||
'layer_id' => $layer->id, 'name' => 'Pilar 12',
|
||||
'geometry' => ['type' => 'Point', 'coordinates' => [-3.0, 40.0]],
|
||||
'progress' => 50, 'status' => 'in_progress',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_feature_table_lists_project_features(): void
|
||||
{
|
||||
Livewire::actingAs($this->user)
|
||||
->test(FeatureTable::class, ['projectId' => $this->project->id])
|
||||
->assertOk()
|
||||
->assertSee('Pilar 12')
|
||||
->assertSee('Capa A');
|
||||
}
|
||||
|
||||
public function test_feature_table_forbidden_for_non_member(): void
|
||||
{
|
||||
$outsider = User::factory()->create();
|
||||
Livewire::actingAs($outsider)
|
||||
->test(FeatureTable::class, ['projectId' => $this->project->id])
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_inspection_table_lists_project_inspections(): void
|
||||
{
|
||||
Inspection::create([
|
||||
'project_id' => $this->project->id,
|
||||
'layer_id' => $this->feature->layer_id,
|
||||
'feature_id' => $this->feature->id,
|
||||
'user_id' => $this->user->id,
|
||||
'data' => ['ok' => true],
|
||||
'status' => 'completed',
|
||||
'result' => 'pass',
|
||||
]);
|
||||
|
||||
Livewire::actingAs($this->user)
|
||||
->test(InspectionTable::class, ['projectId' => $this->project->id])
|
||||
->assertOk()
|
||||
->assertSee('Pilar 12')
|
||||
->assertSee('pass');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user