Repository URL to install this package:
|
Version:
4.0.116 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const tslib_1 = require("tslib");
const mobx_1 = require("xmobx/mobx");
const persistence_1 = require("@skava/persistence");
const PLP_template_fixtures_1 = require("./PLP_template.fixtures");
const fixture_1 = require("./fixture");
const deps_1 = require("./deps");
class DraggableState {
constructor() {
this.collisionProductId = 'none';
this.originalProducts = PLP_template_fixtures_1.products;
this.reArrangedProducts = [];
this.isDragging = false;
this.isActive = false;
this.setDeletedProducts = item => {
const removedProductsList = persistence_1.oneStorage.has('removedProducts') ? persistence_1.oneStorage.get('removedProducts') : [];
removedProductsList.push(item.id);
persistence_1.oneStorage.set('removedProducts', removedProductsList);
};
this.replaceProductData = (index, product) => {
this.products.splice(index, 1, product);
this.setItem('reArrangedProducts', this.products);
};
this.setUpdatedProductsList = product => {
const modifiedAttributes = {
id: product.id,
variant: product.variant,
images: product.images
};
let modifiedProducts = new Map([]);
let modifiedProductsList = [];
if (persistence_1.oneStorage.has('modifiedProducts')) {
const clonedList = persistence_1.oneStorage.get('modifiedProducts');
clonedList.forEach(value => {
modifiedProducts.set(value.id, value);
});
}
modifiedProducts.set(modifiedAttributes.id, modifiedAttributes);
modifiedProducts.forEach((value, key) => {
modifiedProductsList.push(value);
});
persistence_1.oneStorage.set('modifiedProducts', modifiedProductsList);
};
}
setItem(key, value) {
this[key] = value;
}
get products() {
if (this.reArrangedProducts.length) {
return this.reArrangedProducts;
}
return this.originalProducts;
}
get filterList() {
if (this.reArrangedProducts.length) {
return this.reArrangedProducts;
}
return fixture_1.filterList;
}
onDragOver(value) {
this.setItem('collisionProductId', value);
}
onDragEnd(currentDragId, isFilter) {
const clonedProductList = isFilter ? deps_1.reArrangeProducts(this.collisionProductId, currentDragId, this.filterList) : deps_1.reArrangeProducts(this.collisionProductId, currentDragId, this.products);
this.setItem('collisionProductId', 'none');
this.setItem('reArrangedProducts', clonedProductList);
}
deleteProduct(index) {
const clonedProductList = this.products;
const removedProduct = clonedProductList.splice(index, 1);
this.setDeletedProducts(removedProduct[0]);
this.setItem('reArrangedProducts', clonedProductList);
}
updateProducts(updateParams) {
const {
product,
shouldRemove
} = updateParams;
this.products.forEach((item, index) => {
if (item.id === product.id) {
if (shouldRemove) {
this.deleteProduct(index);
return;
} else {
this.replaceProductData(index, product);
this.setUpdatedProductsList(product);
return;
}
}
});
}
}
tslib_1.__decorate([mobx_1.observable], DraggableState.prototype, "collisionProductId", void 0);
tslib_1.__decorate([mobx_1.observable], DraggableState.prototype, "originalProducts", void 0);
tslib_1.__decorate([mobx_1.observable], DraggableState.prototype, "reArrangedProducts", void 0);
tslib_1.__decorate([mobx_1.observable], DraggableState.prototype, "isDragging", void 0);
tslib_1.__decorate([mobx_1.observable], DraggableState.prototype, "isActive", void 0);
tslib_1.__decorate([mobx_1.action.bound], DraggableState.prototype, "setItem", null);
tslib_1.__decorate([mobx_1.computed], DraggableState.prototype, "products", null);
tslib_1.__decorate([mobx_1.computed], DraggableState.prototype, "filterList", null);
tslib_1.__decorate([mobx_1.action.bound], DraggableState.prototype, "onDragOver", null);
tslib_1.__decorate([mobx_1.action.bound], DraggableState.prototype, "onDragEnd", null);
tslib_1.__decorate([mobx_1.action], DraggableState.prototype, "setDeletedProducts", void 0);
tslib_1.__decorate([mobx_1.action.bound], DraggableState.prototype, "deleteProduct", null);
tslib_1.__decorate([mobx_1.action], DraggableState.prototype, "replaceProductData", void 0);
tslib_1.__decorate([mobx_1.action], DraggableState.prototype, "setUpdatedProductsList", void 0);
tslib_1.__decorate([mobx_1.action.bound], DraggableState.prototype, "updateProducts", null);
exports.DraggableState = DraggableState; //# sourceMappingURL=state.js.map