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 $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));
});
}
}

View File

@ -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()

View File

@ -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()

View File

@ -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();
}

View File

@ -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';

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:label>Nama Pengguna <span class="text-red-500 ms-1">*</span></flux:label>
<flux:input placeholder="johndoe"
wire:model.live.debounce.500ms="form.username" autocomplete="off" />
<flux:input placeholder="johndoe" wire:model.live.debounce.500ms="form.username"
autocomplete="off" />
<flux:error name="form.username" />
</flux:field>
@ -63,7 +63,8 @@
<flux:field>
<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"
autocomplete="off" locale="id-ID" selectable-header />
placeholder="Pilih Tanggal" autocomplete="off" locale="id-ID"
selectable-header />
<flux:error name="form.birthdate" />
</flux:field>
@ -71,7 +72,8 @@
<flux:label>Tanggal Bergabung <span class="text-red-500 ms-1">*</span>
</flux:label>
<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:field>
@ -137,6 +139,24 @@
<flux:error name="form.outlet_ids" />
</flux:field>
</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>

View File

@ -97,25 +97,35 @@ class="w-4 h-4 text-gray-500 dark:text-white flex-shrink-0 mt-0.5" />
</div>
@endif
@if (count($employee['outlets']) > 0)
<div>
<flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">Outlet
</flux:heading>
<div class="flex flex-wrap gap-2">
@foreach ($employee['outlets'] as $outlet)
<span class="text-xs text-gray-400 border border-gray-400 rounded px-2 py-1">
{{ $outlet }}
</span>
@endforeach
</div>
<div>
<flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">Outlet
</flux:heading>
<div class="flex flex-wrap gap-2">
@foreach ($employee['outlets'] as $outlet)
<span class="text-xs text-gray-400 border border-gray-400 rounded px-2 py-1">
{{ $outlet }}
</span>
@endforeach
</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>
<flux:heading class="uppercase font-semibold text-gray-500 dark:text-gray-400 ">
Informasi
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>
<span class="font-medium text-gray-900 dark:text-white">Tgl Lahir:</span>