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
No known key found for this signature in database
2 changed files with 61 additions and 3 deletions

View File

@ -48,7 +48,7 @@ public function sortEnvironmentVariables()
{
$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) {
if ($sortBy === 'key') {
@ -56,7 +56,7 @@ public function sortEnvironmentVariables()
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
} else {
return $variables->sortBy('id')->values();
return $variables->sortBy('order')->values();
}
};
@ -102,12 +102,40 @@ public function submit($data = null)
$this->handleSingleSubmit($data);
}
$this->updateOrder();
$this->sortEnvironmentVariables();
} catch (\Throwable $e) {
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()
{
$variables = parseEnvFormatToArray($this->variables);
@ -131,7 +159,9 @@ private function handleSingleSubmit($data)
return;
}
$maxOrder = $this->resource->environment_variables()->max('order') ?? 0;
$environment = $this->createEnvironmentVariable($data);
$environment->order = $maxOrder + 1;
$environment->save();
}

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');
});
}
};