From 0a2bb8e125412992aebf70cdbbf7a64131ec8177 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 Nov 2025 13:05:22 +1100 Subject: [PATCH] Refactor for invoices summary --- app/Livewire/Flow2/InvoicePay.php | 2 + app/Livewire/Flow2/InvoiceSummary.php | 58 +++++++++++-------- app/Utils/Traits/WithSecureContext.php | 13 +---- .../flow2/invoices-summary.blade.php | 19 ++++++ 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/app/Livewire/Flow2/InvoicePay.php b/app/Livewire/Flow2/InvoicePay.php index 47309454b2..7ac9a0fcc1 100644 --- a/app/Livewire/Flow2/InvoicePay.php +++ b/app/Livewire/Flow2/InvoicePay.php @@ -294,6 +294,8 @@ class InvoicePay extends Component 'payable_invoices' => $payable_invoices, ]); + $this->dispatch(self::CONTEXT_READY); + } public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View diff --git a/app/Livewire/Flow2/InvoiceSummary.php b/app/Livewire/Flow2/InvoiceSummary.php index ae29dfe0d1..bfccf07935 100644 --- a/app/Livewire/Flow2/InvoiceSummary.php +++ b/app/Livewire/Flow2/InvoiceSummary.php @@ -28,42 +28,49 @@ class InvoiceSummary extends Component public $gateway_fee; + public $isReady = false; + + #[On(self::CONTEXT_READY)] + public function onContextReady(): void + { + $this->isReady = true; + $this->loadContextData(); + } + public function mount() { - $_context = $this->getContext(); - $contact = $_context['contact'] ?? auth()->guard('contact')->user(); - $this->invoices = $_context['payable_invoices']; - $this->amount = Number::formatMoney($_context['amount'], $contact->client); - $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; + if (!empty($_context)) { + $this->isReady = true; + $this->loadContextData(); + } + } + private function loadContextData(): void + { + $_context = $this->getContext(); + + if (empty($_context)) { + return; + } + + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); + $this->invoices = $_context['payable_invoices'] ?? []; + $this->amount = isset($_context['amount']) ? Number::formatMoney($_context['amount'], $contact->client) : ''; + $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; } #[On(self::CONTEXT_UPDATE)] public function onContextUpdate(): void { - - $_context = $this->getContext(); - - // refactor logic for updating the price for eg if it changes with under/over pay - $contact = $_context['contact'] ?? auth()->guard('contact')->user(); - $this->invoices = $_context['payable_invoices']; - $this->amount = Number::formatMoney($_context['amount'], $contact->client); - $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; - + $this->loadContextData(); } #[On('payment-view-rendered')] - public function handlePaymentViewRendered() + public function handlePaymentViewRendered(): void { - - $_context = $this->getContext(); - - $contact = $_context['contact'] ?? auth()->guard('contact')->user(); - $this->amount = Number::formatMoney($_context['amount'], $contact->client); - $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; - + $this->loadContextData(); } public function downloadDocument($invoice_hashed_id) @@ -91,10 +98,13 @@ class InvoiceSummary extends Component public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View { - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); + $_context = $this->getContext(); + + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); return render('flow2.invoices-summary', [ - 'client' => $contact->client, + 'client' => $contact->client ?? null, + 'isReady' => $this->isReady, ]); } diff --git a/app/Utils/Traits/WithSecureContext.php b/app/Utils/Traits/WithSecureContext.php index 7e391331d3..784367732e 100644 --- a/app/Utils/Traits/WithSecureContext.php +++ b/app/Utils/Traits/WithSecureContext.php @@ -17,6 +17,7 @@ use Illuminate\Support\Str; trait WithSecureContext { public const CONTEXT_UPDATE = 'secureContext.updated'; + public const CONTEXT_READY = 'flow2.context.ready'; /** * @throws \Psr\Container\ContainerExceptionInterface @@ -24,19 +25,9 @@ trait WithSecureContext */ public function getContext(): mixed { - - $context = \Illuminate\Support\Facades\Cache::get(session()->getId()) ?? false; - - if (!$context) { - - usleep(300000); //@monitor - inject delay to catch delays in cache updating - - $context = \Illuminate\Support\Facades\Cache::get(session()->getId()) ?? []; - - } + $context = \Illuminate\Support\Facades\Cache::get(session()->getId()) ?? []; return $context; - } public function setContext(string $property, $value): array diff --git a/resources/views/portal/ninja2020/flow2/invoices-summary.blade.php b/resources/views/portal/ninja2020/flow2/invoices-summary.blade.php index 0cb2776166..af34220588 100644 --- a/resources/views/portal/ninja2020/flow2/invoices-summary.blade.php +++ b/resources/views/portal/ninja2020/flow2/invoices-summary.blade.php @@ -1,4 +1,22 @@
+ @if(!$isReady) +
+
+
+

{{ ctrans('texts.invoices') }}

+
+
+
+
+ + + + +

{{ ctrans('texts.loading') }}

+
+
+
+ @else
@@ -105,4 +123,5 @@
+ @endif
\ No newline at end of file