From 99bb26924baa7132ebf5c50b57079538610cf161 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Wed, 12 Nov 2025 09:06:59 +0700 Subject: [PATCH] feat(user): menambahkan placeholder ke input tanggal - hide user yang punya role Developer - menambahkan input role_ids dan menyesuaikan logikanya - menambahkan kueri skop --- app/Livewire/Forms/Studio/Master/UserForm.php | 10 ++++++ app/Livewire/Studio/Master/User/Create.php | 8 ++++- app/Livewire/Studio/Master/User/Edit.php | 9 ++++- app/Livewire/Studio/Master/User/Index.php | 2 ++ app/Models/Employee.php | 12 +++++++ app/Traits/WithRoleSelector.php | 16 +++++++++ .../studio/master/user/form.blade.php | 28 ++++++++++++--- .../studio/master/user/index.blade.php | 36 ++++++++++++------- 8 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 app/Traits/WithRoleSelector.php diff --git a/app/Livewire/Forms/Studio/Master/UserForm.php b/app/Livewire/Forms/Studio/Master/UserForm.php index 669fbcb..b8a95cd 100644 --- a/app/Livewire/Forms/Studio/Master/UserForm.php +++ b/app/Livewire/Forms/Studio/Master/UserForm.php @@ -40,6 +40,8 @@ class UserForm extends Form public array $outlet_ids = []; + public array $role_ids = []; + public function rules(): array { return [ @@ -55,6 +57,8 @@ public function rules(): array 'status' => ['required', Rule::in(UserStatus::cases())], 'outlet_ids' => ['required', 'array', 'min:1'], 'outlet_ids.*' => Rule::exists('outlets', 'id'), + 'role_ids' => ['required', 'array', 'min:1'], + 'role_ids.*' => Rule::exists('roles', 'id'), ]; } @@ -70,6 +74,7 @@ public function validationAttributes(): array 'address' => 'alamat', 'gender' => 'jenis kelamin', 'outlet_ids' => 'outlet', + 'role_ids' => 'role', ]; } @@ -91,6 +96,7 @@ public function setUser(User $user) $this->gender = $employee->gender->value; $this->status = $user->status->value; $this->outlet_ids = $user->outlets->pluck('id')->toArray(); + $this->role_ids = $user->roles->pluck('id')->toArray(); } public function store() @@ -126,6 +132,8 @@ public function store() ]); $user->outlets()->attach($this->outlet_ids); + + $user->assignRole($this->role_ids); }); } @@ -159,6 +167,8 @@ public function update() ]); $this->user->outlets()->sync($this->outlet_ids); + + $this->user->syncRoles(array_map('intval', $this->role_ids)); }); } } diff --git a/app/Livewire/Studio/Master/User/Create.php b/app/Livewire/Studio/Master/User/Create.php index dbd3130..71e9b82 100644 --- a/app/Livewire/Studio/Master/User/Create.php +++ b/app/Livewire/Studio/Master/User/Create.php @@ -6,23 +6,29 @@ use App\Models\Outlet; use App\Traits\WithAuthorization; use App\Traits\WithOutletSelector; +use App\Traits\WithRoleSelector; use App\Traits\WithToast; use App\Traits\WithUpdatedData; use Livewire\Attributes\Title; use Livewire\Component; +use Spatie\Permission\Models\Role; #[Title('Tambah Pegawai')] class Create extends Component { - use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData; + use WithAuthorization, WithOutletSelector, WithRoleSelector, WithToast, WithUpdatedData; public UserForm $form; public array $outlets = []; + public array $roles = []; + public function mount() { $this->outlets = Outlet::pluck('name', 'id')->toArray(); + + $this->roles = Role::where('name', '!=', 'Developer')->orderBy('id')->pluck('name', 'id')->toArray(); } public function save() diff --git a/app/Livewire/Studio/Master/User/Edit.php b/app/Livewire/Studio/Master/User/Edit.php index 6e40c03..e463ab9 100644 --- a/app/Livewire/Studio/Master/User/Edit.php +++ b/app/Livewire/Studio/Master/User/Edit.php @@ -7,24 +7,31 @@ use App\Models\User; use App\Traits\WithAuthorization; use App\Traits\WithOutletSelector; +use App\Traits\WithRoleSelector; use App\Traits\WithToast; use App\Traits\WithUpdatedData; use Livewire\Attributes\Title; use Livewire\Component; +use Spatie\Permission\Models\Role; #[Title('Ubah Pegawai')] class Edit extends Component { - use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData; + use WithAuthorization, WithOutletSelector, WithRoleSelector, WithToast,WithUpdatedData; public UserForm $form; public array $outlets = []; + public array $roles = []; + public function mount(User $user) { $this->form->setUser($user); + $this->outlets = Outlet::pluck('name', 'id')->toArray(); + + $this->roles = Role::where('name', '!=', 'Developer')->orderBy('id')->pluck('name', 'id')->toArray(); } public function save() diff --git a/app/Livewire/Studio/Master/User/Index.php b/app/Livewire/Studio/Master/User/Index.php index 00ab40a..b5ee853 100644 --- a/app/Livewire/Studio/Master/User/Index.php +++ b/app/Livewire/Studio/Master/User/Index.php @@ -36,6 +36,7 @@ protected function loadEmployees() ->orWhereHas('user', fn ($q) => $q->where('username', 'like', '%'.$this->search.'%')); }) ->when(! empty($this->status), fn ($query) => $query->whereHas('user', fn ($q) => $q->whereIn('status', $this->status))) + ->withoutDeveloper() ->latest() ->get() ->map(fn ($employee) => [ @@ -65,6 +66,7 @@ protected function loadEmployees() ], 'referral_code' => $employee->user?->referralCode?->code, 'outlets' => $employee->user?->outlets?->pluck('name')->toArray() ?? [], + 'roles' => $employee->user?->roles?->pluck('name')->toArray() ?? [], ]) ->toArray(); } diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 0f8b4a0..c6d5b08 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -4,6 +4,8 @@ use App\Enums\EmploymentStatus; use App\Enums\Gender; +use Illuminate\Database\Eloquent\Attributes\Scope; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -24,6 +26,16 @@ protected function casts(): array ]; } + #[Scope] + protected function withoutDeveloper(Builder $query): void + { + $query->whereHas('user', function ($q) { + $q->whereDoesntHave('roles', function ($r) { + $r->where('name', 'Developer'); + }); + }); + } + public static function generateEmployeeCode(): string { $prefix = 'EMP'; diff --git a/app/Traits/WithRoleSelector.php b/app/Traits/WithRoleSelector.php new file mode 100644 index 0000000..154c0ec --- /dev/null +++ b/app/Traits/WithRoleSelector.php @@ -0,0 +1,16 @@ +form->role_ids = array_keys($this->roles); + } + + public function deselectAllRoles() + { + $this->form->role_ids = []; + } +} diff --git a/resources/views/livewire/studio/master/user/form.blade.php b/resources/views/livewire/studio/master/user/form.blade.php index 61f63e4..7cf168e 100644 --- a/resources/views/livewire/studio/master/user/form.blade.php +++ b/resources/views/livewire/studio/master/user/form.blade.php @@ -31,8 +31,8 @@ Nama Pengguna * - + @@ -63,7 +63,8 @@ Tanggal Lahir * + placeholder="Pilih Tanggal" autocomplete="off" locale="id-ID" + selectable-header /> @@ -71,7 +72,8 @@ Tanggal Bergabung * + placeholder="Pilih Tanggal" autocomplete="off" locale="id-ID" + selectable-header /> @@ -137,6 +139,24 @@ + + + + Peran * + + Pilih Semua + + Hapus Semua + + @foreach ($roles as $key => $value) + {{ $value }} + + @endforeach + + + + diff --git a/resources/views/livewire/studio/master/user/index.blade.php b/resources/views/livewire/studio/master/user/index.blade.php index 618a110..22f3bfb 100644 --- a/resources/views/livewire/studio/master/user/index.blade.php +++ b/resources/views/livewire/studio/master/user/index.blade.php @@ -97,25 +97,35 @@ class="w-4 h-4 text-gray-500 dark:text-white flex-shrink-0 mt-0.5" /> @endif - @if (count($employee['outlets']) > 0) -
- Outlet - -
- @foreach ($employee['outlets'] as $outlet) - - {{ $outlet }} - - @endforeach -
+
+ Outlet + +
+ @foreach ($employee['outlets'] as $outlet) + + {{ $outlet }} + + @endforeach
- @endif +
+ +
+ Peran + +
+ @foreach ($employee['roles'] as $role) + + {{ $role }} + + @endforeach +
+
Informasi Tanggal -
+
Tgl Lahir: