Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Loop from '/DateTime/Timer/Loop';
- import Serialize from '/Support/Serialize';
- import Collection from '/Support/Collection';
- import Repository from '/Storage/Repository';
- import CartItemObject from '/App/Cart/CartItemObject';
- import PeriodicTimer from '/DateTime/Timer/PeriodicTimer';
- import {default as AsyncRepository} from '/ActiveRecord/Repository';
- import Serializable, {toObject} from '/Support/Interfaces/Serializable';
- import LocalStorageAdapter from '/Storage/Adapters/LocalStorageAdapter';
- /**
- * Cart instance
- * @TODO Refactor me
- */
- export default class Cart extends Serializable {
- /**
- * @type {Repository}
- * @private
- */
- _repository = new Repository(new LocalStorageAdapter('cart:'));
- /**
- * @type {KnockoutObservableArray<T>}
- * @private
- */
- _items = ko.observableArray([]);
- /**
- * @constructor
- */
- constructor() {
- super();
- this.refresh();
- var loop = new Loop;
- loop
- .subscribe((timer:PeriodicTimer) => {
- this.refresh();
- })
- .everySecond()
- .withoutOverlapping();
- loop.start();
- }
- /**
- * @returns {KnockoutObservableArray.<T>}
- */
- get items() {
- return this._items;
- }
- /**
- * TODO optimize items diff
- * @returns {Cart}
- */
- refresh() {
- var all = new Collection(this.all());
- var rendered = new Collection(this._items());
- // Add from rendered
- all.each(item => {
- var exists = rendered.find(i => i.buyId === item.buyId).first();
- if (!exists) {
- this._items.push(item);
- }
- });
- // Remove from rendered
- rendered.each(item => {
- var exists = all.find(i => i.buyId === item.buyId).first();
- if (!exists) {
- this._items.remove(i => i.buyId === item.buyId);
- }
- });
- return this;
- }
- /**
- * @param {string} uuid
- * @returns {boolean}
- */
- contains(uuid:string):boolean {
- return !!this.get(uuid);
- }
- /**
- * @param {string} uuid
- * @returns {boolean}
- */
- remove(uuid:string):boolean {
- var item = this.get(uuid);
- if (item) {
- this._repository.remove(item.buyId);
- this.refresh();
- return true;
- }
- return false;
- }
- /**
- * @param {string} uuid
- * @returns {CartItemObject}
- */
- get(uuid:string):CartItemObject {
- return (new Collection(this.all()))
- .where('uuid', uuid)
- .first();
- }
- /**
- * @param {object} cartObject
- */
- add(cartObject = {}):Cart {
- if (!cartObject[toObject]) {
- throw new Error('Can not add object to cart');
- }
- var data = cartObject[toObject]();
- var item = new CartItemObject(data);
- this._repository.set(item.buyId, item[toObject]());
- this.refresh();
- return this;
- }
- /**
- * @returns {Array}
- */
- all():Array {
- var items = [];
- var data = this._repository.all();
- Object.keys(data).forEach(key => {
- items.push(CartItemObject.createFromStorage(data[key], key));
- });
- return items;
- }
- /**
- * @param phone
- * @param name
- * @param address
- * @returns {RepositoryResponse}
- */
- async confirm(phone, name, address) {
- return await CartRepository.create().saveData({
- phone: phone,
- name: name,
- address: address,
- cart: Object.values(Serialize.toObject(this))
- });
- }
- /**
- * @returns {{}}
- */
- [toObject]() {
- return this._repository.all();
- }
- }
- export class CartRepository extends AsyncRepository {
- static create() {
- return this.cached(() => {
- return new this(Cart, {
- update: {url: '/cart/confirm.json', method: 'put'}
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement