Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/packages / features / Lists / state / saveForLater / SaveForLaterContainer.js
Size: Mime:
import * as tslib_1 from "tslib";
/**
 * @todo change this file, wip moving out of cart @@wip
 */
import { observable, action } from 'xmobx/mobx';
import { isErrorLikeResponse } from '@skava/is-error-like-response';
import { errorContainer, toastMessage } from '@skava/packages/core/notifications';
import { state } from '@skava/packages/pages/Product/state/ProductDetailsContainer';
import { transformSaveForLaterItems, transformListId, findSaveForLaterList, constructParams, } from './deps';
import { listContainerApi } from '../list/container.list';
import { listContainer } from '../list/container';
export class SaveForLaterContainer {
    constructor() {
        /**
         * @todo @name saveForLaterProducts
         */
        this.saveForLaterProduct = [];
        this.cartCount = 0;
        this.saveListItemsId = [];
        this.saveListId = '';
        this.saveForLaterListsId = '';
    }
    async fetchSaveListItems(saveForLaterListId) {
        if (saveForLaterListId) {
            return listContainerApi.getListBySku(saveForLaterListId);
        }
        else {
            return {};
        }
    }
    async fetchSavedForLaterInner(productInfo) {
        // const saveForLaterProductList: OneProduct[] = []
        const saveForLaterProductList = {
            products: [],
            isSaveForLater: true,
        };
        /**
         * @todo add to gql instead
         */
        const product = productInfo.map(async (productItem) => {
            const { identifier } = productItem;
            const item = await state.productDetail(identifier);
            return item;
        });
        const savedItemList = await Promise.all(product);
        saveForLaterProductList.products = savedItemList;
        this.saveForLaterProduct = [];
        this.saveForLaterProduct = saveForLaterProductList;
    }
    async fetchSaveForLater() {
        console.log('[SaveForLAterContainer]_fetchSaveForLater');
        const saveForLaterListId = this.saveForLaterListsId;
        const saveForLaterList = await this.fetchSaveListItems(saveForLaterListId);
        const savedListItems = saveForLaterList.listItems ? saveForLaterList.listItems : [];
        const productInfo = savedListItems.map(transformSaveForLaterItems);
        this.saveListItemsId = savedListItems.map(transformListId);
        this.fetchSavedForLaterInner(productInfo);
        return this;
    }
    async createSaveForLaterList(listResponse) {
        console.debug('[Lists]{ListApi} createSaveForLaterList');
        // @todo @@perf
        const saveForLaterList = listResponse &&
            listResponse.listOfLists &&
            listResponse.listOfLists.find(findSaveForLaterList);
        if (saveForLaterList && saveForLaterList.listId) {
            this.saveForLaterListsId = saveForLaterList.listId;
            this.fetchSaveForLater();
        }
        else {
            this.createDefaultListForSave();
        }
    }
    async createDefaultListForSave() {
        const createListParams = constructParams();
        const createListResponse = await listContainerApi.createList(createListParams, false);
        this.saveForLaterListsId = createListResponse.listId || '';
    }
    /**
     * @todo @@training split this up, use exotic
     */
    async addToSaveList(productInfo) {
        console.log('[SaveForLAterContainer] addToSaveList');
        const response = await listContainer.addToSaveList(productInfo);
        if (!isErrorLikeResponse(response)) {
            this.fetchSaveForLater();
        }
        return response;
    }
    async removeFromSavedList(productInfo, isFromMoveToCart) {
        console.log('[SaveForLAterContainer] removeFromSavedList');
        const response = await listContainer.removeFromSavedList(productInfo);
        if (!isFromMoveToCart) {
            errorContainer.setMessage(toastMessage.removeSaveForLaterSuccess);
        }
        if (!isErrorLikeResponse(response)) {
            this.fetchSaveForLater();
        }
    }
}
tslib_1.__decorate([
    observable
], SaveForLaterContainer.prototype, "saveForLaterProduct", void 0);
tslib_1.__decorate([
    observable
], SaveForLaterContainer.prototype, "cartCount", void 0);
tslib_1.__decorate([
    observable
], SaveForLaterContainer.prototype, "saveListItemsId", void 0);
tslib_1.__decorate([
    observable
], SaveForLaterContainer.prototype, "saveListId", void 0);
tslib_1.__decorate([
    observable
], SaveForLaterContainer.prototype, "saveForLaterListsId", void 0);
tslib_1.__decorate([
    action.bound
], SaveForLaterContainer.prototype, "fetchSaveListItems", null);
tslib_1.__decorate([
    action.bound
], SaveForLaterContainer.prototype, "fetchSaveForLater", null);
tslib_1.__decorate([
    action.bound
], SaveForLaterContainer.prototype, "createSaveForLaterList", null);
tslib_1.__decorate([
    action.bound
], SaveForLaterContainer.prototype, "addToSaveList", null);
tslib_1.__decorate([
    action.bound
], SaveForLaterContainer.prototype, "removeFromSavedList", null);
//# sourceMappingURL=SaveForLaterContainer.js.map