0

So I have a component in my blade that I want to display the updated value. This value is used to display the correct icon.

The problem with using livewire is that whenever I call togglePinned() it doesn't (2nd if statement) immediately show me the correct icon even though the value is updated in the backend.

 @php
        $isMyTask = $task->assignees->where('id', console_user()->id)->count();
        $task_user = $task->assignees->where('id', auth()->id())->first();
        $read = $task_user?->pivot->read === 1;
        $pinned = $task_user?->pivot->pinned === 1;
    @endphp
(.. some code ..)
<div>
            @if ($isMyTask)
                <a href="#"
                    x-data="{ pinned: @entangle('pinnedTask') }"
                    @click.prevent="$wire.togglePinned(); console.log('pinned', pinned)"
                    @pinned-updated.window="if ($event.detail.task_id === {{ $task->id }}) pinned = $event.detail.pinned"
                    :key="pinned ? 'unpinned-{{ $task?->id }}' : 'pinned-{{ $task?->id }}'">
                    <i :class="pinned ? 'fas fa-fw fa-thumbtack' : 'fal fa-fw fa-thumbtack'" aria-hidden="true"></i>
                </a>
            @endif
            @if ($isMyTask)
                <a href="#" x-tooltip.placement.top.raw="{{ $pinned ? 'Unpin' : 'Pin' }}"
                    wire:click="togglePinned" wire:key="{{ $pinned ? 'unpinned' : 'pinned' }}-{{ $task?->id }}">
                    <i class="{{ $pinned ? 'fas' : 'fal' }} fa-fw fa-thumbtack" aria-hidden="true"></i>
                </a>{{( 'KEY ID' . $task?->id) . ' ' . ($pinned ? 'pinned' : 'unpinned') }}
            @endif
        </div>

This is how my backend look like. Is there a way for me to correctly return the right value after calling togglePinned() so that I could display the correct icon using alpine js?

public function togglePinned()
{
    if (! $this->task) {
        return;
    }

    $currentPinnedState = $this->task->assignees()->where('users.id', auth()->id())->first()->pivot->pinned;
    $this->task->assignees()->where('users.id', auth()->id())->first()->pivot->update([
        'pinned' => ! $currentPinnedState,
    ]);

    $this->pinnedTask = [
        'task_id' => $this->task->id,
        'pinned' => ! $currentPinnedState,
    ];
    
    $this->emit('taskUpdated');
}
3
  • What is the version of Livewire? You're repeating @if ($isMyTask) twice in a row: is that a typo? Commented Mar 20 at 2:09
  • @TUPKAP Well it isn't just a bad code. I am using livewire v2 Commented Mar 20 at 10:41
  • Using the fal class doesn't work for me, but changing the icon displayed when unpinned works. I can't figure out why you're using a wire:key unless you're in a loop. However I think you shouldn't change the prefix (pinned / unpinned) to wire:key. The key attribute in your Alpine version can be removed. Commented Mar 20 at 21:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.