Перевод защищённой части приложения на 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:
@@ -0,0 +1,34 @@
|
||||
<script setup>
|
||||
import AppLayout from '@/Layouts/AppLayout.vue';
|
||||
import Form from '@/Pages/AutomationRules/Form.vue';
|
||||
|
||||
defineProps({
|
||||
zones: { type: Array, required: true },
|
||||
devices: { type: Array, required: true },
|
||||
actionTypeOptions: { type: Array, required: true },
|
||||
sensorTypeOptions: { type: Array, required: true },
|
||||
conditionOperatorOptions: { type: Array, required: true },
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AppLayout>
|
||||
<template #header>
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Новое правило</h2>
|
||||
</template>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||
<Form
|
||||
:zones="zones"
|
||||
:devices="devices"
|
||||
:action-type-options="actionTypeOptions"
|
||||
:sensor-type-options="sensorTypeOptions"
|
||||
:condition-operator-options="conditionOperatorOptions"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</AppLayout>
|
||||
</template>
|
||||
@@ -0,0 +1,36 @@
|
||||
<script setup>
|
||||
import AppLayout from '@/Layouts/AppLayout.vue';
|
||||
import Form from '@/Pages/AutomationRules/Form.vue';
|
||||
|
||||
defineProps({
|
||||
rule: { type: Object, required: true },
|
||||
zones: { type: Array, required: true },
|
||||
devices: { type: Array, required: true },
|
||||
actionTypeOptions: { type: Array, required: true },
|
||||
sensorTypeOptions: { type: Array, required: true },
|
||||
conditionOperatorOptions: { type: Array, required: true },
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AppLayout>
|
||||
<template #header>
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Изменить правило</h2>
|
||||
</template>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||
<Form
|
||||
:rule="rule"
|
||||
:zones="zones"
|
||||
:devices="devices"
|
||||
:action-type-options="actionTypeOptions"
|
||||
:sensor-type-options="sensorTypeOptions"
|
||||
:condition-operator-options="conditionOperatorOptions"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</AppLayout>
|
||||
</template>
|
||||
@@ -0,0 +1,176 @@
|
||||
<script setup>
|
||||
import { useForm } from '@inertiajs/vue3';
|
||||
import { routes } from '@/routes';
|
||||
|
||||
const props = defineProps({
|
||||
rule: { type: Object, default: null },
|
||||
zones: { type: Array, required: true },
|
||||
devices: { type: Array, required: true },
|
||||
actionTypeOptions: { type: Array, required: true },
|
||||
sensorTypeOptions: { type: Array, required: true },
|
||||
conditionOperatorOptions: { type: Array, required: true },
|
||||
});
|
||||
|
||||
const isEdit = !!props.rule;
|
||||
|
||||
const form = useForm({
|
||||
zone_id: props.rule?.zone_id ?? '',
|
||||
condition_source_device_id: props.rule?.condition_source_device_id ?? '',
|
||||
condition_sensor_type: props.rule?.condition_sensor_type ?? '',
|
||||
condition_operator: props.rule?.condition_operator ?? '',
|
||||
condition_value: props.rule?.condition_value ?? '',
|
||||
target_device_id: props.rule?.target_device_id ?? '',
|
||||
action_type: props.rule?.action_type ?? '',
|
||||
level: props.rule?.level ?? '',
|
||||
is_active: props.rule?.is_active ?? true,
|
||||
});
|
||||
|
||||
function submit() {
|
||||
if (isEdit) {
|
||||
form.put(routes.automationRules.update(props.rule.id));
|
||||
} else {
|
||||
form.post(routes.automationRules.store);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form @submit.prevent="submit">
|
||||
<div>
|
||||
<label for="zone_id" class="block font-medium text-sm text-gray-700">Зона</label>
|
||||
<select
|
||||
id="zone_id"
|
||||
v-model="form.zone_id"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<option value="">— выбрать —</option>
|
||||
<option v-for="zone in zones" :key="zone.id" :value="zone.id">{{ zone.name }}</option>
|
||||
</select>
|
||||
<div v-if="form.errors.zone_id" class="mt-2 text-sm text-red-600">{{ form.errors.zone_id }}</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<label for="condition_source_device_id" class="block font-medium text-sm text-gray-700">Устройство-источник показания</label>
|
||||
<select
|
||||
id="condition_source_device_id"
|
||||
v-model="form.condition_source_device_id"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<option value="">— выбрать —</option>
|
||||
<option v-for="device in devices" :key="device.id" :value="device.id">
|
||||
{{ device.zone.name }} — {{ device.name }} ({{ device.device_type.code }})
|
||||
</option>
|
||||
</select>
|
||||
<div v-if="form.errors.condition_source_device_id" class="mt-2 text-sm text-red-600">{{ form.errors.condition_source_device_id }}</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4 grid grid-cols-3 gap-4">
|
||||
<div>
|
||||
<label for="condition_sensor_type" class="block font-medium text-sm text-gray-700">Тип показания</label>
|
||||
<input
|
||||
id="condition_sensor_type"
|
||||
v-model="form.condition_sensor_type"
|
||||
list="sensor-types"
|
||||
type="text"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<datalist id="sensor-types">
|
||||
<option v-for="option in sensorTypeOptions" :key="option" :value="option" />
|
||||
</datalist>
|
||||
<div v-if="form.errors.condition_sensor_type" class="mt-2 text-sm text-red-600">{{ form.errors.condition_sensor_type }}</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="condition_operator" class="block font-medium text-sm text-gray-700">Оператор</label>
|
||||
<select
|
||||
id="condition_operator"
|
||||
v-model="form.condition_operator"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<option value="">— выбрать —</option>
|
||||
<option v-for="op in conditionOperatorOptions" :key="op" :value="op">{{ op }}</option>
|
||||
</select>
|
||||
<div v-if="form.errors.condition_operator" class="mt-2 text-sm text-red-600">{{ form.errors.condition_operator }}</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="condition_value" class="block font-medium text-sm text-gray-700">Порог</label>
|
||||
<input
|
||||
id="condition_value"
|
||||
v-model="form.condition_value"
|
||||
type="number"
|
||||
step="any"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<div v-if="form.errors.condition_value" class="mt-2 text-sm text-red-600">{{ form.errors.condition_value }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<label for="target_device_id" class="block font-medium text-sm text-gray-700">Устройство-исполнитель</label>
|
||||
<select
|
||||
id="target_device_id"
|
||||
v-model="form.target_device_id"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<option value="">— выбрать —</option>
|
||||
<option v-for="device in devices" :key="device.id" :value="device.id">
|
||||
{{ device.zone.name }} — {{ device.name }} ({{ device.device_type.code }})
|
||||
</option>
|
||||
</select>
|
||||
<div v-if="form.errors.target_device_id" class="mt-2 text-sm text-red-600">{{ form.errors.target_device_id }}</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4 grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label for="action_type" class="block font-medium text-sm text-gray-700">Действие</label>
|
||||
<select
|
||||
id="action_type"
|
||||
v-model="form.action_type"
|
||||
required
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<option value="">— выбрать —</option>
|
||||
<option v-for="option in actionTypeOptions" :key="option" :value="option">{{ option }}</option>
|
||||
</select>
|
||||
<div v-if="form.errors.action_type" class="mt-2 text-sm text-red-600">{{ form.errors.action_type }}</div>
|
||||
</div>
|
||||
|
||||
<div v-show="form.action_type === 'set_level'">
|
||||
<label for="level" class="block font-medium text-sm text-gray-700">Уровень</label>
|
||||
<input
|
||||
id="level"
|
||||
v-model="form.level"
|
||||
type="number"
|
||||
step="any"
|
||||
class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"
|
||||
>
|
||||
<div v-if="form.errors.level" class="mt-2 text-sm text-red-600">{{ form.errors.level }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<label class="inline-flex items-center">
|
||||
<input v-model="form.is_active" type="checkbox" class="rounded border-gray-300 text-indigo-600 shadow-sm">
|
||||
<span class="ms-2 text-sm text-gray-600">Правило активно</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="mt-4 flex items-center gap-4">
|
||||
<button
|
||||
type="submit"
|
||||
:disabled="form.processing"
|
||||
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 disabled:opacity-50"
|
||||
>
|
||||
Сохранить
|
||||
</button>
|
||||
<a :href="routes.automationRules.index" class="text-sm text-gray-600 hover:underline">Отмена</a>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,78 @@
|
||||
<script setup>
|
||||
import { computed } from 'vue';
|
||||
import { Link, router, usePage } from '@inertiajs/vue3';
|
||||
import AppLayout from '@/Layouts/AppLayout.vue';
|
||||
import { routes } from '@/routes';
|
||||
|
||||
defineProps({
|
||||
rules: { type: Array, required: true },
|
||||
});
|
||||
|
||||
const isOwner = computed(() => usePage().props.auth.user?.role === 'owner');
|
||||
|
||||
function destroy(rule) {
|
||||
if (confirm('Удалить правило?')) {
|
||||
router.delete(routes.automationRules.destroy(rule.id));
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AppLayout>
|
||||
<template #header>
|
||||
<div class="flex justify-between items-center">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">Правила автоматизации</h2>
|
||||
<Link
|
||||
v-if="isOwner"
|
||||
:href="routes.automationRules.create"
|
||||
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700"
|
||||
>
|
||||
Добавить правило
|
||||
</Link>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||
<table class="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr class="border-b">
|
||||
<th class="py-2">Зона</th>
|
||||
<th class="py-2">Условие</th>
|
||||
<th class="py-2">Действие</th>
|
||||
<th class="py-2">Активно</th>
|
||||
<th class="py-2" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-if="rules.length === 0">
|
||||
<td colspan="5" class="py-4 text-gray-500">Правил пока нет.</td>
|
||||
</tr>
|
||||
<tr v-for="rule in rules" :key="rule.id" class="border-b">
|
||||
<td class="py-2">{{ rule.zone.name }}</td>
|
||||
<td class="py-2">
|
||||
{{ rule.condition_source_device.name }}:
|
||||
{{ rule.condition_sensor_type }}
|
||||
{{ rule.condition_operator }}
|
||||
{{ rule.condition_value }}
|
||||
</td>
|
||||
<td class="py-2">
|
||||
{{ rule.target_device.name }}: {{ rule.action_type }}
|
||||
<template v-if="Object.keys(rule.action_params ?? {}).length">
|
||||
({{ JSON.stringify(rule.action_params) }})
|
||||
</template>
|
||||
</td>
|
||||
<td class="py-2">{{ rule.is_active ? 'да' : 'нет' }}</td>
|
||||
<td class="py-2 text-right space-x-2">
|
||||
<Link v-if="isOwner" :href="routes.automationRules.edit(rule.id)" class="text-indigo-600 hover:underline">Изменить</Link>
|
||||
<button v-if="isOwner" type="button" class="text-red-600 hover:underline" @click="destroy(rule)">Удалить</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</AppLayout>
|
||||
</template>
|
||||
Reference in New Issue
Block a user