diff --git a/Http/Livewire/Branch/BranchModal.php b/Http/Livewire/Branch/BranchModal.php new file mode 100644 index 0000000..c627908 --- /dev/null +++ b/Http/Livewire/Branch/BranchModal.php @@ -0,0 +1,87 @@ + '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(); + } +}