refactor application source view
This commit is contained in:
@@ -4,55 +4,92 @@ namespace App\Livewire\Project\Application;
|
|||||||
|
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
use App\Models\PrivateKey;
|
use App\Models\PrivateKey;
|
||||||
|
use Livewire\Attributes\Locked;
|
||||||
|
use Livewire\Attributes\Rule;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
class Source extends Component
|
class Source extends Component
|
||||||
{
|
{
|
||||||
public $applicationId;
|
|
||||||
|
|
||||||
public Application $application;
|
public Application $application;
|
||||||
|
|
||||||
public $private_keys;
|
#[Locked]
|
||||||
|
public $privateKeys;
|
||||||
|
|
||||||
protected $rules = [
|
#[Rule(['nullable', 'string'])]
|
||||||
'application.git_repository' => 'required',
|
public ?string $privateKeyName = null;
|
||||||
'application.git_branch' => 'required',
|
|
||||||
'application.git_commit_sha' => 'nullable',
|
|
||||||
];
|
|
||||||
|
|
||||||
protected $validationAttributes = [
|
#[Rule(['nullable', 'integer'])]
|
||||||
'application.git_repository' => 'repository',
|
public ?int $privateKeyId = null;
|
||||||
'application.git_branch' => 'branch',
|
|
||||||
'application.git_commit_sha' => 'commit sha',
|
#[Rule(['required', 'string'])]
|
||||||
];
|
public string $gitRepository;
|
||||||
|
|
||||||
|
#[Rule(['required', 'string'])]
|
||||||
|
public string $gitBranch;
|
||||||
|
|
||||||
|
#[Rule(['nullable', 'string'])]
|
||||||
|
public ?string $gitCommitSha = null;
|
||||||
|
|
||||||
public function mount()
|
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) {
|
if ($toModel) {
|
||||||
return $key->id == $this->application->private_key_id;
|
$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;
|
try {
|
||||||
$this->application->save();
|
$this->privateKeyId = $privateKeyId;
|
||||||
$this->application->refresh();
|
$this->syncData(true);
|
||||||
$this->get_private_keys();
|
$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()
|
public function submit()
|
||||||
{
|
{
|
||||||
$this->validate();
|
try {
|
||||||
if (! $this->application->git_commit_sha) {
|
if (str($this->gitCommitSha)->isEmpty()) {
|
||||||
$this->application->git_commit_sha = 'HEAD';
|
$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!');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,24 +27,22 @@
|
|||||||
|
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
<x-forms.input placeholder="coollabsio/coolify-example" id="application.git_repository"
|
<x-forms.input placeholder="coollabsio/coolify-example" id="gitRepository" label="Repository" />
|
||||||
label="Repository" />
|
<x-forms.input placeholder="main" id="gitBranch" label="Branch" />
|
||||||
<x-forms.input placeholder="main" id="application.git_branch" label="Branch" />
|
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-end gap-2">
|
<div class="flex items-end gap-2">
|
||||||
<x-forms.input placeholder="HEAD" id="application.git_commit_sha" placeholder="HEAD"
|
<x-forms.input placeholder="HEAD" id="gitCommitSha" placeholder="HEAD" label="Commit SHA" />
|
||||||
label="Commit SHA" />
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@if (data_get($application, 'private_key_id'))
|
@if ($privateKeyId)
|
||||||
<h3 class="pt-4">Deploy Key</h3>
|
<h3 class="pt-4">Deploy Key</h3>
|
||||||
<div class="py-2 pt-4">Currently attached Private Key: <span
|
<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>
|
</div>
|
||||||
|
|
||||||
<h4 class="py-2 ">Select another Private Key</h4>
|
<h4 class="py-2 ">Select another Private Key</h4>
|
||||||
<div class="flex flex-wrap gap-2">
|
<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 wire:click.defer="setPrivateKey('{{ $key->id }}')">{{ $key->name }}
|
||||||
</x-forms.button>
|
</x-forms.button>
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|||||||
Reference in New Issue
Block a user