File: /var/www/html/app/Livewire/Gig/Listing.php
<?php
namespace App\Livewire\Gig;
use Livewire\Component;
use Livewire\WithPagination;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class Listing extends Component
{
public $active_gigs = [];
public $inactive_gigs = [];
public $shop_id;
use WithPagination;
public function mount()
{
Session::forget('success');
Session::forget('error');
$shop = Session::get('shop');
if (!$shop) {
Session::flash('error', 'You dont have shop. Create your shop here');
$this->redirect('/dashboard-shop');
} else {
$this->shop_id = $shop['id'] ?? $shop->id;
$this->fetchGigs();
}
}
public function fetchGigs()
{
try {
$body = [
"shop_id" => $this->shop_id,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->get(ApiEndpoints::fetchGig());
if ($response->successful()) {
$this->active_gigs = $response->json()['active'];
$this->inactive_gigs = $response->json()['inactive'];
} else {
Log::error("Failed to fetch seller gig. Status code: {$response->getMessage()}");
$this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
}
} catch (\Throwable $e) {
Log::error($e->getMessage());
$this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
}
}
public function deleteGig($id)
{
try {
$body = [
'id' => $id
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->delete(ApiEndpoints::deleteGig());
if ($response->successful()) {
Session::flash('success', $response->json()['message']);
$this->mount();
} else {
Log::error("Failed to delete gig. Status code: {$response->json()['message']}");
$this->addError('delete', 'Failed to delete gig. Please try again later.');
}
} catch (\Throwable $e) {
Log::error($e->getMessage());
Session::flash('error', 'An error occurred while deleting the gig.');
}
}
public function render()
{
return view('livewire.gig.listing');
}
}