add liveware
This commit is contained in:
parent
997a68faec
commit
6a8ecc1937
87
Http/Livewire/Branch/BranchModal.php
Normal file
87
Http/Livewire/Branch/BranchModal.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Writeoff\Http\Livewire\Branch;
|
||||
|
||||
use Livewire\Component;
|
||||
use Modules\Writeoff\Entities\Branch;
|
||||
|
||||
class BranchModal extends Component
|
||||
{
|
||||
public $kode;
|
||||
public $name;
|
||||
|
||||
public Branch $branch;
|
||||
|
||||
protected $rules = [
|
||||
'kode' => 'required|string|unique:branches,kode',
|
||||
'name' => 'required|string',
|
||||
];
|
||||
|
||||
// This is the list of listeners that this component listens to.
|
||||
protected $listeners = [
|
||||
'modal.show.brahcn' => 'mountBranch',
|
||||
'delete_branch' => 'delete'
|
||||
];
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('branch::livewire.branch.branch-modal');
|
||||
}
|
||||
|
||||
public function mountBranch($branch_name = '', $branch_kode='')
|
||||
{
|
||||
if (empty($branch_kode)) {
|
||||
// Create new
|
||||
$this->branch = new Branch;
|
||||
$this->kode = '';
|
||||
if (empty($branch_name)) {
|
||||
$this->name = '';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the role by name.
|
||||
$permission = Branch::where('kode', $branch_kode)->first();
|
||||
if (is_null($permission)) {
|
||||
$this->emit('error', 'The selected kode [' . $branch_kode . '] is not found');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->branch = $permission;
|
||||
|
||||
// Set the name and checked permissions properties to the role's values.
|
||||
$this->kode = $this->branch->kode;
|
||||
$this->name = $this->branch->name;
|
||||
}
|
||||
|
||||
public function submit()
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$this->branch->kode = strtoupper($this->kode);
|
||||
$this->branch->name = strtolower($this->name);
|
||||
if ($this->branch->isDirty()) {
|
||||
$this->branch->save();
|
||||
}
|
||||
|
||||
// Emit a success event with a message indicating that the permissions have been updated.
|
||||
$this->emit('success', 'Branch updated');
|
||||
}
|
||||
|
||||
public function delete($kode)
|
||||
{
|
||||
$branch = Branch::where('kode', $kode)->first();
|
||||
|
||||
if (!is_null($branch)) {
|
||||
$branch->delete();
|
||||
}
|
||||
|
||||
$this->emit('success', 'Branch deleted');
|
||||
}
|
||||
|
||||
public function hydrate()
|
||||
{
|
||||
$this->resetErrorBag();
|
||||
$this->resetValidation();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user