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,52 +105,50 @@ class All extends Component
$this->sortMe();
}
public function saveVariables($isPreview)
public function submit()
{
try {
$isPreview = $this->showPreview;
$variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables);
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) {
if ($isPreview) {
$found = $this->resource->environment_variables_preview()->where('key', $key)->first();
} else {
$found = $this->resource->environment_variables()->where('key', $key)->first();
}
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) {
continue;
}
$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;
// }
// }
if (!$found->is_shown_once && !$found->is_multiline) {
$found->value = $value;
$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->value = $value;
$environment->is_build_time = false;
$environment->is_multiline = false;
$environment->is_preview = $isPreview ? true : false;
$environment->is_preview = $isPreview;
$this->setEnvironmentResourceId($environment);
$environment->save();
}
}
$this->refreshEnvs();
$this->dispatch('success', 'Environment variables saved successfully.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
private function setEnvironmentResourceId($environment)
{
switch ($this->resource->type()) {
case 'application':
$environment->application_id = $this->resource->id;
@@ -183,15 +181,6 @@ class All extends Component
$environment->service_id = $this->resource->id;
break;
}
$environment->save();
}
}
if ($isPreview) {
$this->dispatch('success', 'Preview environment variables updated.');
} else {
$this->dispatch('success', 'Environment variables updated.');
}
$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);
}
}
}