feat(templates): importar de otro proyecto con tabla Rappasoft + selección múltiple
Sustituye el modal (proyecto + checkboxes) por una tabla Rappasoft: - ImportTemplatesTable lista todas las plantillas de proyectos accesibles (excluye el destino), con filtros en cabecera (nombre, proyecto) y checkbox de selección (bulk). - Acción masiva "Importar seleccionadas" copia al proyecto destino (dedupe por nombre) y avisa a TemplateManager por evento (templates-imported) para refrescar/cerrar. - TemplateManager: eliminada la lógica vieja (importProjectId/importableTemplates/ selectedImportTemplateIds/importFromProject); el modal embebe la tabla. Tests: TemplateImportTest adaptado (selección+import, excluye destino). Suite 92 passing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Livewire\Inspections;
|
||||
|
||||
use Livewire\Component;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\WithFileUploads;
|
||||
use App\Models\InspectionTemplate;
|
||||
use App\Models\Project;
|
||||
@@ -36,12 +37,8 @@ class TemplateManager extends Component
|
||||
public $importTemplateName = '';
|
||||
public $importError = '';
|
||||
|
||||
// ── Importar desde otro proyecto ──────────────────────────────────────
|
||||
public $showImportProjectModal = false;
|
||||
public $availableProjects = [];
|
||||
public $importProjectId = null;
|
||||
public $importableTemplates = [];
|
||||
public $selectedImportTemplateIds = [];
|
||||
// ── Importar desde otro proyecto (tabla Rappasoft) ─────────────────────
|
||||
public $showImportProjectModal = false;
|
||||
|
||||
public $fieldTypes = [
|
||||
'text' => 'Texto corto',
|
||||
@@ -369,73 +366,16 @@ class TemplateManager extends Component
|
||||
|
||||
public function openImportProjectModal()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$this->availableProjects = Project::accessibleBy($user)
|
||||
->where('id', '!=', $this->project->id)
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
$this->importProjectId = null;
|
||||
$this->importableTemplates = [];
|
||||
$this->selectedImportTemplateIds = [];
|
||||
$this->showImportProjectModal = true;
|
||||
$this->showImportProjectModal = true;
|
||||
}
|
||||
|
||||
public function updatedImportProjectId()
|
||||
/** La tabla Rappasoft embebida avisa cuando ha importado plantillas. */
|
||||
#[On('templates-imported')]
|
||||
public function onTemplatesImported(int $count = 0): void
|
||||
{
|
||||
$this->selectedImportTemplateIds = [];
|
||||
if (!$this->importProjectId) {
|
||||
$this->importableTemplates = [];
|
||||
return;
|
||||
}
|
||||
// Solo mostrar templates de proyectos accesibles
|
||||
$user = Auth::user();
|
||||
$allowed = Project::accessibleBy($user)->pluck('id');
|
||||
if (!$allowed->contains($this->importProjectId)) {
|
||||
$this->importableTemplates = [];
|
||||
return;
|
||||
}
|
||||
$this->importableTemplates = InspectionTemplate::where('project_id', $this->importProjectId)->get();
|
||||
}
|
||||
|
||||
public function importFromProject()
|
||||
{
|
||||
if (empty($this->selectedImportTemplateIds)) {
|
||||
$this->dispatch('notify', 'Selecciona al menos un template.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Verificar que los templates pertenecen a un proyecto accesible
|
||||
$user = Auth::user();
|
||||
$allowed = Project::accessibleBy($user)->pluck('id');
|
||||
|
||||
$imported = 0;
|
||||
foreach ($this->selectedImportTemplateIds as $templateId) {
|
||||
$source = InspectionTemplate::find($templateId);
|
||||
if (!$source || !$allowed->contains($source->project_id)) continue;
|
||||
|
||||
// Evitar duplicados por nombre
|
||||
$name = $source->name;
|
||||
if (InspectionTemplate::where('project_id', $this->project->id)->where('name', $name)->exists()) {
|
||||
$name .= ' (copia)';
|
||||
}
|
||||
|
||||
InspectionTemplate::create([
|
||||
'name' => $name,
|
||||
'description' => $source->description,
|
||||
'project_id' => $this->project->id,
|
||||
'phase_id' => null,
|
||||
'fields' => $source->fields,
|
||||
]);
|
||||
$imported++;
|
||||
}
|
||||
|
||||
$this->showImportProjectModal = false;
|
||||
$this->importProjectId = null;
|
||||
$this->importableTemplates = [];
|
||||
$this->selectedImportTemplateIds = [];
|
||||
$this->showImportProjectModal = false;
|
||||
$this->loadTemplates();
|
||||
$this->dispatch('notify', "$imported template(s) importado(s) desde otro proyecto");
|
||||
$this->dispatch('notify', "$count plantilla(s) importada(s) desde otro proyecto");
|
||||
}
|
||||
|
||||
public function render()
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Projects;
|
||||
|
||||
use App\Models\InspectionTemplate;
|
||||
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;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;
|
||||
|
||||
class ImportTemplatesTable extends DataTableComponent
|
||||
{
|
||||
protected $model = InspectionTemplate::class;
|
||||
|
||||
public int $projectId;
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setDefaultSort('inspection_templates.name', 'asc')
|
||||
->setSortingPillsEnabled(false)
|
||||
->setBulkActionsEnabled(true)
|
||||
->setAdditionalSelects([
|
||||
'inspection_templates.id as id',
|
||||
'inspection_templates.project_id as project_id',
|
||||
'inspection_templates.phase_id as phase_id',
|
||||
]);
|
||||
}
|
||||
|
||||
/** Sólo plantillas de proyectos accesibles, distintos del proyecto destino. */
|
||||
private function accessibleProjectIds()
|
||||
{
|
||||
return Project::accessibleBy(Auth::user())->pluck('id');
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
return InspectionTemplate::query()
|
||||
->whereIn('inspection_templates.project_id', $this->accessibleProjectIds())
|
||||
->where('inspection_templates.project_id', '!=', $this->projectId)
|
||||
->with(['project', 'phase']);
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make('Plantilla', 'name')
|
||||
->sortable()->searchable()
|
||||
->secondaryHeaderFilter('name')
|
||||
->format(fn ($value) => '<span class="font-medium">' . e($value) . '</span>')
|
||||
->html(),
|
||||
|
||||
Column::make('Proyecto')
|
||||
->secondaryHeaderFilter('project')
|
||||
->label(fn ($row) => e($row->project?->name ?? '—')),
|
||||
|
||||
Column::make('Fase')
|
||||
->label(fn ($row) => e($row->phase?->name ?? '—')),
|
||||
|
||||
Column::make('Campos')
|
||||
->label(fn ($row) => '<span class="badge badge-ghost badge-sm">' . count($row->fields ?? []) . '</span>')
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
$projects = Project::whereIn('id', InspectionTemplate::query()
|
||||
->whereIn('project_id', $this->accessibleProjectIds())
|
||||
->where('project_id', '!=', $this->projectId)
|
||||
->distinct()->pluck('project_id'))
|
||||
->orderBy('name')->pluck('name', 'id')->toArray();
|
||||
|
||||
return [
|
||||
TextFilter::make('Plantilla', 'name')
|
||||
->config(['placeholder' => 'Buscar nombre…'])
|
||||
->filter(fn (Builder $q, string $v) => $q->where('inspection_templates.name', 'like', '%' . $v . '%')),
|
||||
|
||||
SelectFilter::make('Proyecto', 'project')
|
||||
->options(['' => 'Todos'] + $projects)
|
||||
->filter(fn (Builder $q, string $v) => $q->where('inspection_templates.project_id', $v)),
|
||||
];
|
||||
}
|
||||
|
||||
public function bulkActions(): array
|
||||
{
|
||||
return ['importSelected' => 'Importar seleccionadas'];
|
||||
}
|
||||
|
||||
public function importSelected()
|
||||
{
|
||||
$allowed = $this->accessibleProjectIds();
|
||||
$imported = 0;
|
||||
|
||||
foreach (InspectionTemplate::whereIn('id', $this->getSelected())->get() as $source) {
|
||||
if (! $allowed->contains($source->project_id) || $source->project_id === $this->projectId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $source->name;
|
||||
if (InspectionTemplate::where('project_id', $this->projectId)->where('name', $name)->exists()) {
|
||||
$name .= ' (copia)';
|
||||
}
|
||||
|
||||
InspectionTemplate::create([
|
||||
'name' => $name,
|
||||
'description' => $source->description,
|
||||
'project_id' => $this->projectId,
|
||||
'phase_id' => null,
|
||||
'fields' => $source->fields,
|
||||
]);
|
||||
$imported++;
|
||||
}
|
||||
|
||||
$this->clearSelected();
|
||||
$this->dispatch('templates-imported', count: $imported);
|
||||
}
|
||||
}
|
||||
@@ -281,45 +281,18 @@
|
||||
@if($showImportProjectModal)
|
||||
<div class="fixed inset-0 z-40 bg-black/50" wire:click="$set('showImportProjectModal', false)"></div>
|
||||
<div class="fixed inset-0 z-50 flex items-center justify-center p-4">
|
||||
<div class="bg-base-100 rounded-box shadow-2xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
|
||||
<div class="bg-base-100 rounded-box shadow-2xl w-full max-w-4xl max-h-[90vh] overflow-y-auto">
|
||||
<div class="flex items-center justify-between p-4 border-b border-base-300">
|
||||
<h3 class="font-bold">{{ __('Import from another project') }}</h3>
|
||||
<div>
|
||||
<h3 class="font-bold">{{ __('Import from another project') }}</h3>
|
||||
<p class="text-xs text-base-content/60">{{ __('Mark the templates and use "Import selected".') }}</p>
|
||||
</div>
|
||||
<button wire:click="$set('showImportProjectModal', false)" class="btn btn-sm btn-ghost btn-circle">
|
||||
<x-heroicon-o-x-mark class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
<div class="p-4 space-y-3">
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text font-medium">{{ __('Project') }}</span></label>
|
||||
<select wire:model.live="importProjectId" class="select select-bordered w-full">
|
||||
<option value="">{{ __('Select project...') }}</option>
|
||||
@foreach($availableProjects as $p)
|
||||
<option value="{{ $p->id }}">{{ $p->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
|
||||
@if($importProjectId)
|
||||
@if(count($importableTemplates))
|
||||
<div class="border border-base-300 rounded-box p-2 space-y-1 max-h-64 overflow-y-auto">
|
||||
@foreach($importableTemplates as $t)
|
||||
<label class="flex items-center gap-2 p-1 hover:bg-base-200 rounded cursor-pointer">
|
||||
<input type="checkbox" wire:model="selectedImportTemplateIds" value="{{ $t->id }}" class="checkbox checkbox-sm" />
|
||||
<span class="text-sm">{{ $t->name }}</span>
|
||||
<span class="text-xs text-base-content/50">({{ count($t->fields ?? []) }} {{ __('fields') }})</span>
|
||||
</label>
|
||||
@endforeach
|
||||
</div>
|
||||
@else
|
||||
<p class="text-sm text-base-content/40 py-2">{{ __('This project has no templates.') }}</p>
|
||||
@endif
|
||||
@endif
|
||||
</div>
|
||||
<div class="flex justify-end gap-2 p-4 border-t border-base-300">
|
||||
<button wire:click="$set('showImportProjectModal', false)" class="btn btn-ghost btn-sm">{{ __('Cancel') }}</button>
|
||||
<button wire:click="importFromProject" class="btn btn-primary btn-sm" @disabled(empty($selectedImportTemplateIds))>
|
||||
{{ __('Import selected') }}
|
||||
</button>
|
||||
<div class="p-4">
|
||||
<livewire:projects.import-templates-table :project-id="$project->id" :key="'import-tpl-'.$project->id" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Livewire\Inspections\TemplateManager;
|
||||
use App\Livewire\Projects\ImportTemplatesTable;
|
||||
use App\Models\InspectionTemplate;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
@@ -38,19 +39,30 @@ class TemplateImportTest extends TestCase
|
||||
'fields' => [['name' => 'ok', 'label' => 'OK', 'type' => 'boolean']],
|
||||
]);
|
||||
|
||||
// Tabla Rappasoft: lista plantillas de otros proyectos, selección con checkbox.
|
||||
Livewire::actingAs($user)
|
||||
->test(TemplateManager::class, ['project' => $target])
|
||||
->call('openImportProjectModal')
|
||||
->set('importProjectId', $source->id)
|
||||
->set('selectedImportTemplateIds', [$tpl->id])
|
||||
->call('importFromProject')
|
||||
->assertHasNoErrors();
|
||||
->test(ImportTemplatesTable::class, ['projectId' => $target->id])
|
||||
->assertSee('Recepción acero')
|
||||
->set('selected', [(string) $tpl->id])
|
||||
->call('importSelected')
|
||||
->assertDispatched('templates-imported');
|
||||
|
||||
$this->assertDatabaseHas('inspection_templates', [
|
||||
'project_id' => $target->id, 'name' => 'Recepción acero',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_import_table_excludes_target_project_templates(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$target = $this->project($user, 'DST2');
|
||||
InspectionTemplate::create(['project_id' => $target->id, 'name' => 'Propia del destino', 'fields' => []]);
|
||||
|
||||
Livewire::actingAs($user)
|
||||
->test(ImportTemplatesTable::class, ['projectId' => $target->id])
|
||||
->assertDontSee('Propia del destino');
|
||||
}
|
||||
|
||||
public function test_import_template_from_csv(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Reference in New Issue
Block a user