Refactor for livewire components

This commit is contained in:
David Bomba 2024-11-14 11:02:43 +11:00
parent e01e0f92e4
commit 86ed8044c3
11 changed files with 55 additions and 216 deletions

View File

@ -1,26 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\EInvoice;
use App\Http\Controllers\Controller;
use App\Http\Requests\EInvoice\SignupRequest;
class SelfhostController extends Controller
{
public function index(SignupRequest $request)
{
return view('einvoice.index');
}
}

View File

@ -1,142 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Livewire\EInvoice;
use Livewire\Component;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
class Portal extends Component
{
public $email = '';
public $password = '';
public array $companies;
private string $api_url = '';
public function mount()
{
$this->api_url = config('ninja.hosted_ninja_url');
$this->getCompanies();
}
private function getCompanies(): self
{
$this->companies = auth()->guard('user')->check() ? auth()->guard('user')->user()->account->companies->map(function ($company) {
return [
'key' => $company->company_key,
'city' => $company->settings->city,
'country' => $company->country()->iso_3166_2,
'county' => $company->settings->state,
'line1' => $company->settings->address1,
'line2' => $company->settings->address2,
'party_name' => $company->settings->name,
'vat_number' => $company->settings->vat_number,
'zip' => $company->settings->postal_code,
'legal_entity_id' => $company->legal_entity_id,
'tax_registered' => (bool) strlen($company->settings->vat_number ?? '') > 2,
'tenant_id' => $company->company_key,
'classification' => strlen($company->settings->classification ?? '') > 2 ? $company->settings->classification : 'business',
];
})->toArray() : [];
return $this;
}
public function login()
{
$credentials = ['email' => $this->email, 'password' => $this->password];
if (Auth::attempt($credentials)) {
session()->flash('message', 'Logged in successfully.');
App::setLocale(auth()->guard('user')->user()->account->companies->first()->getLocale());
$this->getCOmpanies();
} else {
session()->flash('error', 'Invalid credentials.');
}
}
public function logout()
{
Auth::logout();
session()->flash('message', 'Logged out!');
}
public function register(string $company_key)
{
$register_company = [
'acts_as_receiver' => true,
'acts_as_sender' => true,
'advertisements' => ['invoice']
];
foreach($this->companies as $company)
{
if($company['key'] == $company_key)
$register_company = array_merge($company, $register_company);
}
$r = Http::withHeaders($this->getHeaders())
->post("{$this->api_url}/api/einvoice/createLegalEntity", $register_company);
if($r->successful())
{
nlog($r->body());
$response = $r->json();
$_company = auth()->guard('user')->user()->account->companies()->where('company_key', $company_key)->first();
$_company->legal_entity_id = $response['id'];
$_company->save();
$this->getCompanies();
return;
}
if($r->failed())
nlog($r->getBody()->getContents());
$error = json_decode($r->getBody()->getContents(),true);
session()->flash('error', $error['message']);
}
private function getHeaders()
{
return [
'X-API-SELF-HOST-TOKEN' => config('ninja.license_key'),
"X-Requested-With" => "XMLHttpRequest",
"Content-Type" => "application/json",
];
}
public function render()
{
return view('livewire.e-invoice.portal');
}
}

View File

