File: /var/www/html/app/Livewire/Shop/Add.php
<?php
namespace App\Livewire\Shop;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithFileUploads;
class Add extends Component
{
use WithFileUploads;
public $category_id;
public $user_id;
public $name;
public $description;
public $address;
public $photo_url;
public $profile_photo_url;
public $workshop_photo_urls = [];
public $city_id;
public $latitude;
public $longitude;
public $country_id;
public $selectedState;
public $states = [];
public $cities = [];
public $countries = [];
public $categories = [];
public $plans = [];
public $currentStep = 1; // Starts at step 1
protected $rules = [
'category_id' => 'required|exists:categories,id',
'user_id' => 'required|exists:users,id',
'name' => 'required|string|max:255',
'description' => 'required|string|max:2000',
'address' => 'required|string|max:255',
'profile_photo_url' => 'required|image|max:1024', // max 1MB
'workshop_photo_urls.*' => 'required|image|max:1024', // Adjust as needed
'city_id' => 'required|exists:cities,id',
'state' => 'required|exists:states,id',
'country_id' => 'required|exists:countries,id',
];
protected $messages = [
'category_id.required' => 'Please select a category.',
'category_id.exists' => 'The selected category is invalid.',
'user_id.required' => 'A user must be associated with this profile.',
'user_id.exists' => 'The selected user is invalid.',
'name.required' => 'Please provide a name for the profile.',
'name.string' => 'The name must be a valid text.',
'name.max' => 'The name should not exceed 255 characters.',
'description.required' => 'Please provide a description.',
'description.string' => 'The description must be valid text.',
'description.max' => 'The description should not exceed 2000 characters.',
'address.required' => 'Please provide an address.',
'address.string' => 'The address must be valid text.',
'address.max' => 'The address should not exceed 255 characters.',
'profile_photo_url.required' => 'Please upload a profile photo.',
'profile_photo_url.image' => 'The profile photo must be an image file.',
'profile_photo_url.max' => 'The profile photo should not exceed 1MB in size.',
'workshop_photo_urls.*.required' => 'Please upload workshop photos.',
'workshop_photo_urls.*.image' => 'Each workshop photo must be an image file.',
'workshop_photo_urls.*.max' => 'Each workshop photo should not exceed 1MB in size.',
'city_id.required' => 'Please select a city.',
'city_id.exists' => 'The selected city is invalid.',
'country_id.required' => 'Please select a country.',
'country_id.exists' => 'The selected country is invalid.',
'state.required' => 'Please select a state.',
'state.exists' => 'The selected state is invalid.',
];
public function mount()
{
$this->getCountry();
$country = Session::get('country');
$this->latitude = $country['latitude'] ?? '';
$this->longitude = $country['longitude'] ?? '';
$shop = $this->getSellerShop();
if ($shop != null) {
Session::flash('error', 'You already have shop, update it here');
return redirect('/dashboard-my-shop');
}
$this->loadState();
$this->loadCategory();
$user = Session::get('user');
$this->user_id = $user['id'];
}
private function getCountry()
{
$country = Session::get('country');
if ($country && $country['latitude'] === null) {
try {
$clientIp = request()->getClientIp();
$response = Http::get("https://ipinfo.io/{$clientIp}?token=55690b2a8bf492");
if ($response->successful()) {
$data = $response->json();
// Extract location data from ipinfo response
$location = explode(',', $data['loc'] ?? '');
$latitude = $location[0] ?? '';
$longitude = $location[1] ?? '';
// Include city and state in the response
$data = [
'ip' => $ip ?? '',
'latitude' => $latitude ?? '',
'longitude' => $longitude ?? '',
];
Session::put('country', $data);
}
} catch (\Exception $e) {
Log::error('Location fetching error: ' . $e->getMessage());
}
}
}
private function loadState()
{
// Get the country ID from the session
$this->country_id = Session::get('user')['nationality_id'];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$url = ApiEndpoints::getCountryStates() . '?country_id=' . $this->country_id; // Add country_id as a query parameter
$response = Http::withHeaders($headers)->get($url);
$this->states = $response->json()['data'][0]['states'] ?? [];
}
private function loadCategory()
{
$this->categories = Cache::remember('category', now()->addMonths(1), function () {
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)->get(ApiEndpoints::getCategory());
return $response->json()['data'];
});
}
private function getSellerShop()
{
$this->user_id = Session::get('user')['id'];
$body = [
'user_id' => $this->user_id,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->get(ApiEndpoints::shopInfo());
if ($response->successful()) {
$shop = $response->json()['data'];
Session::put('shop', $shop);
$shop = Session::get('shop');
Session::flash('success', 'Welcome back. Add services to attract customers');
return redirect()->to('/dashboard-gig');
} else {
Session::flash('error', 'Setup your shop to continue');
}
}
// Steps navigation
public function nextStep()
{
if ($this->currentStep < 6) {
$this->currentStep++;
}
}
public function prevStep()
{
if ($this->currentStep > 1) {
$this->currentStep--;
}
}
public function updateStepText()
{
$this->currentStep = $this->currentStep; // Or other relevant logic
}
public $state;
public function updatedSelectedState()
{
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$url = ApiEndpoints::getStateCities() . '?state_id=' . $this->selectedState; // Add country_id as a query parameter
$response = Http::withHeaders($headers)->get($url);
if ($response->successful()) {
$this->cities = $response->json()['data'][0]['cities'];
} else {
$this->addError('cities', $response->json()['message']);
}
}
public function addShop()
{
try {
// $this->validate();
if ($this->profile_photo_url) {
$uploadedFile = $this->profile_photo_url->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'sellerProfilePhoto']);
$profile_photo_url = $uploadResult->getSecurePath();
$profile_photo_public_id = $uploadResult->getPublicId();
}
$workshop_photo_urls = [];
$workshop_photo_public_ids = [];
if ($this->workshop_photo_urls) {
foreach ($this->workshop_photo_urls as $photo) {
$uploadedPhoto = $photo->getRealPath();
$uploadResult = cloudinary()->upload($uploadedPhoto, ['folder' => 'workshopPhotos']);
$workshop_photo_urls[] = $uploadResult->getSecurePath();
$workshop_photo_public_ids[] = $uploadResult->getPublicId();
}
}
$body = [
'user_id' => $this->user_id,
'category_id' => $this->category_id,
'profile_photo_url' => $profile_photo_url,
'profile_photo_public_id' => $profile_photo_public_id,
'workshop_photo_urls' => $workshop_photo_urls,
'workshop_photo_public_ids' => $workshop_photo_public_ids,
'name' => ucwords($this->name),
'slug' => Str::slug($this->name),
'description' => ucfirst($this->description),
'address' => $this->address,
'country_id' => $this->country_id,
'state_id' => $this->selectedState,
'latitude' => $this->latitude ?? '192',
'longitude' => $this->longitude ?? '192',
'city_id' => $this->city_id,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->post(ApiEndpoints::saveShop());
if ($response->successful()) {
$shop = $response->json()['data'];
Session::put('shop', $shop);
Session::flash('success', 'Shop Info updated successfully.');
return redirect()->to('/dashboard-gig');
} else {
$this->addError('workshop_photo_urls', $response->json()['data']);
}
} catch (\Throwable $th) {
$this->addError('workshop_photo_urls', $th->getMessage());
}
}
public function render()
{
return view('livewire.shop.add');
}
}