fix env deletion and sorting of locked envs

This commit is contained in:
ayntk-ai
2024-08-12 14:44:18 +02:00
parent 1d7c833b7c
commit 122491808c
2 changed files with 17 additions and 14 deletions

View File

@@ -17,8 +17,8 @@ class All extends Component
public string $view = 'normal'; public string $view = 'normal';
protected $listeners = [ protected $listeners = [
'refreshEnvs',
'saveKey' => 'submit', 'saveKey' => 'submit',
//'environmentVariableDeleted' => 'refreshEnvs',
]; ];
protected $rules = [ protected $rules = [
@@ -39,20 +39,24 @@ class All extends Component
public function sortMe() public function sortMe()
{ {
if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { $sortBy = 'key'; // Always sort by key
$sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; $this->resource->load(['environment_variables', 'environment_variables_preview']);
$this->resource->environment_variables = $this->resource->environment_variables->sortBy($sortBy, SORT_NATURAL|SORT_FLAG_CASE); $this->resource->environment_variables = $this->resource->environment_variables->sortBy(function ($item) use ($sortBy) {
$this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy($sortBy, SORT_NATURAL|SORT_FLAG_CASE); return strtolower($item->key);
} }, 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();
} }
public function instantSave() public function instantSave()
{ {
if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') {
$this->resource->settings->save(); $this->resource->settings->save();
$this->dispatch('success', 'Environment variable settings updated.');
$this->sortMe(); $this->sortMe();
$this->dispatch('success', 'Environment variable settings updated.');
} }
} }
@@ -87,7 +91,6 @@ class All extends Component
{ {
try { try {
if ($data === null) { if ($data === null) {
// Handle saving in developer view
$variables = parseEnvFormatToArray($this->variables); $variables = parseEnvFormatToArray($this->variables);
$this->deleteRemovedVariables(false, $variables); $this->deleteRemovedVariables(false, $variables);
$this->updateOrCreateVariables(false, $variables); $this->updateOrCreateVariables(false, $variables);
@@ -98,9 +101,9 @@ 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 { } else {
// Handle the case when adding a single variable
$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.');
@@ -117,7 +120,7 @@ class All extends Component
$resourceType = $this->resource->type(); $resourceType = $this->resource->type();
$resourceIdField = $this->getResourceIdField($resourceType); $resourceIdField = $this->getResourceIdField($resourceType);
if ($resourceIdField) { if ($resourceIdField) {
$environment->$resourceIdField = $this->resource->id; $environment->$resourceIdField = $this->resource->id;
} }
@@ -125,7 +128,6 @@ class All extends Component
$environment->save(); $environment->save();
} }
$this->refreshEnvs();
$this->sortMe(); $this->sortMe();
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
@@ -207,4 +209,4 @@ class All extends Component
$this->resource->refresh(); $this->resource->refresh();
$this->getDevView(); $this->getDevView();
} }
} }

View File

@@ -129,9 +129,10 @@ class Show extends Component
{ {
try { try {
$this->env->delete(); $this->env->delete();
$this->dispatch('refreshEnvs'); $this->dispatch('environmentVariableDeleted');
$this->dispatch('success', 'Environment variable deleted successfully.');
} catch (\Exception $e) { } catch (\Exception $e) {
return handleError($e); return handleError($e);
} }
} }
} }