Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eCommerce app integration. #25

Merged
merged 4 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ static/**/*
front/images/catalog
categories.xml
products.xml
pricelist.xlsx
priceru.xml
yandex.yml
Binary file added front/images/btn-close-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added front/images/camera-icon-clr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added front/images/camera-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added front/images/pay/cash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added front/images/pay/cashless.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions front/js/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "airbnb-base",
"env": {
"jquery": true
"globals": {
"$": true
}
}
25 changes: 13 additions & 12 deletions front/js/components/accordion.es6
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const accordion = (() => {

let getSavedItem = () => $('#' + localStorage.getItem(ITEM_KEY));
const getSavedItem = () => $('#' + localStorage.getItem(ITEM_KEY));

const ITEM_KEY = 'activeItem';
const DOM = {
Expand All @@ -9,7 +9,7 @@ const accordion = (() => {
savedItem: getSavedItem(),
};

let init = () => {
const init = () => {
collapseAccordion();
switchItem(DOM.savedItem);
DOM.titles.click((event) => switchItem($(event.target)));
Expand All @@ -21,7 +21,7 @@ const accordion = (() => {
* -- inactive - slide up it, make active
* @param $clickedItem - accordion item as jQuery object
*/
let switchItem = ($clickedItem) => {
const switchItem = ($clickedItem) => {
if (!$clickedItem) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Можно в одну строку, кстати.

return
}
Expand All @@ -35,32 +35,33 @@ const accordion = (() => {
}
};

let openItem = ($clickedItem) => {
let $to_switch = $clickedItem.next();
const openItem = ($clickedItem) => {
let $toSwitch = $clickedItem.next();
collapseAccordion();
$clickedItem.addClass('active');
$to_switch.stop().slideDown();
$toSwitch.stop().slideDown();
};

let collapseItem = ($clickedItem) => {
let $to_switch = $clickedItem.next();
const collapseItem = ($clickedItem) => {
let $toSwitch = $clickedItem.next();
$clickedItem.removeClass('active');
$to_switch.stop().slideUp();
$toSwitch.stop().slideUp();
removeItem();
};

let saveItem = ($item) => {
const saveItem = ($item) => {
localStorage.setItem(ITEM_KEY, $item.attr('id'));
};

let removeItem = ($item) => {
const removeItem = () => {
localStorage.removeItem(ITEM_KEY);
};

let collapseAccordion = () => {
const collapseAccordion = () => {
DOM.titles.removeClass('active');
DOM.panels.stop().slideUp();
};

init();
})();

58 changes: 31 additions & 27 deletions front/js/components/admin.es6
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
const DOM = {
const admin = (() => {
const DOM = {
categoryPage: $('.model-category'),
productPage: $('.model-product'),
};

const CONFIG = {
autoComplete: {
completeURL: '/admin-autocomplete/',
searchFieldId: '#searchbar',
minChars: 3,
},
};
const CONFIG = {
autoComplete: {
completeURL: '/admin-autocomplete/',
searchFieldId: '#searchbar',
minChars: 3,
},
};


let pageType = '';
let pageType = '';

const autoComplete = new autoComplete({
selector: CONFIG.autoComplete.searchFieldId,
minChars: CONFIG.autoComplete.minChars,
source: (term, response) => {
$.getJSON(CONFIG.autoComplete.completeURL, {
q: term,
pageType: getCurrentPageType(),
}, (namesArray) => {
response(namesArray);
});
},
});
const search = new autoComplete({
selector: CONFIG.autoComplete.searchFieldId,
minChars: CONFIG.autoComplete.minChars,
source: (term, response) => {
$.getJSON(CONFIG.autoComplete.completeURL, {
q: term,
pageType: getCurrentPageType(),
}, (namesArray) => {
response(namesArray);
});
},
});

const getCurrentPageType = () => {
if (DOM.productPage.size() > 0) {
pageType = 'product';
} else {
pageType = 'category';
}
const getCurrentPageType = () => {
if (DOM.productPage.size() > 0) {
pageType = 'product';
} else {
pageType = 'category';
}

return pageType;
};
})();

41 changes: 28 additions & 13 deletions front/js/components/category.es6
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Category Page module defines logic, operations and DOM for CategoryPage.
*/
const categoryModule = (function () {
const category = (() => {
const DOM = {
loadedProducts: $('.js-products-showed-count'),
productsList: $('#products-wrapper'),
viewType: $('#category-right'),
loadMore: $('#btn-load-products'),
addToCart: $('.js-product-to-cart'),
tileView: {
$: $('.js-icon-mode-tile'),
mode: 'tile',
Expand All @@ -23,27 +24,28 @@ const categoryModule = (function () {
totalProductsCount: parseInt($('.js-total-products').first().text()),
};

let init = () => {
const init = () => {
setUpListeners();
updateButtonState();
};

/**
* Subscribing on events using mediator.
*/
let setUpListeners = () => {
const setUpListeners = () => {
DOM.loadMore.click(loadProducts);
DOM.sorting.change(changeSort);
DOM.tileView.$.click(() => mediator.publish('onViewTypeChange', DOM.tileView.mode));
DOM.listView.$.click(() => mediator.publish('onViewTypeChange', DOM.listView.mode));
DOM.addToCart.click((event) => buyProduct(event));
mediator.subscribe('onViewTypeChange', updateViewType, sendViewType);
mediator.subscribe('onProductsLoad', updateLoadedCount, updateProductsList, updateButtonState);
};

/**
* Changes sorting option and re-renders the whole screen.
*/
let changeSort = () => location.href = sortingOption().attr('data-path');
const changeSort = () => location.href = sortingOption().attr('data-path');

/**
* Updates Products List DOM via appending html-list of loaded products
Expand All @@ -52,21 +54,21 @@ const categoryModule = (function () {
* @param {Event} event
* @param {string} products - HTML string of fetched product's list
*/
let updateProductsList = (event, products) => DOM.productsList.append(products);
const updateProductsList = (event, products) => DOM.productsList.append(products);

/**
* Updates loaded products counter by a simple logic:
* 1) if we have less products left than we can fetch at a time, it means we have loaded them all,
* so we should set loaded count a value of total products
* 2) otherwise, we simply add PRODUCTS_TO_FETCH to counter.
*/
let updateLoadedCount = () =>
const updateLoadedCount = () =>
DOM.loadedProducts.text(loadedProductsCount() + Math.min(productsLeft(), CONFIG.productsToFetch));

/**
* Adds 'hidden' class to button if there are no more products to load.
*/
let updateButtonState = () => {
const updateButtonState = () => {
if (productsLeft() === 0) {
DOM.loadMore.addClass('hidden');
}
Expand All @@ -79,7 +81,7 @@ const categoryModule = (function () {
* @param {Event} event
* @param {string} viewType: list|tile
*/
let updateViewType = (event, viewType) => {
const updateViewType = (event, viewType) => {
DOM.viewType
.removeClass('view-mode-tile view-mode-list')
.addClass(`view-mode-${viewType}`);
Expand All @@ -96,7 +98,7 @@ const categoryModule = (function () {
/**
* Returns selected sorting option.
*/
let sortingOption = () => DOM.sorting.find(':selected');
const sortingOption = () => DOM.sorting.find(':selected');

/**
* Number of products remained un-fetched from back-end.
Expand All @@ -105,20 +107,20 @@ const categoryModule = (function () {
*
* @returns {Number} - number of products left to fetch
*/
let productsLeft = () => parseInt(CONFIG.totalProductsCount - loadedProductsCount());
const productsLeft = () => parseInt(CONFIG.totalProductsCount - loadedProductsCount());

/**
* Gets number of already loaded products
*
* @returns {int} - number of products which are loaded and presented in DOM
*/
let loadedProductsCount = () => parseInt(DOM.loadedProducts.first().text());
const loadedProductsCount = () => parseInt(DOM.loadedProducts.first().text());

/**
* Loads products from back-end using promise-like fetch object fetchProducts.
* After products successfully loaded - publishes 'onProductLoad' event.
*/
let loadProducts = () => {
const loadProducts = () => {
let categoryUrl = DOM.loadMore.attr('data-url');
let offset = loadedProductsCount();
let sorting = sortingOption().val();
Expand All @@ -128,5 +130,18 @@ const categoryModule = (function () {
.then((products) => mediator.publish('onProductsLoad', products));
};

const buyProduct = (event) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ты слелал ещё один модуль.
Почему просто не поместить код ниже в модуль category?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Не понял.

Copy link
Contributor

Choose a reason for hiding this comment

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

Прошу простить. Посмотрел в исходник. Всё ок.

let buyInfo = (event) => {
let product = $(event.target);
let count = product.closest('.js-order').find('.js-product-count').val();
return {'count': parseInt(count),
'id': parseInt(product.attr('productId'))};
};

let {id, count} = buyInfo(event);
addToCart(id, count).then((data) => mediator.publish('onCartUpdate', data));
};

init();
}());
})();

42 changes: 42 additions & 0 deletions front/js/components/headerCart.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const headerCart = (() => {
const DOM = {
cart: $('.js-cart-header'),
reset: '.js-reset-cart',
removeFromCart: '.js-cart-remove'
};

/**
* Remove product with the given id from cart.
* Trigger 'onCartUpdate' event afterwards.
* @param productId
*/
const remove = (productId) => removeFromCart(productId)
.then((data) => mediator.publish('onCartUpdate', data));

/**
* Remove everything from cart.
* Trigger 'onCartUpdate' event afterwards.
*/
const clear = () => flushCart().then((data) => mediator.publish('onCartUpdate', data));


/**
* Render new cart's html.
* @param event
* @param data
*/
const render = (event, data) => {
DOM.cart.html(data['cart']);
};

const setUpListeners = () => {
// Since product's list in cart dropdown is dynamic, we bind events on static parent
DOM.cart.on('click', DOM.reset, () => clear());
DOM.cart.on('click', DOM.removeFromCart, (event) => remove(event.target.getAttribute('id')));
mediator.subscribe('onCartUpdate', render);
};

const init = () => setUpListeners();
Copy link
Contributor

Choose a reason for hiding this comment

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

Ты опустил объявление init, что-бы линтер не ругался? )
Так просто единообразие нарушается.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Эм.
А const init = () => setUpListeners(); это не объявление init?)

Copy link
Contributor

@YozhEzhi YozhEzhi Jun 16, 2016

Choose a reason for hiding this comment

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

Мы делали вот так:

const init = () => {
 setUpListeners();
};

const setUpListeners = () => {};

init();

В этом файле у тебя:

const setUpListeners = () => {};

const init = () => {
 setUpListeners();
};

init();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

А, ну я совсем на это внимание не обращал.
Хотя более логичен второй вариант.


init();
})();
Loading