Skip to content

Commit

Permalink
#794 lazy cart (#797)
Browse files Browse the repository at this point in the history
* Remove skip for test

* Load cart lazily

* Apply linter rules
  • Loading branch information
ArtemijRodionov authored Mar 29, 2019
1 parent c6ede7f commit 7dbf2c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
49 changes: 43 additions & 6 deletions front/js/components/headerCart.es6
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,57 @@
removeFromCart: '.js-cart-remove',
};

const config = {
storageKey: 'rendered-cart',
};

const init = () => {
// @todo #789:60m Load cart lazily.
// See the parent task for details.
loadCart();
setUpListeners();
};

function setUpListeners() {
mediator.subscribe('onCartUpdate', render, configs.initScrollbar, showCart);
mediator.subscribe(
'onCartUpdate',
(_, data) => {
render(data.header);
saveCart(data.header);
},
configs.initScrollbar,
showCart,
);

// Since product's list in Cart dropdown is dynamic, we bind events on static parent
DOM.$cart.on('click', DOM.resetCart, clear);
DOM.$cart.on('click', DOM.removeFromCart, remove);
}

/**
* Load cart lazily to prevent caching of its content
* and reach ability to store shared cache for pages.
*/
function loadCart() {
const renderedCart = localStorage.getItem(config.storageKey);
const renderCart = (html) => {
render(html);
configs.initScrollbar();
};

if (renderedCart) {
renderCart(renderedCart);
} else {
server.getCart()
.then((data) => {
renderCart(data.header);
saveCart(data.header);
});
}
}

function saveCart(state) {
localStorage.setItem(config.storageKey, state);
}

/**
* Remove product with the given id from cart.
*/
Expand Down Expand Up @@ -70,10 +107,10 @@

/**
* Render new Cart's html.
* @param data
* @param html
*/
function render(_, data) {
DOM.$cart.html(data.header);
function render(html) {
DOM.$cart.html(html);
}

init();
Expand Down
9 changes: 1 addition & 8 deletions shopelectro/tests/tests_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,8 @@ def test_load_more_after_filtering(self):
self.assertEqual(new_product_cards, 96)

@override_settings(DEBUG=True)
@unittest.expectedFailure
def test_cached_cart(self):
"""
The cart state shouldn't be cached on a category page.
@todo #783:60m Load the cart lazily.
It will solve the issue with invalid cart data and
bring ability to create cache shared among users.
"""
"""The cart state shouldn't be cached on a category page."""
def add_products_from(url):
self.browser.get(url)
self.wait_page_loading()
Expand Down
2 changes: 1 addition & 1 deletion templates/layout/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
</div>

<div class="col-md-3 basket-parent js-cart-header">
{% include 'ecommerce/header_cart.html' %}
<!-- Cart loads lazily -->
</div>
</div>
</div>
Expand Down

2 comments on commit 7dbf2c1

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7dbf2c1 Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 783-dbb67fb4 disappeared from shopelectro/tests/tests_selenium.py, that's why I closed #789. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7dbf2c1 Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 789-a1cd638b disappeared from front/js/components/headerCart.es6, that's why I closed #794. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.