# Livewire Integration

**Date:** 2026-05-28  
**Developer:** Jeetendra Maharjan (mail.nepa@gmail.com)  
**Status:** Completed

---

## Summary

Livewire v4 is installed for incremental adoption on the `/systemadmin` backend. The first pilot replaces the tenant users modal (fetch + inline JS) with a class-based Livewire component backed by `TenantService`.

---

## Changes Made

### 1 — Package and config
- **File(s) affected:** `composer.json`, `composer.lock`, `config/livewire.php`, `public/vendor/livewire/`
- **What was done:** Added `livewire/livewire` ^4.3, published config, set `inject_assets` to `false`, added `livewire:assets` to `post-update-cmd`.
- **Why:** Explicit `@livewireStyles` / `@livewireScripts` in the admin layout alongside Vite.

### 2 — Admin layout and frontend hooks
- **File(s) affected:** `resources/views/components/backend-layout.blade.php`, `resources/js/app.js`
- **What was done:** Livewire style/script directives; removed duplicate Alpine from Vite (Livewire bundle provides Alpine); `livewire:init` + `morph.updated` re-inits Select2 and date pickers.
- **Why:** Avoid double Alpine; keep jQuery plugins working after Livewire DOM morphs.

### 3 — Tenant users modal pilot
- **File(s) affected:** `app/Services/Tenants/TenantService.php`, `app/Livewire/Backend/Tenants/TenantUsersModal.php`, `resources/views/livewire/backend/tenants/tenant-users-modal.blade.php`, `resources/views/backend/tenants/index.blade.php`, `routes/web.php`, `app/Http/Controllers/Backend/Tenants/TenantController.php`
- **What was done:** Service for list/reset password; Livewire modal; removed AJAX routes and `users-modal-content` partial.
- **Why:** Pilot incremental Livewire adoption per plan.

---

## Conventions for new Livewire modules

| Concern | Rule |
|--------|------|
| Location | Class: `app/Livewire/Backend/{Module}/`. View: `resources/views/livewire/backend/{module}/`. |
| Business logic | Only in `app/Services/{Module}/` — components validate, authorize, call service, render. |
| Tenant scope | Never accept `tenant_id` from the client; use `Auth::user()` context in service/component. |
| Public IDs | Use `uuid` in component state and actions, not internal `id`. |
| Permissions | `route.permission` middleware does not apply to Livewire requests — use `$this->authorize()` or gates in the component. |
| Opening from Blade | `$dispatch('event-name', { ... })` when the trigger is outside the component; `#[On('event-name')]` on the component. |
| Modals | Livewire: `<x-admin-modal-shell>` (uses shared `admin-modal-frame`). Blade-only: `<x-modal name="...">`. Same fade/scale animation on both. |
| Selects / dates | Use `.js-select2` / `.date-picker`; morph hook in `app.js` re-initializes after updates. |
| Confirmations | SweetAlert2 (`Swal.fire`), not `confirm()` / `alert()`. |

---

## Decisions & Notes

- **Livewire 4** installed (stable) instead of 3.x; API compatible with plan (class components, `#[On]`, morph hooks).
- **Alpine + Livewire** are bundled once in `resources/js/app.js` via `livewire.esm` and started with `Livewire.start()` (see [Livewire Alpine docs](https://livewire.laravel.com/docs/4.x/alpine#manually-bundling-alpine-in-your-javascript-build)). Layout uses `@livewireScriptConfig`, not `@livewireScripts`.
- Modals use `x-show="$wire.showModal"` via `admin-modal-frame` (not `wire:show` or `@entangle`). Close only toggles `showModal`; state resets on next `open()`.
- Auth/guest layouts are unchanged; only `x-backend-layout` includes Livewire assets.

---

### 4 — Users list reset password
- **File(s) affected:** `app/Services/Users/UserService.php`, `app/Livewire/Backend/Users/UserResetPasswordModal.php`, `resources/views/livewire/backend/users/user-reset-password-modal.blade.php`, `resources/views/backend/users/index.blade.php`, `app/Services/Tenants/TenantService.php`
- **What was done:** Added “Reset password” action on Users index with Livewire modal; shared `UserService::resetPassword()`.
- **Why:** Reset password existed only on Tenants → Users modal, not on the main Users list.

### 6 — Unified admin modals
- **File(s) affected:** `resources/views/components/admin-modal-frame.blade.php`, `admin-modal-shell.blade.php`, `modal.blade.php`, `.cursor/rules/systemadmin-listing-and-form-ux.mdc`
- **What was done:** Single shared frame (backdrop + panel transitions); `<x-modal>` and `<x-admin-modal-shell>` both use it.
- **Why:** Consistent open/close animation and one standard for future modals.

### 5 — User create role UUID fix
- **File(s) affected:** `app/Models/Role.php`, `app/Http/Controllers/Backend/Users/UserController.php`, `resources/views/backend/users/create.blade.php`, `resources/views/backend/users/edit.blade.php`, `database/migrations/2026_05_28_000001_backfill_missing_role_uuids.php`
- **What was done:** Backfill missing role UUIDs (Super Admin created before UUID column); tenant-scoped validation; Select2 on role dropdown.
- **Why:** Empty `option value` caused “The role uuid field is required” even when Super Admin was selected.

---

## Pending / Follow-up

- [ ] Convert roles permissions grid and listing search modules when needed.
- [ ] Add `Livewire::test()` feature tests for tenant users modal when test DB is available.
