mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
fixed event
This commit is contained in:
parent
cea0057f06
commit
844b4e4dd0
869
dist/public/css/style.css
vendored
869
dist/public/css/style.css
vendored
File diff suppressed because it is too large
Load Diff
11
dist/public/js/admin.js
vendored
11
dist/public/js/admin.js
vendored
File diff suppressed because one or more lines are too long
11
dist/public/js/main.js
vendored
11
dist/public/js/main.js
vendored
File diff suppressed because one or more lines are too long
8571
package-lock.json
generated
Normal file
8571
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,120 +1,119 @@
|
|||||||
import { Component } from "../core/component";
|
import { Component } from '../core/component'
|
||||||
import { apiService } from "../services/api.service";
|
import { apiService } from '../services/api.service'
|
||||||
import { insertCount } from "../other/counter";
|
import { insertCount } from '../other/counter'
|
||||||
import { renderItems } from "../templates/items.template";
|
import { renderItems } from '../templates/items.template'
|
||||||
|
|
||||||
export class ItemsComponent extends Component {
|
export class ItemsComponent extends Component {
|
||||||
constructor(id, {loader}, {notification}) {
|
constructor(id, { loader }, { notification }) {
|
||||||
super(id);
|
super(id)
|
||||||
this.loader = loader;
|
this.loader = loader
|
||||||
this.notification = notification;
|
this.notification = notification
|
||||||
this.$el ? this.roster = this.$el.querySelector('#roster') : false;
|
this.$el ? (this.roster = this.$el.querySelector('#roster')) : false
|
||||||
if (localStorage.getItem('cart')) {
|
if (localStorage.getItem('cart')) {
|
||||||
this.cart = JSON.parse(localStorage.getItem('cart'));
|
this.cart = JSON.parse(localStorage.getItem('cart'))
|
||||||
} else {
|
} else {
|
||||||
this.cart = [];
|
this.cart = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
if (this.$el) {
|
||||||
|
localStorage.setItem('sortByAll', 'true') // установка значения для компонента Filter
|
||||||
|
this.$el.addEventListener('click', addItem.bind(this))
|
||||||
|
if (!localStorage.getItem('category')) {
|
||||||
|
// Получаем все товары и выводим их если нету ключа category в ls
|
||||||
|
const fData = await apiService.getItems()
|
||||||
|
const categories = await apiService.getCategories()
|
||||||
|
const html = renderItems(categories, fData)
|
||||||
|
this.insertItems(html)
|
||||||
|
this.btnColor()
|
||||||
|
this.loader.hide()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onHide() {
|
||||||
|
this.$el.querySelector('#roster').innerHTML = ''
|
||||||
|
this.loader.hide()
|
||||||
|
}
|
||||||
|
|
||||||
|
onShow() {
|
||||||
|
this.$el.querySelector('#roster').innerHTML = ''
|
||||||
|
this.loader.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
btnColor() {
|
||||||
|
if (localStorage.getItem('cart')) {
|
||||||
|
// Окраска кнопок айтемов которые находятся в корзине
|
||||||
|
const obj = JSON.parse(localStorage.getItem('cart'))
|
||||||
|
|
||||||
|
obj.forEach((item) => {
|
||||||
|
if (
|
||||||
|
this.$el.querySelector(
|
||||||
|
`#rosterButtons[data-id="${item.id}"] #addItem`
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
this.$el
|
||||||
|
.querySelector(`#rosterButtons[data-id="${item.id}"] #addItem`)
|
||||||
|
.classList.add('orange')
|
||||||
|
this.$el.querySelector(
|
||||||
|
`#rosterButtons[data-id="${item.id}"] #addItem i`
|
||||||
|
).innerHTML = 'close'
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async init() {
|
insertItems(html) {
|
||||||
if (this.$el) {
|
this.roster.insertAdjacentHTML('afterbegin', html)
|
||||||
localStorage.setItem('sortByAll', 'true'); // установка значения для компонента Filter
|
this.btnColor()
|
||||||
this.$el.addEventListener('click', addItem.bind(this));
|
}
|
||||||
if (!localStorage.getItem('category')) { // Получаем все товары и выводим их если нету ключа category в ls
|
|
||||||
const fData = await apiService.getItems();
|
|
||||||
const categories = await apiService.getCategories();
|
|
||||||
const html = renderItems(categories, fData);
|
|
||||||
this.insertItems(html);
|
|
||||||
this.btnColor();
|
|
||||||
this.loader.hide();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onHide() {
|
|
||||||
this.$el.querySelector('#roster').innerHTML = '';
|
|
||||||
this.loader.hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
onShow() {
|
|
||||||
this.$el.querySelector('#roster').innerHTML = '';
|
|
||||||
this.loader.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
btnColor() {
|
|
||||||
if (localStorage.getItem('cart')) {
|
|
||||||
// Окраска кнопок айтемов которые находятся в корзине
|
|
||||||
const obj = JSON.parse(localStorage.getItem('cart'));
|
|
||||||
|
|
||||||
obj.forEach(item => {
|
|
||||||
if (this.$el.querySelector(`#rosterButtons[data-id="${item.id}"] #addItem`)) {
|
|
||||||
this.$el.querySelector(`#rosterButtons[data-id="${item.id}"] #addItem`).classList.add('orange');
|
|
||||||
this.$el.querySelector(`#rosterButtons[data-id="${item.id}"] #addItem i`).innerHTML = 'close';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
insertItems(html) {
|
|
||||||
this.roster.insertAdjacentHTML('afterbegin', html);
|
|
||||||
this.btnColor();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function addItem(e) {
|
function addItem(e) {
|
||||||
|
if (e.target.localName === 'button' && e.target.id === 'addItem') {
|
||||||
if (e.target.localName === 'button' && e.target.id === 'addItem') {
|
const target = e.target
|
||||||
const target = e.target;
|
const item = {
|
||||||
const item = {
|
id: e.target.parentNode.dataset.id,
|
||||||
id: e.target.parentNode.dataset.id,
|
title: e.target.parentNode.dataset.title,
|
||||||
title: e.target.parentNode.dataset.title,
|
count: e.target.parentNode.dataset.count,
|
||||||
count: e.target.parentNode.dataset.count,
|
price: e.target.parentNode.dataset.price,
|
||||||
price: e.target.parentNode.dataset.price
|
|
||||||
}
|
|
||||||
renderObj(item, this, target);
|
|
||||||
} else if (e.target.localName === 'i') {
|
|
||||||
const target = e.target.parentNode;
|
|
||||||
const item = {
|
|
||||||
id: e.target.parentNode.parentNode.dataset.id,
|
|
||||||
title: e.target.parentNode.parentNode.dataset.title,
|
|
||||||
count: e.target.parentNode.parentNode.dataset.count,
|
|
||||||
price: e.target.parentNode.parentNode.dataset.price
|
|
||||||
}
|
|
||||||
renderObj(item, this, target);
|
|
||||||
}
|
}
|
||||||
|
renderObj(item, this, target)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderObj(obj, context, target) {
|
function renderObj(obj, context, target) {
|
||||||
if (context.cart.length > 0) {
|
if (context.cart.length > 0) {
|
||||||
let insterted = false;
|
let insterted = false
|
||||||
let index;
|
let index
|
||||||
// Проверка на существующий айтем в корзине
|
// Проверка на существующий айтем в корзине
|
||||||
context.cart.forEach((item, i) => {
|
context.cart.forEach((item, i) => {
|
||||||
if (item.id === obj.id) {
|
if (item.id === obj.id) {
|
||||||
insterted = true;
|
insterted = true
|
||||||
index = i;
|
index = i
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
if (insterted === false) {
|
if (insterted === false) {
|
||||||
context.cart.push(obj)
|
context.cart.push(obj)
|
||||||
localStorage.setItem('cart', JSON.stringify(context.cart));
|
localStorage.setItem('cart', JSON.stringify(context.cart))
|
||||||
target.classList.add('orange');
|
target.classList.add('orange')
|
||||||
target.querySelector('i').innerHTML = 'close';
|
target.querySelector('i').innerHTML = 'close'
|
||||||
insertCount('counter', context.cart);
|
insertCount('counter', context.cart)
|
||||||
window.M.toast({html: 'Товар был успешно добавлен в корзину.'})
|
window.M.toast({ html: 'Товар был успешно добавлен в корзину.' })
|
||||||
} else {
|
|
||||||
context.cart.splice(index, 1);
|
|
||||||
localStorage.setItem('cart', JSON.stringify(context.cart));
|
|
||||||
target.classList.remove('orange');
|
|
||||||
target.querySelector('i').innerHTML = 'shopping_cart';
|
|
||||||
insertCount('counter', context.cart);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
context.cart.push(obj);
|
context.cart.splice(index, 1)
|
||||||
localStorage.setItem('cart', JSON.stringify(context.cart));
|
localStorage.setItem('cart', JSON.stringify(context.cart))
|
||||||
target.classList.add('orange');
|
target.classList.remove('orange')
|
||||||
target.querySelector('i').innerHTML = 'close';
|
target.querySelector('i').innerHTML = 'shopping_cart'
|
||||||
insertCount('counter', context.cart);
|
insertCount('counter', context.cart)
|
||||||
window.M.toast({html: 'Товар был успешно добавлен в корзину.'})
|
|
||||||
}
|
}
|
||||||
};
|
} else {
|
||||||
|
context.cart.push(obj)
|
||||||
|
localStorage.setItem('cart', JSON.stringify(context.cart))
|
||||||
|
target.classList.add('orange')
|
||||||
|
target.querySelector('i').innerHTML = 'close'
|
||||||
|
insertCount('counter', context.cart)
|
||||||
|
window.M.toast({ html: 'Товар был успешно добавлен в корзину.' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user