v1.0.0
This commit is contained in:
123
src/app/Livewire/Client/NewTicket.php
Normal file
123
src/app/Livewire/Client/NewTicket.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Client;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Subcategory;
|
||||
use App\Services\TicketService;
|
||||
use App\Support\Settings;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
class NewTicket extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public int $step = 1;
|
||||
|
||||
public ?int $categoryId = null;
|
||||
|
||||
public ?int $subcategoryId = null;
|
||||
|
||||
public string $subject = '';
|
||||
|
||||
public string $body = '';
|
||||
|
||||
public array $customValues = [];
|
||||
|
||||
public array $attachments = [];
|
||||
|
||||
#[Computed]
|
||||
public function categories()
|
||||
{
|
||||
return Category::query()->with('subcategories')->get();
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function selectedCategory(): ?Category
|
||||
{
|
||||
return $this->categoryId ? Category::query()->with('subcategories')->find($this->categoryId) : null;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function selectedSubcategory(): ?Subcategory
|
||||
{
|
||||
return $this->subcategoryId ? Subcategory::query()->with('customFields')->find($this->subcategoryId) : null;
|
||||
}
|
||||
|
||||
public function selectCategory(int $id): void
|
||||
{
|
||||
$this->categoryId = $id;
|
||||
$this->subcategoryId = null;
|
||||
$this->step = 2;
|
||||
}
|
||||
|
||||
public function selectSubcategory(int $id): void
|
||||
{
|
||||
$this->subcategoryId = $id;
|
||||
$this->step = 3;
|
||||
}
|
||||
|
||||
public function backToCategory(): void
|
||||
{
|
||||
$this->step = 1;
|
||||
}
|
||||
|
||||
public function backToSubcategory(): void
|
||||
{
|
||||
$this->step = 2;
|
||||
}
|
||||
|
||||
public function updatedAttachments(): void
|
||||
{
|
||||
if (! $this->attachments) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($error = Settings::validateAttachments($this->attachments)) {
|
||||
$this->attachments = [];
|
||||
$this->addError('attachments', $error);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeAttachment(int $index): void
|
||||
{
|
||||
unset($this->attachments[$index]);
|
||||
$this->attachments = array_values($this->attachments);
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$rules = [
|
||||
'subject' => 'required|string|max:255',
|
||||
'body' => 'required|string',
|
||||
];
|
||||
|
||||
foreach ($this->selectedSubcategory?->customFields ?? [] as $field) {
|
||||
$rules["customValues.{$field->id}"] = $field->required ? 'required' : 'nullable';
|
||||
}
|
||||
|
||||
$this->validate($rules);
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
$ticket = app(TicketService::class)->create([
|
||||
'email' => $user->email,
|
||||
'subcategory_id' => $this->subcategoryId,
|
||||
'subject' => $this->subject,
|
||||
'body' => $this->body,
|
||||
'custom_values' => $this->customValues,
|
||||
], $user);
|
||||
|
||||
app(TicketService::class)->attachFiles($ticket, $ticket->messages()->first(), $this->attachments);
|
||||
|
||||
$this->redirect(route('client.ticket', $ticket), navigate: true);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.client.new-ticket');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user