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/Buyer/Review.php
<?php

namespace App\Livewire\Buyer;

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

class Review extends Component
{
    public $reviews = [];
    public $user;
    public $photo;
    public $gigId;
    public $comment;
    public $star_rating;

    use WithFileUploads;

    public function mount()
    {
        $this->user = Session::get('user');
        Session::forget('success');
        Session::forget('error');
        if ($this->user && $this->user['role'] == 'buyer') {
            $this->buyerReview();
        } else {
            $this->sellerReview();
        }
    }

    public $pendingGigs = [];

    public function buyerReview()
    {
        try {
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchBuyerReviews());
            if ($response->successful()) {
                $this->reviews = $response->json()['data'];
                $this->pendingGigs = $response->json()['pending'];
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            $this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
        }
    }

    public function sellerReview()
    {
        try {
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchSellerReviews());
            if ($response->successful()) {
                $this->reviews = $response->json()['data'];
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            $this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
        }
    }

    public function submitReview($gigId)
    {
        $this->gigId = $gigId;
        // dd($this->gigId);
        $this->validate([
            'gigId' => 'required|string|exists:gigs,id',
            'comment' => 'required|string|max:255',
            'star_rating' => 'required|numeric|min:1|max:5',
            'photo' => 'nullable|image',
        ]);
        $photo_url = null;
        $photo_public_id = null;
        if ($this->photo) {
            $uploadedFile = $this->photo->getRealPath();
            $uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'reviewPhoto']);
            $photo_url = $uploadResult->getSecurePath();
            $photo_public_id = $uploadResult->getPublicId();
        }
        $body = [
            'gig_id' => $this->gigId,
            'comment' => $this->comment,
            'star_rating' => $this->star_rating,
            'photo_url' => $photo_url,
            'photo_public_id' => $photo_public_id,
        ];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->post(ApiEndpoints::submitReview());
        if ($response->json()['status'] == 'success') {
            // Session::flash('success', $response->json()['message']);
            $this->redirect('/account/review');
        } else {
            // Session::flash('error', $response->json()['data']);
            $this->redirect('/account/review');
        }
    }

    public function render()
    {
        return view('livewire.buyer.review');
    }
}