Перевод защищённой части приложения на Vue.js + Inertia
Дашборд, зоны, устройства, правила автоматизации и профиль теперь Vue 3 + Inertia вместо Blade+Alpine — гостевые страницы Breeze (логин/регистрация) сознательно оставлены на Blade, чтобы не трогать уже протестированную аутентификацию. Контроллеры возвращают Inertia::render() вместо view(), переиспользуют существующие API Resources и FormRequest. Ключевой нюанс: Resource/ResourceCollection, переданные напрямую в проп Inertia, заворачиваются в ключ "data" (Inertia вызывает toResponse() у Responsable-объектов) — везде используется ->resolve() и явное резолвление вложенных ресурсов (zone/device_type внутри Device и т.п.), иначе вложенные поля тоже задваивались бы обёрткой. Alpine.js и все Blade-вьюхи защищённой части удалены как мёртвый код. Тесты (ZoneControllerTest/DeviceControllerTest) переведены на assertInertia(). UserFactory теперь явно задаёт role=owner — без этого Eloquent не подтягивает DB-default обратно в модель после create(), что уронило бы общий auth.user проп на любой странице.
This commit is contained in:
@@ -2,12 +2,18 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\ConditionOperator;
|
||||
use App\Enums\DeviceCategory;
|
||||
use App\Http\Requests\AutomationRuleRequest;
|
||||
use App\Http\Resources\AutomationRuleResource;
|
||||
use App\Http\Resources\DeviceResource;
|
||||
use App\Http\Resources\ZoneResource;
|
||||
use App\Models\AutomationRule;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceType;
|
||||
use App\Models\Zone;
|
||||
use Illuminate\Support\Collection;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class AutomationRuleController extends Controller
|
||||
{
|
||||
@@ -19,14 +25,16 @@ class AutomationRuleController extends Controller
|
||||
->orderByDesc('id')
|
||||
->get();
|
||||
|
||||
return view('automation-rules.index', ['rules' => $rules]);
|
||||
return Inertia::render('AutomationRules/Index', [
|
||||
'rules' => AutomationRuleResource::collection($rules)->resolve(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->authorize('create', AutomationRule::class);
|
||||
|
||||
return view('automation-rules.create', $this->formOptions());
|
||||
return Inertia::render('AutomationRules/Create', $this->formOptions());
|
||||
}
|
||||
|
||||
public function store(AutomationRuleRequest $request)
|
||||
@@ -42,8 +50,19 @@ class AutomationRuleController extends Controller
|
||||
{
|
||||
$this->authorize('update', $automation_rule);
|
||||
|
||||
return view('automation-rules.edit', [
|
||||
'rule' => $automation_rule,
|
||||
return Inertia::render('AutomationRules/Edit', [
|
||||
'rule' => [
|
||||
'id' => $automation_rule->id,
|
||||
'zone_id' => $automation_rule->zone_id,
|
||||
'condition_source_device_id' => $automation_rule->condition_source_device_id,
|
||||
'condition_sensor_type' => $automation_rule->condition_sensor_type,
|
||||
'condition_operator' => $automation_rule->condition_operator->value,
|
||||
'condition_value' => $automation_rule->condition_value,
|
||||
'target_device_id' => $automation_rule->target_device_id,
|
||||
'action_type' => $automation_rule->action_type,
|
||||
'level' => $automation_rule->action_params['level'] ?? '',
|
||||
'is_active' => $automation_rule->is_active,
|
||||
],
|
||||
...$this->formOptions(),
|
||||
]);
|
||||
}
|
||||
@@ -91,10 +110,11 @@ class AutomationRuleController extends Controller
|
||||
$devices = Device::with(['zone', 'deviceType'])->orderBy('name')->get();
|
||||
|
||||
return [
|
||||
'zones' => Zone::orderBy('name')->get(),
|
||||
'devices' => $devices,
|
||||
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
|
||||
'devices' => DeviceResource::collection($devices)->resolve(),
|
||||
'actionTypeOptions' => $this->capabilityOptions(DeviceCategory::Actuator),
|
||||
'sensorTypeOptions' => $this->capabilityOptions(DeviceCategory::Sensor),
|
||||
'conditionOperatorOptions' => array_map(fn ($case) => $case->value, ConditionOperator::cases()),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -102,7 +122,7 @@ class AutomationRuleController extends Controller
|
||||
* Distinct capability values across device_types of the given category —
|
||||
* used as friendly select/datalist suggestions, not a hardcoded list.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection<int, string>
|
||||
* @return Collection<int, string>
|
||||
*/
|
||||
private function capabilityOptions(DeviceCategory $category)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user