Skip to content

Commit

Permalink
change event file name,now show real-time updates
Browse files Browse the repository at this point in the history
  • Loading branch information
golam-sorwar committed Aug 17, 2019
1 parent a6103f1 commit 66be0ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class ProductAddedToCart implements ShouldBroadcast
class ProductUpdatedInCart implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

Expand All @@ -19,9 +19,11 @@ class ProductAddedToCart implements ShouldBroadcast
*
* @return void
*/
public function __construct()
public $productId;

public function __construct($productId)
{
//
$this->productId = $productId;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/CartCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class CartCounter extends Component
{
protected $listeners = [
'productAdded' => 'noop',
'productRemoved' => 'noop'
'productUpdated' => 'noop',
// 'productRemoved' => 'noop'
];

public function noop()
Expand Down
12 changes: 10 additions & 2 deletions app/Http/Livewire/CartProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Livewire\Component;
use App\Cart;
use App\CartProduct as AppCartProduct;
use App\Events\ProductUpdatedInCart;
use App\Product;

class CartProduct extends Component
Expand All @@ -13,19 +14,26 @@ class CartProduct extends Component
public $name;
public $price;

public function listeners()
{
return ['productUpdated:'.$this->id => '$refresh'];
}

public function add()
{
// $cart_id = Cart::first();
// CartPro::create(['cart_id' => $cart_id->id, 'product_id' => $this->id]);
Cart::first()->products()->attach($this->id);
$this->emit('productAdded');
// $this->emit('productAdded');
event(new ProductUpdatedInCart($this->id));
}

public function remove()
{
// CartProduct::where('product_id', $this->id)->delete();
Cart::first()->products()->detach($this->id);
$this->emit('productRemoved');
// $this->emit('productRemoved');
event(new ProductUpdatedInCart($this->id));
}

public function mount($product)
Expand Down
13 changes: 10 additions & 3 deletions resources/views/cart.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
<div class="absolute p-4 right-0">
@livewire('cart-counter')
</div>
<div class=" p-4 flex justify-center">
<div class="w-auto">
<input class="bg-green-100 appearance-none border-2 border-purple-700 rounded-full w-full py-2 px-32 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500" placeholder="Search ...." type="text">
</div>
</div>

<div class="pt-16 flex flex-wrap">
<div class="pt-12 flex flex-wrap">
@foreach ($products as $product)
@livewire('cart-product', $product)
@endforeach
Expand All @@ -29,8 +34,10 @@
<script>
window.addEventListener('DOMContentLoaded',function() {
Echo.channel('cart')
.listen('ProductAddedToCart', (e) => {
console.log('Its work');
.listen('ProductUpdatedInCart', (e) => {
// console.log('Its work');
window.livewire.emit('productUpdated');
window.livewire.emit('productUpdated:'+e.productId);
});
})
</script>
Expand Down

0 comments on commit 66be0ef

Please sign in to comment.