Complete add/edit/delete for scheduled tasks.

Refactor views.
This commit is contained in:
Stuart Rowlands
2024-01-01 18:23:29 -08:00
parent adecf328fc
commit 7913a639b5
8 changed files with 129 additions and 194 deletions

View File

@@ -33,7 +33,6 @@ class Add extends Component
public function submit()
{
error_log("*** IN SUBMIT");
$this->validate();
$isValid = validate_cron_expression($this->frequency);
if (!$isValid) {

View File

@@ -10,102 +10,23 @@ use Illuminate\Support\Str;
class All extends Component
{
public $resource;
public bool $showPreview = false;
public string|null $modalId = null;
public ?string $variables = null;
public ?string $variablesPreview = null;
public array $parameters;
protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit'];
public function mount()
{
$resourceClass = get_class($this->resource);
$resourceWithPreviews = ['App\Models\Application'];
$simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
if (Str::of($resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
$this->showPreview = true;
}
$this->parameters = get_route_parameters();
$this->modalId = new Cuid2(7);
$this->getDevView();
}
public function getDevView()
{
$this->variables = $this->resource->scheduled_tasks->map(function ($item) {
error_log("** got one");
return "$item->name=$item->command";
})->sort();
error_log(print_r($this->variables,1));
}
public function saveVariables($isPreview)
{
if ($isPreview) {
$variables = parseEnvFormatToArray($this->variablesPreview);
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
} else {
$variables = parseEnvFormatToArray($this->variables);
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
}
foreach ($variables as $key => $variable) {
if ($isPreview) {
$found = $this->resource->environment_variables_preview()->where('key', $key)->first();
} else {
$found = $this->resource->environment_variables()->where('key', $key)->first();
}
if ($found) {
if ($found->is_shown_once) {
continue;
}
$found->value = $variable;
$found->save();
continue;
} else {
$task = new ScheduledTask();
$task->key = $key;
$task->value = $variable;
$task->is_build_time = false;
$task->is_preview = $isPreview ? true : false;
switch ($this->resource->type()) {
case 'application':
$task->application_id = $this->resource->id;
break;
case 'standalone-postgresql':
$task->standalone_postgresql_id = $this->resource->id;
break;
case 'standalone-redis':
$task->standalone_redis_id = $this->resource->id;
break;
case 'standalone-mongodb':
$task->standalone_mongodb_id = $this->resource->id;
break;
case 'standalone-mysql':
$task->standalone_mysql_id = $this->resource->id;
break;
case 'standalone-mariadb':
$task->standalone_mariadb_id = $this->resource->id;
break;
case 'service':
$task->service_id = $this->resource->id;
break;
}
$task->save();
}
}
if ($isPreview) {
$this->dispatch('success', 'Preview environment variables updated successfully.');
} else {
$this->dispatch('success', 'Environment variables updated successfully.');
}
$this->refreshTasks();
}
public function refreshTasks()
{
$this->resource->refresh();
$this->getDevView();
}
public function submit($data)
{
error_log("** submitting the beast");
try {
$task = new ScheduledTask();
$task->name = $data['name'];

View File

@@ -4,47 +4,52 @@ namespace App\Livewire\Project\Shared\ScheduledTask;
use App\Models\ScheduledTask as ModelsScheduledTask;
use Livewire\Component;
use App\Models\Application;
use App\Models\Service;
use Visus\Cuid2\Cuid2;
class Show extends Component
{
public $parameters;
public Application|Service $resource;
public ModelsScheduledTask $task;
public ?string $modalId = null;
public bool $isDisabled = false;
public bool $isLocked = false;
public string $type;
protected $rules = [
'task.name' => 'required|string',
'task.command' => 'required|string',
'task.frequency' => 'required|string',
'task.container' => 'nullable|string',
];
protected $validationAttributes = [
'name' => 'Name',
'command' => 'Command',
'name' => 'name',
'command' => 'command',
'frequency' => 'frequency',
'container' => 'container',
];
public function mount()
{
$this->modalId = new Cuid2(7);
$this->parameters = get_route_parameters();
if (data_get($this->parameters, 'application_uuid')) {
$this->type = 'application';
$this->resource = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail();
} else if (data_get($this->parameters, 'service_uuid')) {
$this->type = 'service';
$this->resource = Service::where('uuid', $this->parameters['service_uuid'])->firstOrFail();
}
$this->modalId = new Cuid2(7);
$this->task = ModelsScheduledTask::where('uuid', request()->route('task_uuid'))->first();
}
public function lock()
{
$this->task->is_shown_once = true;
$this->task->save();
$this->dispatch('refreshTasks');
}
public function instantSave()
{
$this->submit();
}
public function submit()
{
$this->validate();
$this->task->save();
$this->dispatch('success', 'Environment variable updated successfully.');
$this->dispatch('success', 'Scheduled task updated successfully.');
$this->dispatch('refreshTasks');
}
@@ -52,7 +57,14 @@ class Show extends Component
{
try {
$this->task->delete();
$this->dispatch('refreshTasks');
if ($this->type == 'application') {
return redirect()->route('project.application.configuration', $this->parameters);
}
else {
return redirect()->route('project.service.configuration', $this->parameters);
}
} catch (\Exception $e) {
return handleError($e);
}