@ -12,39 +12,43 @@
namespace App\Livewire\PaymentMethods; namespace App\Livewire\PaymentMethods;
use App\Libraries\MultiDB;
use Livewire\Component; use Livewire\Component;
use App\Libraries\MultiDB;
use Livewire\Attributes\Computed;
use App\Models\ClientGatewayToken;
class UpdateDefaultMethod extends Component class UpdateDefaultMethod extends Component
{ {
/** @var \App\Models\Company */ public $db;
public $company;
/** @var \App\Models\ClientGatewayToken */ public $token_id;
public $token;
/** @var \App\Models\Client */
public $client;
public function mount() public function mount()
{ {
$this->company = $this->client->company; MultiDB::setDb($this->db);
}
MultiDB::setDb($this->company->db); #[Computed]
public function token()
// $this->is_disabled = $this->token->is_default; {
return ClientGatewayToken::withTrashed()->find($this->token_id);
} }
public function makeDefault(): void public function makeDefault(): void
{ {
if ($this->token->is_default) {
MultiDB::setDb($this->db);
if ($this->token()->is_default) {
return; return;
} }
$this->client->gateway_tokens()->update(['is_default' => 0]); $this->token()->client->gateway_tokens()->update(['is_default' => 0]);
$this->token->is_default = 1; $token = $this->token();
$this->token->save(); $token->is_default = 1;
$token->save();
$this->dispatch('UpdateDefaultMethod::method-updated'); $this->dispatch('UpdateDefaultMethod::method-updated');
} }

View File

@ -12,13 +12,12 @@
namespace App\Livewire\Profile\Settings; namespace App\Livewire\Profile\Settings;
use Illuminate\Support\Facades\Hash;
use Livewire\Component; use Livewire\Component;
use Livewire\Attributes\Computed;
use Illuminate\Support\Facades\Hash;
class General extends Component class General extends Component
{ {
public $profile;
public $first_name; public $first_name;
public $last_name; public $last_name;
@ -40,12 +39,18 @@ class General extends Component
'phone' => ['sometimes'], 'phone' => ['sometimes'],
]; ];
#[Computed]
public function profile()
{
return auth()->guard('contact')->user();
}
public function mount() public function mount()
{ {
$profile = auth()->guard('contact')->user(); $profile = $this->profile();
$this->fill([ $this->fill([
'profile' => $profile, // 'profile' => $profile,
'first_name' => $profile->first_name, 'first_name' => $profile->first_name,
'last_name' => $profile->last_name, 'last_name' => $profile->last_name,
'email' => $profile->email, 'email' => $profile->email,
@ -61,7 +66,10 @@ class General extends Component
public function submit() public function submit()
{ {
if ($this->profile->email != $this->email) {
$profile = $this->profile();
if ($profile->email != $this->email) {
$this->rules['email'][] = 'unique:client_contacts,email'; $this->rules['email'][] = 'unique:client_contacts,email';
} }
@ -72,10 +80,10 @@ class General extends Component
$data = $this->validate($this->rules); $data = $this->validate($this->rules);
if (! empty($this->password)) { if (! empty($this->password)) {
$this->profile->password = Hash::make($this->password); $profile->password = Hash::make($this->password);
} }
$this->profile $profile
->fill($data) ->fill($data)
->save(); ->save();

View File

@ -6,8 +6,6 @@ use Livewire\Component;
class NameWebsiteLogo extends Component class NameWebsiteLogo extends Component
{ {
public $profile;
public $name; public $name;
public $vat_number; public $vat_number;
@ -28,7 +26,7 @@ class NameWebsiteLogo extends Component
public function mount() public function mount()
{ {
$this->fill([ $this->fill([
'profile' => auth()->guard('contact')->user()->client, // 'profile' => auth()->guard('contact')->user()->client,
'name' => auth()->guard('contact')->user()->client->present()->name(), 'name' => auth()->guard('contact')->user()->client->present()->name(),
'vat_number' => auth()->guard('contact')->user()->client->vat_number ?: '', 'vat_number' => auth()->guard('contact')->user()->client->vat_number ?: '',
'website' => auth()->guard('contact')->user()->client->website, 'website' => auth()->guard('contact')->user()->client->website,
@ -46,12 +44,14 @@ class NameWebsiteLogo extends Component
{ {
$data = $this->validate($this->rules); $data = $this->validate($this->rules);
$this->profile->name = $data['name']; $profile = auth()->guard('contact')->user()->client;
$this->profile->vat_number = $data['vat_number'];
$this->profile->website = $data['website'];
$this->profile->phone = $data['phone'];
$this->profile->save(); $profile->name = $data['name'];
$profile->vat_number = $data['vat_number'];
$profile->website = $data['website'];
$profile->phone = $data['phone'];
$profile->save();
$this->saved = ctrans('texts.saved_at', ['time' => now()->toTimeString()]); $this->saved = ctrans('texts.saved_at', ['time' => now()->toTimeString()]);
} }

View File

@ -6,7 +6,6 @@ use Livewire\Component;
class PersonalAddress extends Component class PersonalAddress extends Component
{ {
public $profile;
public $address1; public $address1;
@ -34,7 +33,7 @@ class PersonalAddress extends Component
public function mount() public function mount()
{ {
$this->fill([ $this->fill([
'profile' => auth()->guard('contact')->user()->client, // 'profile' => auth()->guard('contact')->user()->client,
'address1' => auth()->guard('contact')->user()->client->address1, 'address1' => auth()->guard('contact')->user()->client->address1,
'address2' => auth()->guard('contact')->user()->client->address2, 'address2' => auth()->guard('contact')->user()->client->address2,
'city' => auth()->guard('contact')->user()->client->city, 'city' => auth()->guard('contact')->user()->client->city,
@ -58,7 +57,9 @@ class PersonalAddress extends Component
$data['country_id'] = null; $data['country_id'] = null;
} }
$this->profile $profile = auth()->guard('contact')->user()->client;
$profile
->fill($data) ->fill($data)
->save(); ->save();

View File

@ -6,7 +6,6 @@ use Livewire\Component;
class ShippingAddress extends Component class ShippingAddress extends Component
{ {
public $profile;
public $shipping_address1; public $shipping_address1;
@ -34,7 +33,7 @@ class ShippingAddress extends Component
public function mount() public function mount()
{ {
$this->fill([ $this->fill([
'profile' => auth()->guard('contact')->user()->client, // 'profile' => auth()->guard('contact')->user()->client,
'shipping_address1' => auth()->guard('contact')->user()->client->shipping_address1, 'shipping_address1' => auth()->guard('contact')->user()->client->shipping_address1,
'shipping_address2' => auth()->guard('contact')->user()->client->shipping_address2, 'shipping_address2' => auth()->guard('contact')->user()->client->shipping_address2,
'shipping_city' => auth()->guard('contact')->user()->client->shipping_city, 'shipping_city' => auth()->guard('contact')->user()->client->shipping_city,
@ -58,7 +57,9 @@ class ShippingAddress extends Component
$data['shipping_country_id'] = null; $data['shipping_country_id'] = null;
} }
$this->profile $profile = auth()->guard('contact')->user()->client;
$profile
->fill($data) ->fill($data)
->save(); ->save();

View File

@ -1,5 +0,0 @@
@extends('layouts.ninja')
@section('body')
@livewire('e-invoice.portal')
@stop

View File

@ -9,13 +9,13 @@
<span class="text-primary mr-1 hidden" data-ref="success-label">{{ ctrans('texts.success') }}!</span> <span class="text-primary mr-1 hidden" data-ref="success-label">{{ ctrans('texts.success') }}!</span>
<p> <p>
{{ $token->is_default ? ctrans('texts.already_default_payment_method') : ctrans('texts.default_payment_method') }} {{ $this->token->is_default ? ctrans('texts.already_default_payment_method') : ctrans('texts.default_payment_method') }}
</p> </p>
</div> </div>
</div> </div>
<div class="mt-5 sm:mt-0 sm:ml-6 sm:flex-shrink-0 sm:flex sm:items-center"> <div class="mt-5 sm:mt-0 sm:ml-6 sm:flex-shrink-0 sm:flex sm:items-center">
<form wire:submit="makeDefault"> <form wire:submit="makeDefault">
<button class="button button-primary bg-primary" {{ $token->is_default ? 'disabled' : '' }}> <button class="button button-primary bg-primary" {{ $this->token->is_default ? 'disabled' : '' }}>
{{ ctrans('texts.save_as_default') }} {{ ctrans('texts.save_as_default') }}
</button> </button>
</form> </form>

View File

@ -116,7 +116,7 @@
</div> </div>
@endif @endif
@livewire('payment-methods.update-default-method', ['token' => $payment_method, 'client' => $client]) @livewire('payment-methods.update-default-method', ['token_id' => $payment_method->id, 'db' => $client->company->db])
<div class="mt-4 mb-4 bg-white shadow sm:rounded-lg"> <div class="mt-4 mb-4 bg-white shadow sm:rounded-lg">
<div class="px-4 py-5 sm:p-6"> <div class="px-4 py-5 sm:p-6">

View File

@ -57,6 +57,4 @@ Route::get('gocardless/oauth/connect/confirm', [GoCardlessOAuthController::class
Route::post('gocardless/oauth/connect/webhook', GoCardlessOAuthWebhookController::class)->name('gocardless.oauth.webhook'); Route::post('gocardless/oauth/connect/webhook', GoCardlessOAuthWebhookController::class)->name('gocardless.oauth.webhook');
Route::get('gocardless/oauth/connect/{token}', [GoCardlessOAuthController::class, 'connect']); Route::get('gocardless/oauth/connect/{token}', [GoCardlessOAuthController::class, 'connect']);
Route::get('einvoice/beta', [SelfhostController::class, 'index'])->name('einvoice.beta');
\Illuminate\Support\Facades\Broadcast::routes(['middleware' => ['token_auth']]); \Illuminate\Support\Facades\Broadcast::routes(['middleware' => ['token_auth']]);