Skip to content

Commit

Permalink
chore: apply flex layout and add page filter component
Browse files Browse the repository at this point in the history
- Applied flex layout and added the page filter component to the employee, division, and level pages for better UI/UX.
  • Loading branch information
Mahatma Mahardhika committed Aug 6, 2024
1 parent be1796d commit a271606
Show file tree
Hide file tree
Showing 23 changed files with 574 additions and 9 deletions.
8 changes: 8 additions & 0 deletions app/Http/Controllers/SubDivisionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Http\Controllers;

class SubDivisionController extends Controller
{
//
}
9 changes: 7 additions & 2 deletions app/Livewire/FormBranchOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Livewire;

use App\Models\Branch;
use App\Models\Company;
use Illuminate\View\View;
use Livewire\Attributes\On;
Expand Down Expand Up @@ -38,8 +37,14 @@ public function mount(): void
}
}

/**
* Retrieves the branch information for a given company code.
*
* @param string $companyCode The code of the company.
* @return void
*/
#[On('getBranch')]
public function getBranch($companyCode): void
public function getBranch(string $companyCode): void
{
$this->reset([
'branches',
Expand Down
168 changes: 168 additions & 0 deletions app/Livewire/SubDivisionForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace App\Livewire;

use App\Models\Department;
use App\Models\SubDivision;
use DB;
use Illuminate\Contracts\View\View;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;

class SubDivisionForm extends Component
{
public $departmentId;

#[Validate('required|unique:sub_divisions|min:3')]
public $code;

#[Validate('required|min:3')]
public $name;

public $subDivision;

public $actionForm = 'save';

/**
* Updates the specified property with the given value and performs validation if the property is 'code',
* 'email', or 'phone'.
*
* @param string $key The name of the property to be updated.
* @param mixed $value The new value for the property.
* @return void
* @throws ValidationException
*/
public function updated(string $key, mixed $value): void
{
$this->resetErrorBag();

if($key == 'code' || $key == 'name'){
$this->validateOnly($key);
}
}

/**
* Sets the department ID based on the provided department code.
*
* @param string $departmentCode The code of the department.
* @return void
*/
#[On('setDepartment')]
public function setDepartment(string $departmentCode): void
{
$department = Department::where('code', $departmentCode)->first();

if(!$department){
$this->dispatch('setErrorDepartment');
return;
}

$this->departmentId = $department->id;
}

/**
* The default data for the form.
*
* @return array
*/
public function subDivisionData(): array
{
return [
'company_id' => auth()->user()->details->company_id,
'branch_id' => auth()->user()->details->branch_id,
'department_id' => $this->departmentId,
'code' => $this->code,
'name' => $this->name,
];
}

/**
* Saves the subDivision details to the database and dispatches a 'sub-division-created' event.
*
* @return void
*/
public function save(): void
{
if(!$this->departmentId){
$this->dispatch('setErrorDepartment');
return;
}

$this->validate();

DB::transaction(function () {
$this->subDivision = SubDivision::create($this->subDivisionData());
}, 5);

$this->dispatch('sub-division-created', subDivisionId: $this->subDivision->id);

$this->reset();
}

/**
* Edit the subDivision details.
*/
#[On('edit')]
public function edit($code): void
{
$this->subDivision = SubDivision::where('code',$code)->first();
$this->departmentId = $this->subDivision->department_id;

$this->dispatch('selectDepartment', departmentId: $this->departmentId );

$this->code = $this->subDivision->code;
$this->name = $this->subDivision->name;

$this->actionForm = 'update';

$this->dispatch('show-form');
}

/**
* Updates the subDivision details in the database.
*/
public function update(): void
{
if(!$this->departmentId){
$this->dispatch('setErrorDepartment');
return;
}

DB::transaction(function () {
$this->subDivision->update($this->subDivisionData());
}, 5);

$this->dispatch('sub-division-updated', subDivisionId: $this->subDivision->id);

$this->reset();
}

/**
* Deletes the subDivision from the database.
*/
#[On('delete')]
public function destroy($code): void
{
$this->subDivision = SubDivision::where('code',$code)->first();
$this->subDivision->code = $code . '-deleted';
$this->subDivision->save();

$this->subDivision->delete();

$this->dispatch('sub-division-deleted', refreshCompanies: true);
}

/**
* Render the livewire component.
*
* @return View
*/
public function render(): View
{
return view('livewire.sub-division-form', [
'departments' => Department::all(),
]);
}
}
22 changes: 22 additions & 0 deletions app/Livewire/SubDivisionPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Livewire;

use Illuminate\Contracts\View\View;
use Livewire\Component;

class SubDivisionPage extends Component
{
public $moduleLabel = 'Sub Division';

/**
* Renders the view for the unit page.
*
* @return View The rendered view.
*/
public function render(): View
{
return view('livewire.sub-division-page')
->layout('components.layouts.dashboard');
}
}
94 changes: 94 additions & 0 deletions app/Livewire/SubDivisionTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Livewire;

use App\Models\SubDivision;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

class SubDivisionTable extends Component
{
use withPagination;
public $showForm = false;

#[Url]
public $search;

/**
* Handles the event when a subDivision is created.
*
* @param int $subDivisionId The ID of the created subDivision.
* @return void
*/
#[On('sub-division-created')]
public function subDivisionAdded(int $subDivisionId): void
{
$this->showForm = false;
}

/**
* Handles the event when a subDivision is updated.
*
* @param int $subDivisionId The ID of the updated subDivision.
* @return void
*/
#[On('sub-division-updated')]
public function subDivisionUpdated(int $subDivisionId): void
{
$this->showForm = false;
}

/**
* Handles the event when a subDivision is deleted.
* @return void
*/
#[On('sub-division-deleted')]
public function subDivisionDeleted(): void
{
$this->showForm = false;
$this->resetPage();
$this->getSubDivisions();
}

/**
* Shows the form subDivision.
*
* @return void
*/
#[On('show-form')]
public function showForm(): void
{
$this->showForm = true;
}

/**
* Retrieves a paginated list of subDivisions based on a search query.
*
* @return LengthAwarePaginator The paginated list of subDivisions.
*/
public function getSubDivisions(): LengthAwarePaginator
{
return SubDivision::where(function ($query){
$query->where('name', 'like', '%' . $this->search . '%')
->orWhere('code', 'like', '%' . $this->search . '%');
})
->orderBy('id', 'asc')
->paginate(5);
}

/**
* Render the livewire component.
*
* @return View
*/
public function render(): View
{
return view('livewire.sub-division-table',[
'subDivisions' => self::getSubDivisions()
]);
}
}
3 changes: 2 additions & 1 deletion app/Livewire/TransactionBusinessTripMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App\Livewire;

use Illuminate\View\View;
use Livewire\Component;

class TransactionBusinessTripMenu extends Component
{
public function render()
public function render(): View
{
return view('livewire.transaction-business-trip-menu');
}
Expand Down
Loading

0 comments on commit a271606

Please sign in to comment.