order column added to track order of env in the UI

This commit is contained in:
ayntk-ai
2024-08-14 20:35:08 +02:00
parent 77044d51c7
commit 4d12447715
2 changed files with 61 additions and 3 deletions

View File

@@ -48,7 +48,7 @@ class All extends Component
{ {
$this->resource->load(['environment_variables', 'environment_variables_preview']); $this->resource->load(['environment_variables', 'environment_variables_preview']);
$sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id'; $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'order';
$sortFunction = function ($variables) use ($sortBy) { $sortFunction = function ($variables) use ($sortBy) {
if ($sortBy === 'key') { if ($sortBy === 'key') {
@@ -56,7 +56,7 @@ class All extends Component
return strtolower($item->key); return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values(); }, SORT_NATURAL | SORT_FLAG_CASE)->values();
} else { } else {
return $variables->sortBy('id')->values(); return $variables->sortBy('order')->values();
} }
}; };
@@ -102,12 +102,40 @@ class All extends Component
$this->handleSingleSubmit($data); $this->handleSingleSubmit($data);
} }
$this->updateOrder();
$this->sortEnvironmentVariables(); $this->sortEnvironmentVariables();
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }
} }
private function updateOrder()
{
$variables = parseEnvFormatToArray($this->variables);
$order = 1;
foreach ($variables as $key => $value) {
$env = $this->resource->environment_variables()->where('key', $key)->first();
if ($env) {
$env->order = $order;
$env->save();
}
$order++;
}
if ($this->showPreview) {
$previewVariables = parseEnvFormatToArray($this->variablesPreview);
$order = 1;
foreach ($previewVariables as $key => $value) {
$env = $this->resource->environment_variables_preview()->where('key', $key)->first();
if ($env) {
$env->order = $order;
$env->save();
}
$order++;
}
}
}
private function handleBulkSubmit() private function handleBulkSubmit()
{ {
$variables = parseEnvFormatToArray($this->variables); $variables = parseEnvFormatToArray($this->variables);
@@ -131,7 +159,9 @@ class All extends Component
return; return;
} }
$maxOrder = $this->resource->environment_variables()->max('order') ?? 0;
$environment = $this->createEnvironmentVariable($data); $environment = $this->createEnvironmentVariable($data);
$environment->order = $maxOrder + 1;
$environment->save(); $environment->save();
} }
@@ -231,4 +261,4 @@ class All extends Component
$this->sortEnvironmentVariables(); $this->sortEnvironmentVariables();
$this->getDevView(); $this->getDevView();
} }
} }

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('environment_variables', function (Blueprint $table) {
$table->integer('order')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('environment_variables', function (Blueprint $table) {
$table->dropColumn('order');
});
}
};