Skip to content

Commit

Permalink
fix: Get all session data from local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jansentjeu committed Apr 16, 2024
1 parent b2794b8 commit 01498e9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 57 deletions.
10 changes: 3 additions & 7 deletions src/Plugin/Frontend/Magento/Framework/View/Result/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
namespace Emico\RobinHq\Plugin\Frontend\Magento\Framework\View\Result;

use Magento\Checkout\Helper\Cart;
use Magento\Customer\Model\Session;

class Layout
{
/**
* Layout constructor.
* @param Session $customerSession
* @param Cart $cartHelper
* @param Cart $cartHelper
*/
public function __construct(private Session $customerSession, private Cart $cartHelper)
public function __construct(private Cart $cartHelper)
{
}

Expand All @@ -31,9 +29,7 @@ public function afterAddDefaultHandle(
\Magento\Framework\View\Result\Layout $subject,
mixed $result
) {
if ($this->customerSession->getCustomerId()) {
$result->addHandle('robinhq_customer_logged_in');
}
$result->addHandle('robinhq_customer_logged_in');

if ($this->cartHelper->getItemsCount() !== 0) {
$result->addHandle('robinhq_cart_contents');
Expand Down
28 changes: 0 additions & 28 deletions src/ViewModel/LoggedInCustomer.php

This file was deleted.

6 changes: 1 addition & 5 deletions src/view/frontend/layout/robinhq_customer_logged_in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="emico_robinhq_logged_in" template="Emico_RobinHq::logged_in.phtml">
<arguments>
<argument name="view_model" xsi:type="object">Emico\RobinHq\ViewModel\LoggedInCustomer</argument>
</arguments>
</block>
<block name="emico_robinhq_logged_in" template="Emico_RobinHq::logged_in.phtml"/>
</referenceContainer>
</body>
</page>
45 changes: 28 additions & 17 deletions src/view/frontend/templates/logged_in.phtml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php
/**
* @var $block \Magento\Framework\View\Element\Template
* @var $viewModel \Emico\RobinHq\ViewModel\LoggedInCustomer
*/

$viewModel = $block->getViewModel();
if (!$viewModel->shouldRender()) {
return '';
}
?>
<script type="text/javascript">
if (typeof __robin !== 'undefined') {
if (typeof __robin !== 'undefined' && isLoggedIn()) {
var customer = getCustomer();
var robin_settings = {
callback: function (event, data) {
Expand All @@ -31,22 +25,39 @@ if (!$viewModel->shouldRender()) {
}

/**
* Get the customer from the local storage, because the PHP customer session is depersonalized on cached pages
* @returns {Object}
* Function to check if customer is logged in
* @returns {boolean}
*/
function getCustomer()
{
if (localStorage.getItem('mage-cache-storage') != null) {
var mageCacheStorage = JSON.parse(localStorage.getItem('mage-cache-storage'));
function isLoggedIn() {
var mageCacheStorage = getMageCacheStorage();

if (typeof mageCacheStorage.customer !== 'undefined') {
return mageCacheStorage.customer;
}
return mageCacheStorage != null && typeof mageCacheStorage.customer !== 'undefined';
}

/**
* Get the customer from the local storage, because the PHP customer session is depersonalized on cached pages
* @returns {Object}
*/
function getCustomer() {
var mageCacheStorage = getMageCacheStorage();
if (mageCacheStorage != null && typeof mageCacheStorage.customer !== 'undefined') {
return mageCacheStorage.customer;
}

return {
fullName: 'Unknown',
fullname: 'Unknown',
email: '[email protected]'
};
}

/**
* @returns {Object|null}
*/
function getMageCacheStorage()
{
if (localStorage.getItem('mage-cache-storage') != null) {
return JSON.parse(localStorage.getItem('mage-cache-storage'));
}
return null;
}
</script>

0 comments on commit 01498e9

Please sign in to comment.