feat(user): menambahkan placeholder ke input tanggal

- hide user yang punya role Developer
- menambahkan input role_ids dan menyesuaikan logikanya
- menambahkan kueri skop
This commit is contained in:
Yoga Pangestu 2025-11-12 09:06:59 +07:00
parent db67c6af30
commit 99bb26924b
8 changed files with 102 additions and 19 deletions

View File

@ -40,6 +40,8 @@ class UserForm extends Form
public array $outlet_ids = []; public array $outlet_ids = [];
public array $role_ids = [];
public function rules(): array public function rules(): array
{ {
return [ return [
@ -55,6 +57,8 @@ public function rules(): array
'status' => ['required', Rule::in(UserStatus::cases())], 'status' => ['required', Rule::in(UserStatus::cases())],
'outlet_ids' => ['required', 'array', 'min:1'], 'outlet_ids' => ['required', 'array', 'min:1'],
'outlet_ids.*' => Rule::exists('outlets', 'id'), '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', 'address' => 'alamat',
'gender' => 'jenis kelamin', 'gender' => 'jenis kelamin',
'outlet_ids' => 'outlet', 'outlet_ids' => 'outlet',
'role_ids' => 'role',
]; ];
} }
@ -91,6 +96,7 @@ public function setUser(User $user)
$this->gender = $employee->gender->value; $this->gender = $employee->gender->value;
$this->status = $user->status->value; $this->status = $user->status->value;
$this->outlet_ids = $user->outlets->pluck('id')->toArray(); $this->outlet_ids = $user->outlets->pluck('id')->toArray();
$this->role_ids = $user->roles->pluck('id')->toArray();
} }
public function store() public function store()
@ -126,6 +132,8 @@ public function store()
]); ]);
$user->outlets()->attach($this->outlet_ids); $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->outlets()->sync($this->outlet_ids);
$this->user->syncRoles(array_map('intval', $this->role_ids));
}); });
} }
} }

View File

@ -6,23 +6,29 @@
use App\Models\Outlet; use App\Models\Outlet;
use App\Traits\WithAuthorization; use App\Traits\WithAuthorization;
use App\Traits\WithOutletSelector; use App\Traits\WithOutletSelector;
use App\Traits\WithRoleSelector;
use App\Traits\WithToast; use App\Traits\WithToast;
use App\Traits\WithUpdatedData; use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title; use Livewire\Attributes\Title;
use Livewire\Component; use Livewire\Component;
use Spatie\Permission\Models\Role;
#[Title('Tambah Pegawai')] #[Title('Tambah Pegawai')]
class Create extends Component class Create extends Component
{ {
use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData; use WithAuthorization, WithOutletSelector, WithRoleSelector, WithToast, WithUpdatedData;
public UserForm $form; public UserForm $form;
public array $outlets = []; public array $outlets = [];
public array $roles = [];
public function mount() public function mount()
{ {
$this->outlets = Outlet::pluck('name', 'id')->toArray(); $this->outlets = Outlet::pluck('name', 'id')->toArray();
$this->roles = Role::where('name', '!=', 'Developer')->orderBy('id')->pluck('name', 'id')->toArray();
} }
public function save() public function save()

View File

@ -7,24 +7,31 @@
use App\Models\User; use App\Models\User;
use App\Traits\WithAuthorization; use App\Traits\WithAuthorization;
use App\Traits\WithOutletSelector; use App\Traits\WithOutletSelector;
use App\Traits\WithRoleSelector;
use App\Traits\WithToast; use App\Traits\WithToast;
use App\Traits\WithUpdatedData; use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title; use Livewire\Attributes\Title;
use Livewire\Component; use Livewire\Component;
use Spatie\Permission\Models\Role;
#[Title('Ubah Pegawai')] #[Title('Ubah Pegawai')]
class Edit extends Component class Edit extends Component
{ {
use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData; use WithAuthorization, WithOutletSelector, WithRoleSelector, WithToast,WithUpdatedData;
public UserForm $form; public UserForm $form;
public array $outlets = []; public array $outlets = [];
public array $roles = [];
public function mount(User $user) public function mount(User $user)
{ {
$this->form->setUser($user); $this->form->setUser($user);
$this->outlets = Outlet::pluck('name', 'id')->toArray(); $this->outlets = Outlet::pluck('name', 'id')->toArray();
$this->roles = Role::where('name', '!=', 'Developer')->orderBy('id')->pluck('name', 'id')->toArray();
} }
public function save() public function save()

View File

@ -36,6 +36,7 @@ protected function loadEmployees()
->orWhereHas('user', fn ($q) => $q->where('username', 'like', '%'.$this->search.'%')); ->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))) ->when(! empty($this->status), fn ($query) => $query->whereHas('user', fn ($q) => $q->whereIn('status', $this->status)))
->withoutDeveloper()
->latest() ->latest()
->get() ->get()
->map(fn ($employee) => [ ->map(fn ($employee) => [
@ -65,6 +66,7 @@ protected function loadEmployees()
], ],
'referral_code' => $employee->user?->referralCode?->code, 'referral_code' => $employee->user?->referralCode?->code,
'outlets' => $employee->user?->outlets?->pluck('name')->toArray() ?? [], 'outlets' => $employee->user?->outlets?->pluck('name')->toArray() ?? [],
'roles' => $employee->user?->roles?->pluck('name')->toArray() ?? [],
]) ])
->toArray(); ->toArray();
} }

