Skip to content

Commit

Permalink
order column added to track order of env in the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
peaklabs-dev committed Aug 14, 2024
1 parent 77044d5 commit 4d12447
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
36 changes: 33 additions & 3 deletions app/Livewire/Project/Shared/EnvironmentVariable/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ 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') {
return $variables->sortBy(function ($item) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
} else {
return $variables->sortBy('id')->values();
return $variables->sortBy('order')->values();
}
};

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -231,4 +261,4 @@ public function refreshEnvs()
$this->sortEnvironmentVariables();
$this->getDevView();
}
}
}
Original file line number Diff line number Diff line change
@@ -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');
});
}
};

0 comments on commit 4d12447

Please sign in to comment.