Hello,
I know the title might sound confusing, but I hope I manage to explain myself properly.
I currently have a blade view with different components. In one of these I want to perform a fetch() when a value has been picked from a dropdown.
@component('company::components.form.select', [
'name' => 'warehouse_id',
'required' => true,
'label' => trans('Almacen'),
'icon' => 'warehouse',
'options' => $warehouses,
'event' => '@form-select-warehouseid-changed.window="getDeletionData"'
])@endcomponent
And getDeletionData is being declared at the end of the blade file, in a script tag, such as:
function getDeletionData($event) {
const warehouseId = $event.detail.value;
const url = new URL('{{ route('company.api.stocks.clear.countdeletions') }}');
url.searchParams.append('warehouse_id', warehouseId);
fetch(url)
.then(response => response.json())
.then(data => {
// Handle the response data here
const movementsInputField = document.querySelector('input[name=\'movements\']');
const stocksInputField = document.querySelector('input[name=\'stocks\']');
movementsInputField.value = data.movements_count;
stocksInputField.value = data.stocks_count;
});
}
My line manager has told me not to follow this approach because all the components in the rest of the projects have the js code directly written down where my current getDeletionData would be.
Due to single quotes, double quotes, trying to get a php variable (route(...)) is just a huge mess, and no matter how hard I try, I just cannot put this code in the component
@component('company::components.form.select', [
'name' => 'warehouse_id',
'required' => true,
'label' => trans('Almacen'),
'icon' => 'warehouse',
'options' => $warehouses,
'event' => '@form-select-warehouseid-changed.window="
const warehouseId = $event.detail.value;
//...
"'
])@endcomponent
A hand is greatly appreciated, as I am quite frustrated for both not being able to follow a much more clear and easier to maintain code style, and also not being able to put the code there directly, no matter how many quotes i escape, etc, I just can't.
Thank you