diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php index b182652d7..cde5876fd 100644 --- a/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php +++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/Add.php @@ -15,6 +15,7 @@ class Add extends Component public string $value; public bool $is_build_time = false; + protected $listeners = ['clearAddEnv' => 'clear']; protected $rules = [ 'key' => 'required|string', 'value' => 'required|string', @@ -27,25 +28,16 @@ class Add extends Component public function submit() { $this->validate(); - try { - $application_id = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail()->id; - EnvironmentVariable::create([ - 'key' => $this->key, - 'value' => $this->value, - 'is_build_time' => $this->is_build_time, - 'application_id' => $application_id, - ]); - $this->emit('refreshEnvs'); - $this->key = ''; - $this->value = ''; - } catch (mixed $e) { - dd('asdf'); - if ($e instanceof QueryException) { - dd($e->errorInfo); - $this->emit('error', $e->errorInfo[2]); - } else { - $this->emit('error', $e); - } - } + $this->emitUp('submit', [ + 'key' => $this->key, + 'value' => $this->value, + 'is_build_time' => $this->is_build_time, + ]); + } + public function clear() + { + $this->key = ''; + $this->value = ''; + $this->is_build_time = false; } } diff --git a/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php b/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php index 05085b433..a3e845783 100644 --- a/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php +++ b/app/Http/Livewire/Project/Application/EnvironmentVariable/All.php @@ -3,14 +3,30 @@ namespace App\Http\Livewire\Project\Application\EnvironmentVariable; use App\Models\Application; +use App\Models\EnvironmentVariable; use Livewire\Component; class All extends Component { public Application $application; - protected $listeners = ['refreshEnvs']; + protected $listeners = ['refreshEnvs', 'submit']; public function refreshEnvs() { $this->application->refresh(); } + public function submit($data) + { + try { + EnvironmentVariable::create([ + 'key' => $data['key'], + 'value' => $data['value'], + 'is_build_time' => $data['is_build_time'], + 'application_id' => $this->application->id, + ]); + $this->application->refresh(); + $this->emit('clearAddEnv'); + } catch (\Exception $e) { + return generalErrorHandlerLivewire($e, $this); + } + } } diff --git a/app/Http/Livewire/Project/Application/Storages/Add.php b/app/Http/Livewire/Project/Application/Storages/Add.php index 4870638ef..8e2c9a384 100644 --- a/app/Http/Livewire/Project/Application/Storages/Add.php +++ b/app/Http/Livewire/Project/Application/Storages/Add.php @@ -14,6 +14,8 @@ class Add extends Component public string $name; public string $mount_path; public string|null $host_path = null; + + protected $listeners = ['clearAddStorage' => 'clear']; protected $rules = [ 'name' => 'required|string', 'mount_path' => 'required|string', @@ -26,27 +28,16 @@ class Add extends Component public function submit() { $this->validate(); - try { - $application_id = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail()->id; - LocalPersistentVolume::create([ - 'name' => $this->name, - 'mount_path' => $this->mount_path, - 'host_path' => $this->host_path, - 'resource_id' => $application_id, - 'resource_type' => Application::class, - ]); - $this->emit('refreshStorages'); - $this->name = ''; - $this->mount_path = ''; - $this->host_path = ''; - } catch (mixed $e) { - dd('asdf'); - if ($e instanceof QueryException) { - dd($e->errorInfo); - $this->emit('error', $e->errorInfo[2]); - } else { - $this->emit('error', $e); - } - } + $this->emitUp('submit', [ + 'name' => $this->name, + 'mount_path' => $this->mount_path, + 'host_path' => $this->host_path, + ]); + } + public function clear() + { + $this->name = ''; + $this->mount_path = ''; + $this->host_path = null; } } diff --git a/app/Http/Livewire/Project/Application/Storages/All.php b/app/Http/Livewire/Project/Application/Storages/All.php index 590350bdb..9c8e50b3a 100644 --- a/app/Http/Livewire/Project/Application/Storages/All.php +++ b/app/Http/Livewire/Project/Application/Storages/All.php @@ -3,14 +3,32 @@ namespace App\Http\Livewire\Project\Application\Storages; use App\Models\Application; +use App\Models\LocalPersistentVolume; +use Illuminate\Database\QueryException; use Livewire\Component; class All extends Component { public Application $application; - protected $listeners = ['refreshStorages']; + protected $listeners = ['refreshStorages', 'submit']; public function refreshStorages() { $this->application->refresh(); } + public function submit($data) + { + try { + LocalPersistentVolume::create([ + 'name' => $data['name'], + 'mount_path' => $data['mount_path'], + 'host_path' => $data['host_path'], + 'resource_id' => $this->application->id, + 'resource_type' => Application::class, + ]); + $this->application->refresh(); + $this->emit('clearAddStorage'); + } catch (\Exception $e) { + return generalErrorHandlerLivewire($e, $this); + } + } } diff --git a/bootstrap/helpers.php b/bootstrap/helpers.php index a625ea133..d913fb94f 100644 --- a/bootstrap/helpers.php +++ b/bootstrap/helpers.php @@ -4,6 +4,7 @@ use App\Actions\CoolifyTask\PrepareCoolifyTask; use App\Data\CoolifyTaskArgs; use App\Models\Server; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\QueryException; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; @@ -11,6 +12,20 @@ use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Storage; use Spatie\Activitylog\Contracts\Activity; +if (!function_exists('generalErrorHandlerLivewire')) { + function generalErrorHandlerLivewire(\Throwable $e, $that) + { + if ($e instanceof QueryException) { + if ($e->errorInfo[0] === '23505') { + $that->emit('error', 'Duplicate entry found.'); + } else { + $that->emit('error', $e->errorInfo[3]); + } + } else { + $that->emit('error', $e); + } + } +} if (!function_exists('remoteProcess')) { /** * Run a Remote Process, which SSH's asynchronously into a machine to run the command(s). diff --git a/resources/views/components/inputs/input.blade.php b/resources/views/components/inputs/input.blade.php index ec7e38e1c..429f943dc 100644 --- a/resources/views/components/inputs/input.blade.php +++ b/resources/views/components/inputs/input.blade.php @@ -13,7 +13,8 @@ 'flex flex-col' => $type !== 'checkbox', ])> @if (!$noLabel) -