Files
construprogress/app/Livewire/IssueManager.php
T

136 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Livewire;
use Livewire\Component;
use Livewire\Attributes\Layout;
use Illuminate\Support\Facades\Auth;
use App\Models\Project;
use App\Models\Issue;
use App\Notifications\IssueReportedNotification;
#[Layout('layouts.app')]
class IssueManager extends Component
{
public Project $project;
public $editing = false;
public $editingId = null;
public $title = '';
public $description = '';
public $status = 'open';
public $priority = 'medium';
public $featureId = null;
public $inspectionId = null;
public $assignedTo = null;
public $issues = [];
public function mount(Project $project)
{
$this->project = $project;
$this->loadIssues();
}
public function loadIssues()
{
$this->issues = Issue::where('project_id', $this->project->id)
->with(['feature', 'reporter', 'assignee'])
->orderBy('created_at', 'desc')
->get();
}
public function create()
{
$this->reset(['title', 'description', 'status', 'priority', 'featureId', 'inspectionId', 'assignedTo', 'editingId']);
$this->status = 'open';
$this->priority = 'medium';
$this->editing = true;
}
public function edit($issueId)
{
$issue = Issue::findOrFail($issueId);
$this->editingId = $issue->id;
$this->title = $issue->title;
$this->description = $issue->description ?? '';
$this->status = $issue->status;
$this->priority = $issue->priority;
$this->featureId = $issue->feature_id;
$this->inspectionId = $issue->inspection_id;
$this->assignedTo = $issue->assigned_to;
$this->editing = true;
}
public function save()
{
$this->validate([
'title' => 'required|string|max:255',
'status' => 'required|in:' . implode(',', Issue::STATUSES),
'priority' => 'required|in:' . implode(',', Issue::PRIORITIES),
]);
if ($this->editingId) {
$issue = Issue::findOrFail($this->editingId);
$issue->update([
'title' => $this->title,
'description' => $this->description,
'status' => $this->status,
'priority' => $this->priority,
'feature_id' => $this->featureId,
'inspection_id' => $this->inspectionId,
'assigned_to' => $this->assignedTo,
]);
} else {
$issue = Issue::create([
'project_id' => $this->project->id,
'title' => $this->title,
'description' => $this->description,
'status' => $this->status,
'priority' => $this->priority,
'feature_id' => $this->featureId,
'inspection_id' => $this->inspectionId,
'reported_by' => Auth::id(),
'assigned_to' => $this->assignedTo,
]);
if ($issue->wasRecentlyCreated) {
$issue->load(['feature', 'assignee']);
$creator = $this->project->creator;
if ($creator && $creator->id !== Auth::id()) {
$creator->notify(new IssueReportedNotification($issue));
}
if ($issue->assignee && $issue->assignee->id !== Auth::id()) {
$issue->assignee->notify(new IssueReportedNotification($issue));
}
}
}
$this->editing = false;
$this->loadIssues();
$this->dispatch('notify', 'Issue guardado correctamente');
}
public function delete($issueId)
{
$issue = Issue::where('project_id', $this->project->id)->findOrFail($issueId);
$issue->delete();
$this->loadIssues();
$this->dispatch('notify', 'Issue eliminado');
}
public function cancel()
{
$this->editing = false;
$this->reset(['title', 'description', 'status', 'priority', 'featureId', 'inspectionId', 'assignedTo', 'editingId']);
}
public function render()
{
return view('livewire.issue-manager');
}
}