refactor application source view

This commit is contained in:
Andras Bacsai
2024-11-04 00:14:44 +01:00
parent ca7290cebe
commit 83b3f6fe49
2 changed files with 70 additions and 35 deletions

View File

@@ -4,55 +4,92 @@ namespace App\Livewire\Project\Application;
use App\Models\Application;
use App\Models\PrivateKey;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Rule;
use Livewire\Component;
class Source extends Component
{
public $applicationId;
public Application $application;
public $private_keys;
#[Locked]
public $privateKeys;
protected $rules = [
'application.git_repository' => 'required',
'application.git_branch' => 'required',
'application.git_commit_sha' => 'nullable',
];
#[Rule(['nullable', 'string'])]
public ?string $privateKeyName = null;
protected $validationAttributes = [
'application.git_repository' => 'repository',
'application.git_branch' => 'branch',
'application.git_commit_sha' => 'commit sha',
];
#[Rule(['nullable', 'integer'])]
public ?int $privateKeyId = null;
#[Rule(['required', 'string'])]
public string $gitRepository;
#[Rule(['required', 'string'])]
public string $gitBranch;
#[Rule(['nullable', 'string'])]
public ?string $gitCommitSha = null;
public function mount()
{
$this->get_private_keys();
try {
$this->syncData();
$this->getPrivateKeys();
} catch (\Throwable $e) {
handleError($e, $this);
}
}
private function get_private_keys()
public function syncData(bool $toModel = false)
{
$this->private_keys = PrivateKey::whereTeamId(currentTeam()->id)->get()->reject(function ($key) {
return $key->id == $this->application->private_key_id;
if ($toModel) {
$this->validate();
$this->application->update([
'git_repository' => $this->gitRepository,
'git_branch' => $this->gitBranch,
'git_commit_sha' => $this->gitCommitSha,
'private_key_id' => $this->privateKeyId,
]);
} else {
$this->gitRepository = $this->application->git_repository;
$this->gitBranch = $this->application->git_branch;
$this->gitCommitSha = $this->application->git_commit_sha;
$this->privateKeyId = $this->application->private_key_id;
$this->privateKeyName = data_get($this->application, 'private_key.name');
}
}
private function getPrivateKeys()
{
$this->privateKeys = PrivateKey::whereTeamId(currentTeam()->id)->get()->reject(function ($key) {
return $key->id == $this->privateKeyId;
});
}
public function setPrivateKey(int $private_key_id)
public function setPrivateKey(int $privateKeyId)
{
$this->application->private_key_id = $private_key_id;
$this->application->save();
$this->application->refresh();
$this->get_private_keys();
try {
$this->privateKeyId = $privateKeyId;
$this->syncData(true);
$this->getPrivateKeys();
$this->application->refresh();
$this->privateKeyName = $this->application->private_key->name;
$this->dispatch('success', 'Private key updated!');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function submit()
{
$this->validate();
if (! $this->application->git_commit_sha) {
$this->application->git_commit_sha = 'HEAD';
try {
if (str($this->gitCommitSha)->isEmpty()) {
$this->gitCommitSha = 'HEAD';
}
$this->syncData(true);
$this->dispatch('success', 'Application source updated!');
} catch (\Throwable $e) {
return handleError($e, $this);
}
$this->application->save();
$this->dispatch('success', 'Application source updated!');
}
}

View File

@@ -27,24 +27,22 @@
<div class="flex flex-col gap-2">
<div class="flex gap-2">
<x-forms.input placeholder="coollabsio/coolify-example" id="application.git_repository"
label="Repository" />
<x-forms.input placeholder="main" id="application.git_branch" label="Branch" />
<x-forms.input placeholder="coollabsio/coolify-example" id="gitRepository" label="Repository" />
<x-forms.input placeholder="main" id="gitBranch" label="Branch" />
</div>
<div class="flex items-end gap-2">
<x-forms.input placeholder="HEAD" id="application.git_commit_sha" placeholder="HEAD"
label="Commit SHA" />
<x-forms.input placeholder="HEAD" id="gitCommitSha" placeholder="HEAD" label="Commit SHA" />
</div>
</div>
@if (data_get($application, 'private_key_id'))
@if ($privateKeyId)
<h3 class="pt-4">Deploy Key</h3>
<div class="py-2 pt-4">Currently attached Private Key: <span
class="dark:text-warning">{{ data_get($application, 'private_key.name') }}</span>
class="dark:text-warning">{{ $privateKeyName }}</span>
</div>
<h4 class="py-2 ">Select another Private Key</h4>
<div class="flex flex-wrap gap-2">
@foreach ($private_keys as $key)
@foreach ($privateKeys as $key)
<x-forms.button wire:click.defer="setPrivateKey('{{ $key->id }}')">{{ $key->name }}
</x-forms.button>
@endforeach