File: /var/www/html/app/Livewire/Certificate/Listing.php
<?php
namespace App\Livewire\Certificate;
use Livewire\Component;
use Livewire\WithPagination;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class Listing extends Component
{
use WithPagination;
public $user_id;
public $certificates = [];
public function mount()
{
$user = Session::get('user', []);
Session::forget('success');
Session::forget('error');
if ($user) {
$this->user_id = $user['id'];
$this->fetchCertificate();
} else {
Session::flash('error', 'You dont have certificate. Create your certificate here');
return redirect('/dashboard-certificate');
}
}
public function fetchCertificate()
{
try {
$body = [
"user_id" => $this->user_id,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->get(ApiEndpoints::fetchCert());
// dd($response->json());
if ($response->successful()) {
$this->certificates = $response->json()['data'];
} 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 deleteCert($id)
{
try {
// Prepare the body for the request
$body = ['id' => $id];
// Make the API request to delete the gig
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->delete(ApiEndpoints::deleteCert()); // Use DELETE method
if ($response->successful()) {
$this->mount();
Session::flash('success', 'Certificate deleted successfully.');
} else {
$this->mount();
Session::flash('success', 'Failed to delete Certificate. Please try again later.');
}
} catch (\Throwable $e) {
Log::error($e->getMessage());
Session::flash('error', 'An error occurred while deleting the Certificate.');
}
}
public function render()
{
return view('livewire.certificate.listing');
}
}