Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Select Component does not update after add new option #215

Open
jcabanillas opened this issue Dec 31, 2024 · 0 comments
Open

Custom Select Component does not update after add new option #215

jcabanillas opened this issue Dec 31, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jcabanillas
Copy link

Laravel Form Components Version

v8.x

Laravel Version

v11

Alpine Version

No response

Bug description

I have the following problem:
when adding a new provider on form, the provider is added as part of the menu options but is not displayed as the default option, why is this happening and how can I fix it?

Steps to reproduce

I have this code on my view form:


x-slot:before

</x-slot:before>

I have this code on my AdminComponent:

#[On('proveedorGuardado')]
public function seleccionarProveedor($id)
{
Log::info("Entra a Seleccionar Proveedor");
Log::info($id);
// dd($id);
// Agregar el nuevo proveedor a la lista de opciones
$nuevoProveedor = Proveedor::findOrFail($id);
$this->proveedores[] = [
'value' => $nuevoProveedor['id'],
'label' => $nuevoProveedor['nombre'],
];

    $this->model->id_proveedor = $id;
}

public function handleSearch($searchTerm)
{
    // dd($searchTerm);
    $proveedores = collect();

    /*
    $id = $request->get('id');
    if ($id) {
        // Buscar el proveedor por ID para incluirlo como opción preseleccionada
        $selectedOption = Proveedor::where('id', $id)
            ->where('id_empresa', Auth::user()->empresa()->id)
            ->where('activo', 1)
            ->where('es_cliente', 0)
            ->first();

        if ($selectedOption) {
            $options->push([
                'value' => $selectedOption->id, // Clave para custom-select
                'label' => $selectedOption->nombre, // Valor para custom-select
            ]);
        }
    }
    */

    // Buscar proveedores solo si el término tiene al menos 3 caracteres
    if (strlen($searchTerm) >= 3) {
        $searchResults = Proveedor::where('nombre', 'like', '%' . $searchTerm . '%')
            ->where('id_empresa', Auth::user()->empresa()->id)
            ->where('activo', 1)
            ->where('es_cliente', 0)
            ->get(['id', 'nombre'])
            ->map(fn($option) => [
                'value' => $option->id, // Clave para custom-select
                'label' => $option->nombre, // Valor para custom-select
            ]);

        // Combinar los resultados de búsqueda con el proveedor preseleccionado
        $proveedores = $proveedores->merge($searchResults);
    }
    $this->proveedores = $proveedores;
}

And this is the code of my component for create a provider:

public function save()
{
$this->validate();
try {
$this->proveedor->es_cliente = 0;
$this->proveedor->save();
$this->close(); // Cierra el modal
$this->alert('success', 'Proveedor registrado correctamente.');
// Emitir el evento con el ID del proveedor guardado
$this->dispatch('proveedorGuardado', $this->proveedor->id);
session()->flash('success', 'Proveedor registrado correctamente.');
} catch (\Exception $e) {
Log::info($e);
$this->dispatch('modal.close');
session()->flash('success', 'Proveedor no se registró correctamente.');
}
}

Relevant log output

No response

@jcabanillas jcabanillas added the bug Something isn't working label Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant