fix(map): selectFeature event handling - unify payload format for map click and feature table

- FeatureTable: dispatch { featureId } object instead of raw ID
- ProjectMap: selectFeature listener accepts both formats (direct ID or { featureId })
- project-map.blade.php: move selectFeature inside livewire:init handler so Livewire.emit is available
- Added logging for debugging

Fixes: 'Livewire.emit is not a function' on map click and 'Only arrays and Traversables can be unpacked' on table button
This commit is contained in:
Javier Braña
2026-08-24 11:12:32 +02:00
parent 534d9f8eab
commit c58a49f22d
3 changed files with 25 additions and 14 deletions
+12 -10
View File
@@ -69,15 +69,17 @@ class FeatureTable extends DataTableComponent
->html(),
Column::make('Acciones')
->label(fn ($row) =>
'<div class="flex justify-end">'
. '<button wire:click="$dispatch(\'map-select-feature\', ' . $row->id . ')"'
. 'class="btn btn-xs btn-primary gap-1" title="Editar elemento">'
. '<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>'
. 'Abrir'
. '</button>'
. '</div>')
->html(),
->label(function ($row) {
$id = $row->id;
return '<div class="flex justify-end">'
. '<button wire:click="$dispatch(\'map-select-feature\', { featureId: ' . $id . ' })"'
. 'class="btn btn-xs btn-primary gap-1" title="' . e(__('Editar elemento')) . '">'
. '<svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>'
. e(__('Abrir'))
. '</button>'
. '</div>';
})
->html(),
];
}
@@ -102,4 +104,4 @@ class FeatureTable extends DataTableComponent
->filter(fn (Builder $query, string $value) => $query->whereHas('layer', fn ($l) => $l->where('phase_id', $value))),
];
}
}
}
+7
View File
@@ -228,6 +228,13 @@ class ProjectMap extends Component
#[On('map-select-feature')]
public function selectFeature($featureId)
{
\Log::info('map-select-feature received', ['payload' => $featureId]);
// Handle both formats: direct ID or { featureId: X }
if (is_array($featureId) && isset($featureId['featureId'])) {
$featureId = $featureId['featureId'];
}
$this->selectedFeature = null;
$feature = Feature::with(['template', 'layer.phase'])->find($featureId);
if (!$feature) return;
@@ -724,10 +724,6 @@
}
}
function selectFeature(featureId) {
Livewire.emit('map-select-feature', featureId);
}
function getUserLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((pos) => {
@@ -743,6 +739,12 @@
document.addEventListener('livewire:init', function () {
setTimeout(initMap, 50);
// Define selectFeature here so Livewire is guaranteed to be available
window.selectFeature = function(featureId) {
console.log('selectFeature called with:', featureId);
Livewire.emit('map-select-feature', { featureId: featureId });
};
Livewire.on('layersUpdated', (activeIds) => {
const ids = Array.isArray(activeIds) ? activeIds[0] : activeIds;
for (let id in layers) {