jantinnerezo / livewire-alert

SweetAlert2 wrapper for Livewire
https://livewire-alert.jantinnerezo.com
MIT License
682 stars 73 forks source link

Delete Confirmation with Sweet Alert #126

Open spitfire64 opened 9 months ago

spitfire64 commented 9 months ago

How can I accomplish this? Also It would be nice with a success alert after delete is complete At the moment I have this code that results in a standard, ugly alert when I want to delete a Product. app\Livewire\Products.php

public function deleteProduct(int $productId): void
    {
        Product::where('id', $productId)->delete();
        $this->resetPage();
    }

resources\views\livewire\products.blade.php

 @forelse($products as $product)
....
  <a wire:click="deleteProduct({{ $product->id }})" onclick="return confirm('Are you sure?') || event.stopImmediatePropagation()" href="#" class="inline-flex items-center px-4 py-2 bg-red-600 rounded-md font-semibold text-xs text-white uppercase tracking-widest">
                          Delete
  </a>
...
 @empty
.....
@endforelse

I use Livewire 3 Laravel 10

jantinnerezo commented 7 months ago

Hi @spitfire64 , not sure if you already did but you can do

public Product $product;

public function deleteProduct(int $productId): void
{
    $this->product = Product::find($productId);

    if ($this->product) {
        $this->confirm('Are you sure do want to leave?', [
           'onConfirmed' => 'confirmed', // function to call when user click yes
        ]);
    }
}

public function confirmed(): void
{
      $this->product->delete();
}
sushilAsCambodia commented 3 months ago

public function confirmed(): void { $this->product->delete(); } this function will not work.

need to add listeners. =>

protected $listeners = [
    'confirmed'
];