Skip to content

Commit

Permalink
feat(livewire): add FormBranchOption component
Browse files Browse the repository at this point in the history
- Added FormBranchOption component to handle branch selection in the user form
- Included blade file for FormBranchOption component in the user form view
- Implemented functionality to fetch and display branches based on selected company code
- Updated UserForm component to include branch ID in user detail data
- Updated FormCompanyOption component to dispatch 'getBranch' event when company code is set
- Updated FormDepartmentOption component to handle cases where company is not found

See the changes for more details.
  • Loading branch information
Mahatma Mahardhika committed Aug 6, 2024
1 parent 02bb103 commit be1796d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 14 deletions.
75 changes: 75 additions & 0 deletions app/Livewire/FormBranchOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Livewire;

use App\Models\Branch;
use App\Models\Company;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Attributes\Url;
use Livewire\Component;

class FormBranchOption extends Component
{

public $companyId;

#[Url(keep:true)]
public $companyCode = "all";

public $branchCode;

public $option = 'disabled';

public $branches;

public function mount(): void
{
$this->reset();

if($this->companyCode != "all") {
$company = Company::where('code', $this->companyCode)->first();

if($company) {
$this->option = "";
$this->companyId = $company->id;
$this->branches = $company->branches;
}
}
}

#[On('getBranch')]
public function getBranch($companyCode): void
{
$this->reset([
'branches',
'option'
]);

$company = Company::where('code', $companyCode)->first();

if(!$company) {
$this->option = "disabled";
return;
}

$this->option = "";
$this->companyId = $company->id;

if($company->branches()->count() > 0) {
$this->option = "";
$this->branches = $company->branches;
}
}


/**
* Render the view for the Livewire component.
*
* @return View
*/
public function render(): View
{
return view('livewire.form-branch-option');
}
}
1 change: 1 addition & 0 deletions app/Livewire/FormCompanyOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function updatedCompanyCode(mixed $companyCode): void
$this->companyCode = $companyCode;

$this->dispatch('setCompany', $companyCode);
$this->dispatch('getBranch', $companyCode);
$this->dispatch('getDepartment', $companyCode);
}

Expand Down
10 changes: 7 additions & 3 deletions app/Livewire/FormDepartmentOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ public function getDepartment(string $companyCode): void
]);

$company = Company::where('code', $companyCode)->first();
$departments = Department::where('company_id', $company->id);

if($departments->count() > 0){
if(!$company) {
$this->option = "disabled";
return;
}

if($company->departments()->count() > 0){
$this->option ="";
$this->departments = $departments->get();
$this->departments = $company->departments;
}
}

Expand Down
28 changes: 17 additions & 11 deletions app/Livewire/UserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public function updated(string $key, mixed $value): void
if($key == 'name'){
$this->validateOnly($key);
}

if($key == 'password'){
$this->newPassword = true;
}
}

/**
Expand Down Expand Up @@ -167,7 +163,21 @@ public function userData(): array
return [
'name' => $this->name,
'email' => $this->email,
'password' => $this->password,
];
}

/**
* Returns an array containing the company ID, branch ID, and user ID of the given user.
*
* @param User $user The user object to retrieve the details from.
* @return array An array with keys 'company_id', 'branch_id', and 'user_id', each containing the respective ID.
*/
public function userDetailData(User $user): array
{
return [
'company_id' => $this->companyId,
'branch_id' => $this->branchId,
'user_id' => $this->user->id
];
}

Expand All @@ -181,12 +191,8 @@ public function save(): void
$this->validate();

DB::transaction(function () {
$this->user = User::create($this->userData());
UserDetail::create([
'company_id' => auth()->user()->details->company_id,
'branch_id' => auth()->user()->details->branch_id,
'user_id' => $this->user->id
]);
$this->user = User::create(self::userData());
UserDetail::create(self::UserDetailData($this->user));
}, 5);

$this->dispatch('user-created', userId: $this->user->id);
Expand Down
24 changes: 24 additions & 0 deletions resources/views/livewire/form-branch-option.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="flex flex-col gap-3">
<label for="branchCode" class="block text-sm font-medium text-gray-700">Branch</label>
<select
wire:model.live="branchCode"
id="branchCode"
class="mt-1 shadow-sm focus:ring-indigo-500
focus:border-indigo-500 block w-full
sm:text-sm border border-gray-300
rounded-md p-2"
{{$option}}
>
<option value="all">Select Branch</option>
@if($branches)
@foreach ($branches as $branch)
<option value="{{ $branch->code }}">{{ $branch->name }}</option>
@endforeach
@endif
</select>
@error('branchCode')
<div class="p-4 mb-4 text-sm text-red-700 bg-red-100 rounded-lg dark:bg-red-200 dark:text-red-800" role="alert">
<span class="font-medium">Error!</span> {{ $message }}
</div>
@enderror
</div>
1 change: 1 addition & 0 deletions resources/views/livewire/user-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class="mt-1 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full
<div class="flex flex-col gap-4 flex-1">
<h2 class="mb-1 text-2xl">Organization</h2>
<livewire:form-company-option />
<livewire:form-branch-option />
<livewire:form-department-option />
<livewire:form-division-option />
<livewire:form-level-option />
Expand Down

0 comments on commit be1796d

Please sign in to comment.