*/ public array $newName = ['revenu' => '', 'depense' => '', 'epargne' => '']; public ?int $editingId = null; public string $editingName = ''; public function add(string $type): void { $this->validate([ "newName.{$type}" => ['required', 'string', 'max:255'], ]); if (! in_array($type, ['revenu', 'depense', 'epargne'], true)) { abort(422); } $household = auth()->user()->currentHousehold; $position = $household->categories()->where('type', $type)->max('position') + 1; $household->categories()->create([ 'name' => $this->newName[$type], 'type' => $type, 'position' => $position, ]); $this->newName[$type] = ''; } public function startEdit(int $categoryId): void { $category = Category::findOrFail($categoryId); $this->authorize('update', $category); $this->editingId = $category->id; $this->editingName = $category->name; } public function saveEdit(): void { $category = Category::findOrFail($this->editingId); $this->authorize('update', $category); $this->validate([ 'editingName' => ['required', 'string', 'max:255'], ]); $category->update(['name' => $this->editingName]); $this->editingId = null; } public function cancelEdit(): void { $this->editingId = null; } public function delete(int $categoryId): void { $category = Category::findOrFail($categoryId); $this->authorize('delete', $category); $category->delete(); } public function render() { $household = auth()->user()->currentHousehold; $categories = $household->categories()->orderBy('position')->get()->groupBy(fn (Category $category) => $category->type->value); return view('livewire.categories.index', [ 'types' => CategoryType::cases(), 'categories' => $categories, ]); } }