Skip to content

Hard to track down missing $rowActionArray #160

@Iaotle

Description

@Iaotle

Image

Schermopname_2024-09-09_om_12.25.37.mov

The table in question:

<?php

namespace App\Tables;

use App\Tables\RowActions\CopyReplicationAction;
use App\Tables\RowActions\RunWorkerForReplicationAction;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Okipa\LaravelTable\Filters\BooleanFilter;
use Okipa\LaravelTable\HeadActions\CreateHeadAction;
use Okipa\LaravelTable\RowActions\DestroyRowAction;
use Okipa\LaravelTable\RowActions\EditRowAction;
use App\Tables\AbstractQueryTable;
use Okipa\LaravelTable\Column;
use Okipa\LaravelTable\Table;
use App\Replication;

class ReplicationsTable extends AbstractQueryTable {
	public int $replicator;
	public string $selectedWorkerID = '';
	/**
	 * Configure the table itself.
	 *
	 * @return \Okipa\LaravelTable\Table
	 * @throws \ErrorException
	 */
	protected function table(): Table {
		$routeParams = [
			'replicator' => $this->replicator,
		];
		// Setup route implicit binding params and names

		if (Gate::check('edit-replications', null)) {
			Route::get('replicators/{replicator}/replications/create', [
				'as' => 'replicators.replications.create',
				fn() => null,
			]);
			Route::get('replications/{replication}/edit', [
				'as' => 'replications.edit',
				fn() => null,
			]);
			Route::get('replications/{replication}', [
				'as' => 'replications.destroy',
				fn() => null,
			]);
		}

		return Table::make()
			->model(Replication::class)
			->filters([new BooleanFilter(__('general.completed_fields'), 'has_all_fields_completed')])
			->rowActions(
				fn(Replication $replication) => [
					new RunWorkerForReplicationAction(),
					new CopyReplicationAction(),
					new EditRowAction(
						route('replications.edit', [
							'replication' => $replication,
						]),
					),
					(new DestroyRowAction())->confirmationQuestion(
						__('activity.delete_confirm', [
							'model' => __('general.replication'),
							'name' => $replication->name,
						]),
					),
				],
			)
			->headAction(new CreateHeadAction(route('replicators.replications.create', $routeParams)))
			->numberOfRowsPerPageOptions($this->getRowsNumber())
			->query(function (Builder $query) {
				if (isset($this->builder)) {
					$query->setQuery($this->builder);
				}
				$query->where('replicator_id', $this->replicator);
			});
	}

	/**
	 * Configure the table columns.
	 * Uses the Replication model
	 * @param \Okipa\LaravelTable\Table $table
	 *
	 * @throws \ErrorException
	 */
	protected function columns(): array {
		return [
			// Uncomment below to show ID column
			// Column::make('id')
			// 	->title(__('general.id'))
			// 	->searchable()
			// 	->sortable(),
			Column::make('name')->title(__('general.name'))->searchable()->sortable(),
			Column::make('completed_fields')
				->title(__('general.completed_fields'))
				->format(function (Replication $replication) {
					$fields = array_column($replication->replicator->configFields(), 'name');
					$replicationFields = $replication->configuration;
					$fieldsPresent = array_filter($replicationFields, fn($key) => in_array($key, $fields), ARRAY_FILTER_USE_KEY);

					$status = count($fieldsPresent) !== count($fields) ? 'warning' : 'success';
					return "<span class=\"badge rounded-pill bg-{$status}\" style=\"font-size:100%\">" .
						count($fieldsPresent) .
						'/' .
						count($fields) .
						'</span>';
				}),
			Column::make('active_status')
				->title(__('general.active_status'))
				->format(function (Replication $replication) {
					$route = route('toggle_replicator_status', [
						'replicator' => $replication->replicator_id,
						'replication' => $replication->id,
					]);
					$status = $replication->active_status ? __('general.active') : __('general.inactive');
					$badge = $replication->active_status ? 'bg-success' : 'bg-warning';

					return <<<HTML
						<a href=$route>
							<span class="badge rounded-pill $badge" style="font-size:100%">$status</span>
						</a>
					HTML;
				})
				->searchable(),
		];
	}
}

and the relevant action:

<?php

namespace App\Tables\RowActions;

use Illuminate\Database\Eloquent\Model;
use App\Traits\HasFlashMessages;
use App\Replicator;
use App\Worker;
use Livewire\Component;

class RunWorkerForReplicationAction extends AbstractIDSelectAction {
	use HasFlashMessages;

	protected function class(Model $model): array {
		return [];
	}

	protected function title(Model $model): string {
		return __('activity.run_replication');
	}

	protected function icon(Model $model): string {
		return '<i class="fas fa-play text-success"></i>';
	}

	protected function gatherSettings(Model $model): array|null {
		// make the user pick a worker to run the replication on. Uses ReplicationWorkerSelect component
		/** @var Replicator */
		$replicator = $model->replicator()->first();
		$workers = $replicator->workers()->pluck('name', 'id')->toArray();

		return ['options' => $workers, 'resoursePath' => 'workers'];
	}

	protected function defaultFeedbackMessage(Model $model): string|null {
		return null;
	}

	public function action(Model $model, Component $livewire) {
		$worker_id = $livewire->configParams['id']; // result from the modal
		$worker = Worker::find($worker_id);
		return redirect()->route('replication.run', ['id' => $worker->id, 'replication_id' => $model->id]);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions