Respect's avatar

how to show delete confirmation and make request continmue

  • hot to make request continue
<script>
        document.addEventListener('livewire:init', () => {
            Livewire.hook('component.init', ({
                component
            }) => {
                component.intercept('delete', async ({
                    action
                }) => {
                    action.cancel();
                    const result = await Swal.fire({
                        title: 'Are you sure?',
                        text: "This action is permanent!",
                        icon: 'warning',
                        showCancelButton: true,
                        confirmButtonText: 'Yes, delete it!',
                        cancelButtonText: 'Cancel'
                    });

                    // 3. If confirmed, resume the action
                    if (result.isConfirmed) {
                        // how to make request continue to delete
                    }
                })
            });
        });
    </script>
    public function delete($id)
    {
        $jobTitle = JobTitle::findOrFail($id)->delete();
    }
0 likes
4 replies
imranbru's avatar

intercept the click, show the alert, and then trigger the method via $wire if they confirm.

Drop the <script> hook entirely and just do this on your view:

<button 
    x-data 
    @click.prevent="
        Swal.fire({
            title: 'Are you sure?',
            text: 'This action is permanent!',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {
                $wire.delete({{ $id }})
            }
        })
    "
>
    Delete
</button>

alse use wire:confirm:

<button wire:click="delete({{ $id }})" wire:confirm="Are you sure? This action is permanent!">
    Delete
</button>
Respect's avatar

i need it globally no need put it in each action button need something put it inside app-blade.php

Jsanwo64's avatar

something like this

<script>
document.addEventListener('livewire:init', () => {
    Livewire.hook('commit', ({ commit, succeed, fail }) => {

        // Only intercept specific actions (delete)
        if (!commit.method || commit.method !== 'delete') {
            return;
        }

        commit.prevent(); // stop request immediately

        Swal.fire({
            title: 'Are you sure?',
            text: "This action is permanent!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes, delete it!',
            cancelButtonText: 'Cancel'
        }).then((result) => {

            if (result.isConfirmed) {
                commit.resume(); // 🔥 continue original Livewire request
            }
        });
    });
});
</script>

What you were missing

  1. action.cancel() → prevents execution
  2. But there is no action.resume() in that hook
  3. The correct object to control flow is the commit object

So the lifecycle is:

commit.prevent()  → stops request
commit.resume()   → continues original request

Please or to participate in this conversation.