coolify/app/Livewire/Project/Application/Preview/Form.php

64 lines
1.8 KiB
PHP
Raw Normal View History

2023-05-30 13:52:17 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Application\Preview;
2023-05-30 13:52:17 +00:00
use App\Models\Application;
2024-11-08 14:10:08 +00:00
use Livewire\Attributes\Validate;
2023-05-30 13:52:17 +00:00
use Livewire\Component;
use Spatie\Url\Url;
class Form extends Component
{
public Application $application;
2024-06-10 20:43:34 +00:00
#[Validate('required')]
2024-11-04 10:32:03 +00:00
public string $previewUrlTemplate;
2024-06-10 20:43:34 +00:00
2024-11-04 10:32:03 +00:00
public function mount()
2023-05-30 13:52:17 +00:00
{
2024-11-04 10:32:03 +00:00
try {
$this->previewUrlTemplate = $this->application->preview_url_template;
$this->generateRealUrl();
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-05-30 13:52:17 +00:00
}
2024-11-04 10:32:03 +00:00
public function submit()
2023-05-30 13:52:17 +00:00
{
2024-11-04 10:32:03 +00:00
try {
$this->resetErrorBag();
$this->validate();
$this->application->preview_url_template = str_replace(' ', '', $this->previewUrlTemplate);
$this->application->save();
$this->dispatch('success', 'Preview url template updated.');
$this->generateRealUrl();
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-30 13:52:17 +00:00
}
}
2024-11-04 10:32:03 +00:00
public function resetToDefault()
2023-05-30 13:52:17 +00:00
{
2024-11-04 10:32:03 +00:00
try {
$this->application->preview_url_template = '{{pr_id}}.{{domain}}';
$this->previewUrlTemplate = $this->application->preview_url_template;
$this->application->save();
$this->generateRealUrl();
$this->dispatch('success', 'Preview url template updated.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
2023-05-30 13:52:17 +00:00
}
2024-11-04 10:32:03 +00:00
public function generateRealUrl()
2023-05-30 13:52:17 +00:00
{
2024-11-04 10:32:03 +00:00
if (data_get($this->application, 'fqdn')) {
$firstFqdn = str($this->application->fqdn)->before(',');
$url = Url::fromString($firstFqdn);
$host = $url->getHost();
$this->previewUrlTemplate = str($this->application->preview_url_template)->replace('{{domain}}', $host);
}
2023-05-30 13:52:17 +00:00
}
}