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(); $this->sortMe();
} }
public function saveVariables($isPreview) public function submit()
{ {
try {
$isPreview = $this->showPreview;
$variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables);
if ($isPreview) { if ($isPreview) {
$variables = parseEnvFormatToArray($this->variablesPreview);
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete(); $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
} else { } else {
$variables = parseEnvFormatToArray($this->variables);
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete(); $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
} }
foreach ($variables as $key => $variable) {
if ($isPreview) { foreach ($variables as $key => $value) {
$found = $this->resource->environment_variables_preview()->where('key', $key)->first(); $found = $isPreview
} else { ? $this->resource->environment_variables_preview()->where('key', $key)->first()
$found = $this->resource->environment_variables()->where('key', $key)->first(); : $this->resource->environment_variables()->where('key', $key)->first();
}
if ($found) { if ($found) {
if ($found->is_shown_once || $found->is_multiline) { if (!$found->is_shown_once && !$found->is_multiline) {
continue; $found->value = $value;
}
$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(); $found->save();
}
continue;
} else { } else {
$environment = new EnvironmentVariable; $environment = new EnvironmentVariable;
$environment->key = $key; $environment->key = $key;
$environment->value = $variable; $environment->value = $value;
// 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_build_time = false;
$environment->is_multiline = 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()) { switch ($this->resource->type()) {
case 'application': case 'application':
$environment->application_id = $this->resource->id; $environment->application_id = $this->resource->id;
@@ -183,15 +181,6 @@ class All extends Component
$environment->service_id = $this->resource->id; $environment->service_id = $this->resource->id;
break; break;
} }
$environment->save();
}
}
if ($isPreview) {
$this->dispatch('success', 'Preview environment variables updated.');
} else {
$this->dispatch('success', 'Environment variables updated.');
}
$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);
}
}
} }