GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
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');
    }
}