cleanup all.php

This commit is contained in:
ayntk-ai
2024-08-12 14:46:30 +02:00
parent 122491808c
commit e28289ce1e

View File

@@ -18,7 +18,7 @@ class All extends Component
protected $listeners = [ protected $listeners = [
'saveKey' => 'submit', 'saveKey' => 'submit',
//'environmentVariableDeleted' => 'refreshEnvs', 'environmentVariableDeleted' => 'refreshEnvs',
]; ];
protected $rules = [ protected $rules = [
@@ -41,24 +41,24 @@ class All extends Component
{ {
$sortBy = 'key'; // Always sort by key $sortBy = 'key'; // Always sort by key
$this->resource->load(['environment_variables', 'environment_variables_preview']); $this->resource->load(['environment_variables', 'environment_variables_preview']);
$this->resource->environment_variables = $this->resource->environment_variables->sortBy(function ($item) use ($sortBy) { $this->resource->environment_variables = $this->sortEnvironmentVariables($this->resource->environment_variables, $sortBy);
return strtolower($item->key); $this->resource->environment_variables_preview = $this->sortEnvironmentVariables($this->resource->environment_variables_preview, $sortBy);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
$this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy(function ($item) use ($sortBy) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
$this->getDevView(); $this->getDevView();
} }
private function sortEnvironmentVariables($variables, $sortBy)
{
return $variables->sortBy(function ($item) use ($sortBy) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
}
public function instantSave() public function instantSave()
{ {
if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') {
$this->resource->settings->save(); $this->resource->settings->save();
$this->sortMe(); $this->sortMe();
$this->dispatch('success', 'Environment variable settings updated.'); $this->dispatch('success', 'Environment variable settings updated.');
} }
}
public function getDevView() public function getDevView()
{ {
@@ -91,6 +91,19 @@ class All extends Component
{ {
try { try {
if ($data === null) { if ($data === null) {
$this->handleBulkSubmit();
} else {
$this->handleSingleSubmit($data);
}
$this->sortMe();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
private function handleBulkSubmit()
{
$variables = parseEnvFormatToArray($this->variables); $variables = parseEnvFormatToArray($this->variables);
$this->deleteRemovedVariables(false, $variables); $this->deleteRemovedVariables(false, $variables);
$this->updateOrCreateVariables(false, $variables); $this->updateOrCreateVariables(false, $variables);
@@ -101,15 +114,23 @@ class All extends Component
$this->updateOrCreateVariables(true, $previewVariables); $this->updateOrCreateVariables(true, $previewVariables);
} }
$this->sortMe();
$this->dispatch('success', 'Environment variables updated.'); $this->dispatch('success', 'Environment variables updated.');
} else { }
private function handleSingleSubmit($data)
{
$found = $this->resource->environment_variables()->where('key', $data['key'])->first(); $found = $this->resource->environment_variables()->where('key', $data['key'])->first();
if ($found) { if ($found) {
$this->dispatch('error', 'Environment variable already exists.'); $this->dispatch('error', 'Environment variable already exists.');
return; return;
} }
$environment = $this->createEnvironmentVariable($data);
$environment->save();
}
private function createEnvironmentVariable($data)
{
$environment = new EnvironmentVariable; $environment = new EnvironmentVariable;
$environment->key = $data['key']; $environment->key = $data['key'];
$environment->value = $data['value']; $environment->value = $data['value'];
@@ -125,13 +146,7 @@ class All extends Component
$environment->$resourceIdField = $this->resource->id; $environment->$resourceIdField = $this->resource->id;
} }
$environment->save(); return $environment;
}
$this->sortMe();
} catch (\Throwable $e) {
return handleError($e, $this);
}
} }
private function getResourceIdField($resourceType) private function getResourceIdField($resourceType)