diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 9e6760293..dbd5c2b9b 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -105,93 +105,82 @@ class All extends Component $this->sortMe(); } - public function saveVariables($isPreview) + public function submit() { - if ($isPreview) { - $variables = parseEnvFormatToArray($this->variablesPreview); - $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); - } else { - $variables = parseEnvFormatToArray($this->variables); - $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); - } - foreach ($variables as $key => $variable) { + try { + $isPreview = $this->showPreview; + $variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables); + if ($isPreview) { - $found = $this->resource->environment_variables_preview()->where('key', $key)->first(); + $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); } else { - $found = $this->resource->environment_variables()->where('key', $key)->first(); + $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); } - if ($found) { - if ($found->is_shown_once || $found->is_multiline) { - continue; + + foreach ($variables as $key => $value) { + $found = $isPreview + ? $this->resource->environment_variables_preview()->where('key', $key)->first() + : $this->resource->environment_variables()->where('key', $key)->first(); + + if ($found) { + if (!$found->is_shown_once && !$found->is_multiline) { + $found->value = $value; + $found->save(); + } + } else { + $environment = new EnvironmentVariable; + $environment->key = $key; + $environment->value = $value; + $environment->is_build_time = false; + $environment->is_multiline = false; + $environment->is_preview = $isPreview; + + $this->setEnvironmentResourceId($environment); + $environment->save(); } - $found->value = $variable; - // if (str($found->value)->startsWith('{{') && str($found->value)->endsWith('}}')) { - // $type = str($found->value)->after('{{')->before('.')->value; - // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { - // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); - - // return; - // } - // } - $found->save(); - - continue; - } else { - $environment = new EnvironmentVariable; - $environment->key = $key; - $environment->value = $variable; - // if (str($environment->value)->startsWith('{{') && str($environment->value)->endsWith('}}')) { - // $type = str($environment->value)->after('{{')->before('.')->value; - // if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) { - // $this->dispatch('error', 'Invalid shared variable type.', 'Valid types are: team, project, environment.'); - - // return; - // } - // } - $environment->is_build_time = false; - $environment->is_multiline = false; - $environment->is_preview = $isPreview ? true : false; - switch ($this->resource->type()) { - case 'application': - $environment->application_id = $this->resource->id; - break; - case 'standalone-postgresql': - $environment->standalone_postgresql_id = $this->resource->id; - break; - case 'standalone-redis': - $environment->standalone_redis_id = $this->resource->id; - break; - case 'standalone-mongodb': - $environment->standalone_mongodb_id = $this->resource->id; - break; - case 'standalone-mysql': - $environment->standalone_mysql_id = $this->resource->id; - break; - case 'standalone-mariadb': - $environment->standalone_mariadb_id = $this->resource->id; - break; - case 'standalone-keydb': - $environment->standalone_keydb_id = $this->resource->id; - break; - case 'standalone-dragonfly': - $environment->standalone_dragonfly_id = $this->resource->id; - break; - case 'standalone-clickhouse': - $environment->standalone_clickhouse_id = $this->resource->id; - break; - case 'service': - $environment->service_id = $this->resource->id; - break; - } - $environment->save(); } + + $this->refreshEnvs(); + $this->dispatch('success', 'Environment variables saved successfully.'); + } catch (\Throwable $e) { + return handleError($e, $this); } - if ($isPreview) { - $this->dispatch('success', 'Preview environment variables updated.'); - } else { - $this->dispatch('success', 'Environment variables updated.'); + } + + private function setEnvironmentResourceId($environment) + { + switch ($this->resource->type()) { + case 'application': + $environment->application_id = $this->resource->id; + break; + case 'standalone-postgresql': + $environment->standalone_postgresql_id = $this->resource->id; + break; + case 'standalone-redis': + $environment->standalone_redis_id = $this->resource->id; + break; + case 'standalone-mongodb': + $environment->standalone_mongodb_id = $this->resource->id; + break; + case 'standalone-mysql': + $environment->standalone_mysql_id = $this->resource->id; + break; + case 'standalone-mariadb': + $environment->standalone_mariadb_id = $this->resource->id; + break; + case 'standalone-keydb': + $environment->standalone_keydb_id = $this->resource->id; + break; + case 'standalone-dragonfly': + $environment->standalone_dragonfly_id = $this->resource->id; + break; + case 'standalone-clickhouse': + $environment->standalone_clickhouse_id = $this->resource->id; + break; + case 'service': + $environment->service_id = $this->resource->id; + break; } - $this->refreshEnvs(); } public function refreshEnvs() @@ -199,61 +188,4 @@ class All extends Component $this->resource->refresh(); $this->getDevView(); } - - public function submit($data) - { - try { - $found = $this->resource->environment_variables()->where('key', $data['key'])->first(); - if ($found) { - $this->dispatch('error', 'Environment variable already exists.'); - - return; - } - $environment = new EnvironmentVariable; - $environment->key = $data['key']; - $environment->value = $data['value']; - $environment->is_build_time = $data['is_build_time']; - $environment->is_multiline = $data['is_multiline']; - $environment->is_literal = $data['is_literal']; - $environment->is_preview = $data['is_preview']; - - switch ($this->resource->type()) { - case 'application': - $environment->application_id = $this->resource->id; - break; - case 'standalone-postgresql': - $environment->standalone_postgresql_id = $this->resource->id; - break; - case 'standalone-redis': - $environment->standalone_redis_id = $this->resource->id; - break; - case 'standalone-mongodb': - $environment->standalone_mongodb_id = $this->resource->id; - break; - case 'standalone-mysql': - $environment->standalone_mysql_id = $this->resource->id; - break; - case 'standalone-mariadb': - $environment->standalone_mariadb_id = $this->resource->id; - break; - case 'standalone-keydb': - $environment->standalone_keydb_id = $this->resource->id; - break; - case 'standalone-dragonfly': - $environment->standalone_dragonfly_id = $this->resource->id; - break; - case 'standalone-clickhouse': - $environment->standalone_clickhouse_id = $this->resource->id; - break; - case 'service': - $environment->service_id = $this->resource->id; - break; - } - $environment->save(); - $this->refreshEnvs(); - $this->dispatch('success', 'Environment variable added.'); - } catch (\Throwable $e) { - return handleError($e, $this); - } - } -} +} \ No newline at end of file