View File

@ -4,6 +4,8 @@
use App\Enums\EmploymentStatus; use App\Enums\EmploymentStatus;
use App\Enums\Gender; 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\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; 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 public static function generateEmployeeCode(): string
{ {
$prefix = 'EMP'; $prefix = 'EMP';

View File

@ -0,0 +1,16 @@
<?php
namespace App\Traits;
trait WithRoleSelector
{
public function selectAllRoles()
{
$this->form->role_ids = array_keys($this->roles);
}
public function deselectAllRoles()
{
$this->form->role_ids = [];
}
}

View File

@ -31,8 +31,8 @@
<flux:field> <flux:field>
<flux:label>Nama Pengguna <span class="text-red-500 ms-1">*</span></flux:label> <flux:label>Nama Pengguna <span class="text-red-500 ms-1">*</span></flux:label>
<flux:input placeholder="johndoe" <flux:input placeholder="johndoe" wire:model.live.debounce.500ms="form.username"
wire:model.live.debounce.500ms="form.username" autocomplete="off" /> autocomplete="off" />
<flux:error name="form.username" /> <flux:error name="form.username" />
</flux:field> </flux:field>
@ -63,7 +63,8 @@
<flux:field> <flux:field>
<flux:label>Tanggal Lahir <span class="text-red-500 ms-1">*</span></flux:label> <flux:label>Tanggal Lahir <span class="text-red-500 ms-1">*</span></flux:label>
<flux:date-picker with-today wire:model.live.debounce.500ms="form.birthdate" <flux:date-picker with-today wire:model.live.debounce.500ms="form.birthdate"
autocomplete="off" locale="id-ID" selectable-header /> placeholder="Pilih Tanggal" autocomplete="off" locale="id-ID"
selectable-header />
<flux:error name="form.birthdate" /> <flux:error name="form.birthdate" />
</flux:field> </flux:field>
@ -71,7 +72,8 @@
<flux:label>Tanggal Bergabung <span class="text-red-500 ms-1">*</span> <flux:label>Tanggal Bergabung <span class="text-red-500 ms-1">*</span>
</flux:label> </flux:label>
<flux:date-picker with-today wire:model.live.debounce.500ms="form.hire_date" <flux:date-picker with-today wire:model.live.debounce.500ms="form.hire_date"
autocomplete="off" locale="id-ID" selectable-header /> placeholder="Pilih Tanggal" autocomplete="off" locale="id-ID"
selectable-header />
<flux:error name="form.hire_date" /> <flux:error name="form.hire_date" />
</flux:field> </flux:field>
@ -137,6 +139,24 @@
<flux:error name="form.outlet_ids" /> <flux:error name="form.outlet_ids" />
</flux:field> </flux:field>
</flux:card> </flux:card>
<flux:card class="space-y-6 p-6">
<flux:field>
<flux:label>Peran <span class="text-red-500 ms-1">*</span></flux:label>
<flux:select variant="listbox" multiple searchable placeholder="Pilih Peran"
wire:model.live.debounce.500ms="form.role_ids">
<flux:select.option wire:click="selectAllRoles" wire:ignore>Pilih Semua
</flux:select.option>
<flux:select.option wire:click="deselectAllRoles" wire:ignore>Hapus Semua
</flux:select.option>
@foreach ($roles as $key => $value)
<flux:select.option value="{{ $key }}">{{ $value }}
</flux:select.option>
@endforeach
</flux:select>
<flux:error name="form.role_ids" />
</flux:field>
</flux:card>
</div> </div>
</div> </div>

View File

@ -97,25 +97,35 @@ class="w-4 h-4 text-gray-500 dark:text-white flex-shrink-0 mt-0.5" />
</div> </div>
@endif @endif
@if (count($employee['outlets']) > 0) <div>
<div> <flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">Outlet
<flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">Outlet </flux:heading>
</flux:heading> <div class="flex flex-wrap gap-2">
<div class="flex flex-wrap gap-2"> @foreach ($employee['outlets'] as $outlet)
@foreach ($employee['outlets'] as $outlet) <span class="text-xs text-gray-400 border border-gray-400 rounded px-2 py-1">
<span class="text-xs text-gray-400 border border-gray-400 rounded px-2 py-1"> {{ $outlet }}
{{ $outlet }} </span>
</span> @endforeach
@endforeach
</div>
</div> </div>
@endif </div>
<div>
<flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">Peran
</flux:heading>
<div class="flex flex-wrap gap-2">
@foreach ($employee['roles'] as $role)
<span class="text-xs text-gray-400 border border-gray-400 rounded px-2 py-1">
{{ $role }}
</span>
@endforeach
</div>
</div>
<div> <div>
<flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 "> <flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">
Informasi Informasi
Tanggal</flux:heading> Tanggal</flux:heading>
<div class="bg-gray-50 dark:bg-gray-800 rounded-lg p-3"> <div class="bg-gray-200 dark:bg-gray-800 rounded-lg p-3">
<div class="flex flex-col gap-2 text-xs"> <div class="flex flex-col gap-2 text-xs">
<div> <div>
<span class="font-medium text-gray-900 dark:text-white">Tgl Lahir:</span> <span class="font-medium text-gray-900 dark:text-white">Tgl Lahir:</span>