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

59 lines
1.7 KiB
PHP

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