añadir nuevas funcionalidades
This commit is contained in:
@@ -9,12 +9,15 @@ use App\Models\Project;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Document;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ProjectShow extends Component
|
||||
{
|
||||
|
||||
use WithFileUploads;
|
||||
|
||||
protected $middleware = ['auth']; // Añade esto
|
||||
|
||||
public Project $project;
|
||||
public ?Folder $currentFolder = null;
|
||||
public $expandedFolders = [];
|
||||
@@ -22,8 +25,13 @@ class ProjectShow extends Component
|
||||
public $folderName = '';
|
||||
|
||||
public $selectedFolderId = null;
|
||||
public $showFolderModal = false;
|
||||
public $showUploadModal = false;
|
||||
public $tempFiles = [];
|
||||
public $selectedFiles = []; // Archivos temporales en el modal
|
||||
public $uploadProgress = [];
|
||||
|
||||
|
||||
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project->load('rootFolders');
|
||||
@@ -57,27 +65,27 @@ class ProjectShow extends Component
|
||||
})
|
||||
]
|
||||
]);
|
||||
|
||||
Folder::create([
|
||||
'name' => $this->folderName,
|
||||
'project_id' => $this->project->id,
|
||||
'parent_id' => $this->currentFolder?->id
|
||||
'parent_id' => $this->currentFolder?->id,
|
||||
]);
|
||||
|
||||
$this->hideCreateFolderModal();
|
||||
$this->reset('folderName');
|
||||
$this->project->load('rootFolders'); // Recargar carpetas raíz
|
||||
$this->project->load('rootFolders'); // Recargar carpetas raíz
|
||||
if ($this->currentFolder) {
|
||||
$this->currentFolder->load('children'); // Recargar hijos si está en una subcarpeta
|
||||
}
|
||||
$this->project->refresh();
|
||||
}
|
||||
|
||||
public function uploadFiles(): void
|
||||
/*public function uploadFiles(): void
|
||||
{
|
||||
$this->validate([
|
||||
'files.*' => 'file|max:10240|mimes:pdf,docx,xlsx,jpg,png'
|
||||
]);
|
||||
|
||||
dd($this->files);
|
||||
foreach ($this->files as $file) {
|
||||
Document::create([
|
||||
'name' => $file->getClientOriginalName(),
|
||||
@@ -92,7 +100,7 @@ class ProjectShow extends Component
|
||||
$this->currentFolder->refresh(); // Recargar documentos
|
||||
}
|
||||
$this->reset('files');
|
||||
}
|
||||
}*/
|
||||
|
||||
public function getDocumentsProperty()
|
||||
{
|
||||
@@ -116,11 +124,128 @@ class ProjectShow extends Component
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
public function showCreateFolderModal()
|
||||
{
|
||||
$this->folderName = '';
|
||||
$this->showFolderModal = true;
|
||||
}
|
||||
|
||||
public function hideCreateFolderModal()
|
||||
{
|
||||
$this->showFolderModal = false;
|
||||
}
|
||||
|
||||
// Método para abrir el modal
|
||||
public function openUploadModal(): void
|
||||
{
|
||||
$this->showUploadModal = true;
|
||||
}
|
||||
|
||||
// Método para manejar archivos seleccionados
|
||||
public function selectFiles($files): void
|
||||
{
|
||||
$this->validate([
|
||||
'selectedFiles.*' => 'file|max:10240|mimes:pdf,docx,xlsx,jpg,png'
|
||||
]);
|
||||
|
||||
$this->selectedFiles = array_merge($this->selectedFiles, $files);
|
||||
}
|
||||
|
||||
// Método para eliminar un archivo de la lista
|
||||
public function removeFile($index): void
|
||||
{
|
||||
unset($this->selectedFiles[$index]);
|
||||
$this->selectedFiles = array_values($this->selectedFiles); // Reindexar array
|
||||
}
|
||||
|
||||
// Método para confirmar y guardar
|
||||
public function uploadFiles(): void
|
||||
{
|
||||
foreach ($this->selectedFiles as $file) {
|
||||
Document::create([
|
||||
'name' => $file->getClientOriginalName(),
|
||||
'file_path' => $file->store("projects/{$this->project->id}/documents"),
|
||||
'project_id' => $this->project->id, // Asegurar que se envía
|
||||
'folder_id' => $this->currentFolder?->id,
|
||||
//'user_id' => Auth::id(),
|
||||
//'status' => 'active' // Añadir si tu modelo lo requiere
|
||||
]);
|
||||
}
|
||||
|
||||
$this->resetUpload();
|
||||
$this->project->refresh();
|
||||
}
|
||||
|
||||
// Método para procesar los archivos
|
||||
protected function processFiles(): void
|
||||
{
|
||||
foreach ($this->files as $file) {
|
||||
Document::create([
|
||||
'name' => $file->getClientOriginalName(),
|
||||
'file_path' => $file->store("projects/{$this->project->id}/documents"),
|
||||
'project_id' => $this->project->id, // Asegurar que se envía
|
||||
'folder_id' => $this->currentFolder?->id,
|
||||
//'user_id' => Auth::id(),
|
||||
//'status' => 'active' // Añadir si tu modelo lo requiere
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Método para resetear
|
||||
public function resetUpload(): void
|
||||
{
|
||||
$this->reset(['selectedFiles', 'showUploadModal', 'uploadProgress']);
|
||||
}
|
||||
|
||||
#[On('upload-progress')]
|
||||
public function updateProgress($name, $progress)
|
||||
{
|
||||
$this->uploadProgress[$name] = $progress;
|
||||
}
|
||||
|
||||
public function addFiles($files)
|
||||
{
|
||||
$this->validate([
|
||||
'selectedFiles.*' => 'file|max:10240|mimes:pdf,docx,xlsx,jpg,png'
|
||||
]);
|
||||
|
||||
$this->selectedFiles = array_merge($this->selectedFiles, $files);
|
||||
}
|
||||
|
||||
public function startUpload()
|
||||
{
|
||||
foreach ($this->selectedFiles as $file) {
|
||||
try {
|
||||
$path = $file->store(
|
||||
"projects/{$this->project->id}/".($this->currentFolder ? "folders/{$this->currentFolder->id}" : ""),
|
||||
'public'
|
||||
);
|
||||
|
||||
Document::create([
|
||||
'name' => $file->getClientOriginalName(),
|
||||
'file_path' => $path,
|
||||
'project_id' => $this->project->id,
|
||||
'folder_id' => $this->currentFolder?->id,
|
||||
'user_id' => Auth::id()
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->addError('upload', "Error subiendo {$file->getClientOriginalName()}: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
|
||||
$this->resetUpload();
|
||||
$this->project->refresh();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.project-show')
|
||||
->layout('layouts.livewire-app', [
|
||||
'title' => $this->project->name
|
||||
]);
|
||||
return view('livewire.project.show')->layout('components.layouts.documents', [
|
||||
'title' => __('Project: :name', ['name' => $this->project->name]),
|
||||
'breadcrumbs' => [
|
||||
['name' => __('Projects'), 'url' => route('projects.index')],
|
||||
['name' => $this->project->name, 'url' => route('projects.show', $this->project)],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user