File: /var/www/html/app/Livewire/Gig/Update.php
<?php
namespace App\Livewire\Gig;
use Livewire\Component;
use Illuminate\Support\Str;
use Livewire\WithFileUploads;
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 Update extends Component
{
use WithFileUploads;
public $id;
public $slug;
public $gig;
public $shop_id;
public $user_id;
public $category_id;
public $currency;
public $formattedPrice;
public $price;
public $status;
public $subcategory_id;
public $subcategories = [];
public $title;
public $delivery_number;
public $delivery_unit; // Default time unit
public $description;
public $new_img_url;
public $image_urls;
public $image_public_ids;
public function mount($slug)
{
$this->slug = $slug;
$this->queryGig();
$gigData = $this->gig;
$this->id = $gigData['id'];
$this->title = $gigData['title'];
$this->description = $gigData['description'];
$this->price = $gigData['price'];
$this->status = $gigData['status'];
$this->delivery_number = $gigData['delivery_number'];
$this->delivery_unit = $gigData['delivery_unit'];
$this->subcategory_id = $gigData['subcategory_id'];
$this->image_urls = $gigData['image_urls'];
$this->image_public_ids = $gigData['image_public_ids'];
$this->shop_id = $gigData['shop_id'];
$this->category_id = $gigData['category_id'];
$this->subcategories = DB::table('categories')
->where('parent_id', $this->category_id)
->orderBy('name', 'asc')
->get();
$this->currency = $gigData['currency'];
$this->formattedPrice = $this->formatPrice($this->price);
}
public function updatedPrice($value)
{
$this->formattedPrice = $this->formatPrice($value);
}
public function formatPrice($value)
{
// Ensure the value is numeric before formatting
if (is_numeric($value)) {
return number_format((float)$value, 0, '.', ',');
}
return $value;
}
public function queryGig()
{
try {
$body = [
"gig_slug" => $this->slug,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->get(ApiEndpoints::queryGig());
if ($response->successful()) {
$this->gig = $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 $th) {
return $th;
}
}
public function deleteGigPhoto($publicId)
{
$body = [
'gig_id'=> $this->id,
'public_id'=> $publicId,
];
// dd($body);
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->delete(ApiEndpoints::deleteGigPhoto());
if ($response->successful()) {
$info = $response->json()['message'];
$this->mount($this->slug);
$this->addError('new_img_url', $info);
} else {
Log::error('Failed to add a new service ' . $response->json()['message']);
$this->addError('new_img_url', $response->json('message'));
}
}
public function uploadNewImage()
{
if ($this->new_img_url && is_array($this->new_img_url)) {
// Initialize arrays for new and old images
$new_img_url = [];
$new_image_public_ids = [];
// Upload new images
foreach ($this->new_img_url as $photo) {
$uploadedPhoto = $photo->getRealPath();
$uploadResult = cloudinary()->upload($uploadedPhoto, ['folder' => 'gigPhotos']);
$new_img_url[] = $uploadResult->getSecurePath();
$new_image_public_ids[] = $uploadResult->getPublicId();
}
// Get old images
$old_img_url = $this->image_urls ?? [];
$old_image_public_ids = $this->image_public_ids ?? [];
// Combine old and new images
$combined_img_url = array_merge($old_img_url, $new_img_url);
$combined_image_public_ids = array_merge($old_image_public_ids, $new_image_public_ids);
// Limit the total to a maximum of 4 images
$combined_img_url = array_slice($combined_img_url, 0, 4);
$combined_image_public_ids = array_slice($combined_image_public_ids, 0, 4);
// Update the properties with the combined arrays
$this->image_urls = $combined_img_url;
$this->image_public_ids = $combined_image_public_ids;
} else {
// If no new images are uploaded, use the existing images
$this->image_urls = $this->image_urls ?? [];
$this->image_public_ids = $this->image_public_ids ?? [];
}
}
public function updateService()
{
try {
$this->uploadNewImage();
$body = [
'slug' => Str::slug($this->title),
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'price' => preg_replace('/[^0-9.]/', '', $this->price),
'status' => $this->status,
'subcategory_id' => $this->subcategory_id,
'image_urls' => $this->image_urls,
'image_public_ids' => $this->image_public_ids,
'delivery_unit' => $this->delivery_unit,
'delivery_number' => $this->delivery_number,
'currency' => $this->currency,
];
// dd($body);
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->put(ApiEndpoints::updateGig());
if ($response->successful()) {
$gig = $response->json()['data'];
Session::put('gig', $gig);
$gig = Session::get('gig');
Session::flash('success', 'Service updated successfully.');
return redirect()->to('/dashboard-gigs');
} else {
Log::error('Failed to add a new service ' . $response->json()['data']);
$this->addError('image_urls', $response->json('data'));
}
// Flash success message and redirect
Session::flash('success', 'Service successfully updated.');
return redirect('/dashboard-gigs');
} catch (\Throwable $th) {
Log::error($th);
Session::flash('error', 'Failed to update the service.');
return redirect('/dashboard-gigs');
}
}
public function render()
{
return view('livewire.gig.update');
}
}