diff --git a/app/Livewire/Admin/EditUserModal.php b/app/Livewire/Admin/EditUserModal.php new file mode 100644 index 0000000..baf641e --- /dev/null +++ b/app/Livewire/Admin/EditUserModal.php @@ -0,0 +1,42 @@ +userId = $user->id; + $this->name = $user->name; + $this->email = $user->email; + } + + public function save() + { + $this->validate([ + 'name' => 'required', + 'email' => 'required|email', + ]); + + User::find($this->userId)->update([ + 'name' => $this->name, + 'email' => $this->email, + ]); + + $this->closeModal(); + $this->dispatch('notify', '使用者更新成功'); + } + + public function render() + { + return view('livewire.admin.edit-user-modal'); + } +} diff --git a/app/Livewire/Admin/UserTable.php b/app/Livewire/Admin/UserTable.php new file mode 100644 index 0000000..3ba91db --- /dev/null +++ b/app/Livewire/Admin/UserTable.php @@ -0,0 +1,148 @@ + 'outside']); + } + + /* */ + + + public function setUp(): array + { + $this->showCheckBox(); + + return [ + PowerGrid::exportable(fileName: 'my-export-file') + ->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV), + PowerGrid::header() + //->showSoftDeletes() + ->showToggleColumns() + ->showSearchInput(), + PowerGrid::footer()->showPerPage()->showRecordCount(), + ]; + } + public function header(): array + { + return [ + Button::add('bulk-delete') + ->slot('Bulk delete ()') + ->icon('solid-trash',['id' => 'my-custom-icon-id', 'class' => 'font-bold']) + ->class('inline-flex items-center gap-1 px-3 py-1 rounded ') + ->dispatch('bulkDelete.' . $this->tableName, []), + ]; + } + + public function datasource(): Builder + { + return User::query(); + } + + public function relationSearch(): array + { + return []; + } + + public function fields(): PowerGridFields + { + return PowerGrid::fields() + ->add('id') + ->add('name') + ->add('email') + ->add('created_at_formatted', fn (User $model) => Carbon::parse($model->created_at)->format('d/m/Y H:i:s')); + } + + public function columns(): array + { + return [ + Column::make('ID', 'id'), + Column::make('名稱', 'name')->sortable()->searchable(), + Column::make('Email', 'email')->sortable()->searchable(), + Column::make('建立時間', 'created_at_formatted', 'created_at')->sortable(), + Column::action('操作') + ]; + } + + public function filters(): array + { + return [ + Filter::inputText('name')->placeholder('Dish Name'), + Filter::inputText('email')->placeholder('Dish Email'), + Filter::datetimepicker('created_at'), + ]; + } + + public function actions(User $row): array + { + return [ + Button::add('edit') + ->slot('編輯') + ->icon('solid-pencil-square') + ->class('inline-flex items-center gap-1 px-3 py-1 rounded ') + ->openModal('admin.edit-user-modal', ['userId' => $row->id]), + Button::add('delete') + ->slot('刪除') + ->icon('solid-trash') + ->class('inline-flex items-center gap-1 px-3 py-1 rounded ') + ->confirmPrompt('確定要刪除這位使用者嗎?|DELETE', 'DELETE') // 使用 'DELETE' 作為標識符 + ->dispatch('delete-user', ['userId' => $row->id]), + ]; + } + #[On('bulkDelete.{tableName}')] + public function bulkDelete(): void + { + $this->js('alert(window.pgBulkActions.get(\'' . $this->tableName . '\'))'); + if($this->checkboxValues){ + User::destroy($this->checkboxValues); + $this->js('window.pgBulkActions.clearAll()'); // clear the count on the interface. + } + } + + #[\Livewire\Attributes\On('delete-user')] + public function deleteUser($userId): void + { + User::findOrFail($userId)->delete(); + + $this->dispatch('notify', '刪除成功'); + } + + /* public function actionRules($row): array + { + return [ + // Hide button edit for ID 1 + Rule::button('edit') + ->when(fn($row) => $row->id === 1) + ->hide(), + ]; + } */ + +} diff --git a/app/Livewire/Admin/Users.php b/app/Livewire/Admin/Users.php deleted file mode 100644 index 52fd57f..0000000 --- a/app/Livewire/Admin/Users.php +++ /dev/null @@ -1,104 +0,0 @@ - 'required|string|max:255', - ]; - - protected $paginationTheme = 'tailwind'; - - public function getUsersProperty() - { - return User::where('name', 'like', "%{$this->search}%") - ->orderBy($this->sortField, $this->sortDirection) - ->paginate(10); - } - - public function sortBy($field) - { - if ($this->sortField === $field) { - $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; - } else { - $this->sortField = $field; - $this->sortDirection = 'asc'; - } - } - public function mount() - { - $this->roles = Role::all(); - } - - public function openCreateModal() - { - $this->resetFields(); - $this->showCreateModal = true; - } - - public function openEditModal($id) - { - $user = User::findOrFail($id); - $this->editingUserId = $user->id; - $this->name = $user->name; - $this->selectedRoles = $user->roles()->pluck('id')->toArray(); - $this->showCreateModal = true; - } - - public function save() - { - $this->validate(); - - if ($this->editingRoleId) { - $role = User::findOrFail($this->editingRoleId); - $role->update(['name' => $this->name]); - $role->syncRolses($this->selectedRoles); - session()->flash('message', '使用者已更新'); - } else { - $role = User::create(['name' => $this->name]); - $role->syncRolses($this->selectedRoles); - session()->flash('message', '使用者已新增'); - } - - $this->resetFields(); - $this->showCreateModal = false; - } - - public function delete($id) - { - User::findOrFail($id)->delete(); - session()->flash('message', '使用者已刪除'); - } - - public function resetFields() - { - $this->name = ''; - $this->selectedRoles = []; - $this->editingUserId = null; - } - - public function render() - { - return view('livewire.admin.users', [ - 'users' => $this->users, - ])->layout('layouts.admin'); - } -} diff --git a/resources/views/livewire/admin/edit-user-modal.blade.php b/resources/views/livewire/admin/edit-user-modal.blade.php new file mode 100644 index 0000000..2efe871 --- /dev/null +++ b/resources/views/livewire/admin/edit-user-modal.blade.php @@ -0,0 +1,16 @@ +