File: /var/www/html/app/Livewire/Profile/Show.php
<?php
namespace App\Livewire\Profile;
use Livewire\Component;
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\Cache;
use Illuminate\Support\Facades\Session;
class Show extends Component
{
use WithFileUploads;
public $newPhoto;
public $newPassword;
public $userData;
public $sellerNINData;
public $user_id;
public $email;
public $changeEmail;
public $new_email;
public $phone;
public $profile_photo_url;
public $photo;
public $email_otp;
public $country;
public $city;
public $state;
public $showVerifyOtpForm = false;
public $hideForm = true;
public $verified_email = false;
public $un_verified_email = false;
public function mount()
{
$this->userData = Session::get('user');
$this->country = Session::get('country');
$this->city = $this->country['city_name'] ?? '';
$this->state = $this->country['region_name'] ?? '';
$this->email = $this->userData['email'];
if ($this->userData['email_verified_at'] == null) {
$this->un_verified_email = true;
} else {
$this->verified_email = true;
}
$this->phone = $this->userData['phone'] ?? '';
$this->photo = $this->userData['profile_photo_url'] ?? 'https://fixgini.com/favicon.ico';
if ($this->userData['role'] == 'seller') {
$this->fetchSellerNIN();
}
}
public function toggleEmailEdit()
{
$this->un_verified_email = !$this->un_verified_email;
}
private function fetchSellerNIN()
{
$this->user_id = $this->userData['id'];
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::fetchSellerNIN());
if ($response->successful()) {
$this->sellerNINData = $response->json()['data'];
} else {
Log::error("Failed to fetch seller nin. Status code: {$response->status()}");
$this->addError('orders', 'Failed to fetch seller nin. Please try again later.');
}
} catch (\Throwable $e) {
Log::error($e->getMessage());
$this->addError('orders', 'Failed to fetch seller nin. Please try again later.');
}
}
public function sendEmailOtp()
{
try {
$this->validate([
'email' => 'required|email|valid_email_domain',
]);
$response = Http::post(ApiEndpoints::sendEmailOtp(), [
'email' => $this->email,
]);
if ($response->successful()) {
$info = $response->json()['message'];
Session::flash('success', $info);
//show the input field to enter code
$this->showVerifyOtpForm = true;
$this->un_verified_email = false; // to hide the unverified form
} else {
$info = $response->json()['message'];
$this->addError('email', $info);
Session::flash('error', $info);
}
} catch (\Throwable $th) {
$info = $th->getMessage();
Session::flash('error', $info);
Log::error($th->getMessage());
$this->addError('email', $info);
}
}
public $showPassword = false;
public function verifyEmailOtp()
{
try {
$body = [
'email' => $this->email,
'otp' => $this->email_otp,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->post(ApiEndpoints::changeEmail());
if ($response->successful()) {
$info = $response->json()['data'];
Log::info($info['google_id']);
if (isset($info['google_id']) && !empty($info['google_id'])) {
$this->showVerifyOtpForm = false;
$this->showPassword = true;
} else {
$userSession = Session::get('user', []); // Retrieve the existing session data
$userSession['email'] = $info['email']; // Update the email in the session data
Session::put('user', $userSession); // Save the updated session data
info(Session::get('user'));
Session::get('user');
Session::flash('success', 'Email verified successfully');
$this->redirect('dashboard-profile');
}
} else {
$info = $response->json()['message'];
$this->addError('email_otp', $info);
Session::flash('error', $info);
}
} catch (\Throwable $th) {
$info = $th->getMessage();
Session::flash('error', $info);
Log::error($th->getMessage());
$this->addError('email_otp', $info);
}
}
public function newSetPassword()
{
// dd('hi');
try {
$this->validate([
'newPassword' => 'required|min:8',
]);
$body = [
'newPassword' => $this->newPassword,
];
// dd($body);
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->post(ApiEndpoints::setNewPassword());
if ($response->successful()) {
Session::flash('success', $response->json()['message']);
Session::forget('user');
Session::put('user', $response->json()['data']);
Session::get('user');
$this->redirect('/dashboard-profile');
} else {
$info = $response->json()['message'];
$this->addError('newPassword', $info);
}
} catch (\Throwable $th) {
info($th->getMessage());
$this->addError('newPassword', $th->getMessage());
}
}
public function updatedProfilePhotoUrl()
{
try {
$this->validate([
'profile_photo_url' => 'required|image|max:512',
]);
if ($this->profile_photo_url) {
// Upload the file to Cloudinary
$uploadedFile = $this->profile_photo_url->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'BuyerProfilePhoto']);
$profile_photo_url = $uploadResult->getSecurePath();
$profile_photo_public_id = $uploadResult->getPublicId();
}
$body = [
'user_id' => $this->user_id,
'profile_photo_url' => $profile_photo_url ?? '',
'profile_photo_public_id' => $profile_photo_public_id ?? '',
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->post(ApiEndpoints::updateProfilePhoto());
if ($response->successful()) {
$user = $response->json()['data'];
Session::put('user', $user);
$this->mount();
$this->addError('profile_photo_url', $response->json()['message']);
} else {
$this->addError('profile_photo_url', $response->json()['message']);
}
} catch (\Throwable $th) {
$this->addError('profile_photo_url', $th->getMessage());
}
}
public function render()
{
$sellerData = $this->sellerNINData;
return view('livewire.profile.show', compact('sellerData'));
}
}