1
0
Fork 0
mirror of https://github.com/archtechx/airwire-demo.git synced 2025-12-12 08:34:03 +00:00

public release

This commit is contained in:
Samuel Štancl 2021-05-21 18:38:26 +02:00
commit d6d22f8355
115 changed files with 67218 additions and 0 deletions

View file

@ -0,0 +1,70 @@
<?php
namespace App\Airwire;
use Airwire\Attributes\Wired;
use Airwire\Component;
use App\Models\Report;
use App\Models\User;
use Exception;
use Illuminate\Validation\Rule;
class CreateReport extends Component
{
#[Wired]
public string $name;
#[Wired]
public int $assignee;
#[Wired]
public int $category;
public function rules()
{
return [
'name' => ['required', 'min:10', 'max:35'],
'assignee' => ['required', 'exists:users,id'],
'category' => ['required', Rule::in([1, 2, 3])],
];
}
public function mount(): array
{
return [
'readonly' => [
'users' => User::all()->values()->toArray(),
],
];
}
public function updatedAssignee(int $assignee)
{
if (isset($this->changes['name'])) {
// Don't override user-initiated name changes
return;
}
$this->name = 'Report for ' . User::find($assignee)->name;
}
#[Wired]
public function create(): Report
{
if ($found = Report::firstWhere('name', $this->name)) {
throw new Exception('Report with this name already exists. Please see report ' . $found->id);
}
$report = Report::create([
'name' => $this->name,
'assignee_id' => $this->assignee,
'category' => $this->category,
]);
$this->meta('notification', __('reports.created', ['id' => $report->id, 'name' => $report->name]));
$this->reset();
return $report;
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace App\Airwire;
use Airwire\Attributes\Wired;
use Airwire\Component;
use App\Models\User;
use Exception;
class CreateUser extends Component
{
public bool $strictValidation = false;
#[Wired]
public string $name = '';
#[Wired]
public string $email = '';
#[Wired]
public string $password = '';
#[Wired]
public string $password_confirmation = '';
public function rules()
{
return [
'name' => ['required', 'min:5', 'max:25', 'unique:users'],
'email' => ['required', 'unique:users'],
'password' => ['required', 'min:8', 'confirmed'],
];
}
#[Wired]
public function create(): User
{
$user = User::create($this->validated());
$this->meta('notification', __('users.created', ['id' => $user->id, 'name' => $user->name]));
$this->reset();
return $user;
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace App\Airwire;
use Airwire\Attributes\Wired;
use Airwire\Component;
use App\Models\Report;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;
class ReportFilter extends Component
{
public function rules()
{
return [
'search' => ['nullable', 'string', 'max:10'],
'assignee' => ['required', 'exists:users,id'],
'category' => ['nullable', Rule::in([1, 2, 3])],
'status' => ['nullable', Rule::in(['pending', 'resolved', 'invalid'])],
];
}
#[Wired]
public string $search = '';
#[Wired]
public int $assignee;
#[Wired]
public int $category;
#[Wired]
public string $status;
#[Wired(default: [], readonly: true, type: 'Report[]')]
public Collection $reports;
public function mount(): array
{
return [
'readonly' => [
'users' => User::all()->values()->toArray(),
],
];
}
#[Wired]
public function changeStatus(Report $report): string
{
$report->update([
// We shift the status to the next one
'status' => [
'pending' => 'resolved',
'resolved' => 'invalid',
'invalid' => 'pending'
][$report->status]
]);
$this->meta('notification', __('reports.status_changed', ['status' => $report->status]));
return $report->status;
}
public function dehydrate()
{
$this->reports = Report::query()
->where('name', 'LIKE', "%{$this->search}%")->with('assignee')
->when(isset($this->assignee), fn (Builder $query) => $query->where('assignee_id', $this->assignee))
->when(isset($this->category), fn (Builder $query) => $query->where('category', $this->category))
->when(isset($this->status), fn (Builder $query) => $query->where('status', $this->status))
->get();
}
}