cms-laravel/app/Livewire/Promotions/PromotionsList.php

56 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\Promotions;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class PromotionsList extends Component
{
use LivewireAlert;
public $promotions = [];
public function mount()
{
$this->fetchPromotions();
}
public function fetchPromotions()
{
try {
$response = Http::get('https://api.example.com/promotion');
if ($response->successful()) {
$this->promotions = $response->json()['data'] ?? [];
} else {
$this->alert('error', 'Failed to fetch promotions.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
}
}
public function delete($id)
{
if (confirm('Are you sure you want to delete this promotion?')) {
try {
$response = Http::delete("https://api.example.com/promotion/{$id}");
if ($response->successful()) {
$this->alert('success', 'Promotion deleted successfully!');
$this->fetchPromotions();
} else {
$this->alert('error', 'Failed to delete promotion.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
}
}
}
public function render()
{
return view('livewire.promotions.promotions-list')->layout('layouts.app', ['title' => 'Promotions']);
}
}