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');
}
}