new submit logic

This commit is contained in:
ayntk-ai
2024-08-12 13:23:36 +02:00
parent d13c321c3d
commit c5aba34a6f

View File

@@ -105,93 +105,82 @@ class All extends Component
$this->sortMe(); $this->sortMe();
} }
public function saveVariables($isPreview) public function submit()
{ {
if ($isPreview) { try {
$variables = parseEnvFormatToArray($this->variablesPreview); $isPreview = $this->showPreview;
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); $variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables);
} else {
$variables = parseEnvFormatToArray($this->variables);
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
}
foreach ($variables as $key => $variable) {
if ($isPreview) { if ($isPreview) {
$found = $this->resource->environment_variables_preview()->where('key', $key)->first(); $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
} else { } 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) { foreach ($variables as $key => $value) {
continue; $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 { private function setEnvironmentResourceId($environment)
$this->dispatch('success', 'Environment variables updated.'); {
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() public function refreshEnvs()
@@ -199,61 +188,4 @@ class All extends Component
$this->resource->refresh(); $this->resource->refresh();
$this->getDevView(); $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);
}
}
} }