UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

193 lines (192 loc) 10.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.signalApiAccessFailed = exports.XCAP_API_ACCESS_FAILED = exports.loadInitialStoreValues = exports.initialize = void 0; var index_1 = require("./index"); var loginAction_1 = require("../login/loginAction"); var communityAction_1 = require("../stackend/communityAction"); var configReducer_1 = require("./configReducer"); var requestActions_1 = require("../request/requestActions"); var moduleAction_1 = require("../stackend/moduleAction"); var cmsActions_1 = require("../cms/cmsActions"); var pageActions_1 = require("../cms/pageActions"); var modules_1 = require("../stackend/modules"); var shopReducer_1 = require("../shop/shopReducer"); var shopActions_1 = require("../shop/shopActions"); /** * Initialize the stackend API. * Supply either communityId or permalink * @param props */ function initialize(props) { var _this = this; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { if (!props.communityId && !props.permalink) { throw Error('Supply communityId or permalink'); } if (props.logger) { (0, index_1.setLogger)(props.logger); } if (props.config) { (0, index_1.setConfigDefaults)(props.config); dispatch((0, index_1.setConfiguration)(props.config)); } return [2 /*return*/, dispatch(loadInitialStoreValues(props))]; }); }); }; } exports.initialize = initialize; function receiveInitialStoreValues(json) { return { type: configReducer_1.XCAP_INITIAL_STORE_DATA_RECEIVED, json: json }; } /* * Populate the initial redux store. */ function loadInitialStoreValues(params) { var _this = this; return function (dispatch) { return __awaiter(_this, void 0, void 0, function () { var r, allCmsContents, community, _a, products, collections, listings, shopifyDomainReferenceUrlId; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, dispatch((0, index_1.getInitialStoreValues)(params))]; case 1: r = _b.sent(); if (typeof r === 'undefined' || r === null || r.error) { return [2 /*return*/, r]; } if (Object.keys(r.modules).length !== 0) { dispatch((0, moduleAction_1.receiveModules)({ modules: r.modules })); } allCmsContents = {}; if (r.cmsPages && Object.keys(r.cmsPages).length !== 0) { dispatch((0, pageActions_1.receivePages)((0, index_1.newXcapJsonResult)('success', { pages: r.cmsPages }))); // Add content Object.values(r.cmsPages).forEach(function (p) { var page = p; page.content.forEach(function (pc) { if (pc.type === modules_1.ModuleType.CMS) { if (pc.referenceRef) { allCmsContents[pc.referenceRef.id] = pc.referenceRef; } else { console.warn('Stackend: cms content ' + pc.name + ' of page ' + page.id + ' is missing'); } } }); }); } if (r.cmsContents && Object.keys(r.cmsContents).length !== 0) { Object.values(r.cmsContents).forEach(function (c) { var y = c; if (y) { allCmsContents[y.id] = y; } }); } if (Object.keys(allCmsContents).length !== 0) { dispatch((0, cmsActions_1.receiveContents)(allCmsContents)); } if (r.subSites && Object.keys(r.subSites).length !== 0) { dispatch((0, pageActions_1.receiveSubSites)({ subSites: r.subSites })); } community = r.stackendCommunity; dispatch(receiveInitialStoreValues(r)); dispatch((0, requestActions_1.setRequestInfo)({ referenceUrlId: r.referenceUrlId })); dispatch((0, loginAction_1.receiveLoginData)({ user: r.user })); dispatch((0, communityAction_1.setCurrentCommunity)(community)); dispatch((0, shopActions_1.setCommunityVATS)(community)); //dispatch(receiveNotificationCounts({ numberOfUnseen: r.numberOfUnseen })); dispatch((0, communityAction_1.receiveResourceUsage)(r)); // fields not documented if (r.shopData) { _a = r.shopData, products = _a.products, collections = _a.collections, listings = _a.listings, shopifyDomainReferenceUrlId = _a.shopifyDomainReferenceUrlId; if (Object.keys(products).length !== 0) { dispatch({ type: shopReducer_1.RECEIVE_MULTIPLE_PRODUCTS, json: { products: products } }); } if (Object.keys(collections).length !== 0) { dispatch({ type: shopReducer_1.RECEIVE_COLLECTIONS, collections: collections }); } if (Object.keys(listings).length !== 0) { dispatch({ type: shopReducer_1.RECEIVE_LISTINGS, listings: listings }); } if (shopifyDomainReferenceUrlId !== 0) { dispatch({ type: shopReducer_1.RECEIVE_SHOPIFY_DOMAIN_REFERENCE_URL_ID, shopifyDomainReferenceUrlId: shopifyDomainReferenceUrlId }); } if (community.settings.shop && typeof community.settings.shop.enableCartNotifications === 'boolean') { dispatch((0, shopActions_1.setEnableCartNotifications)(community.settings.shop.enableCartNotifications)); } } /* TODO: Handle this if (r.data) { } */ return [2 /*return*/, r]; } }); }); }; } exports.loadInitialStoreValues = loadInitialStoreValues; /** * Signals that an api access has failed due to some error, for example server down or insufficient privileges. * A custom reducer could set up logic to handle re-authentication */ exports.XCAP_API_ACCESS_FAILED = 'XCAP_API_ACCESS_FAILED'; /** * Signals that an api access has failed due to some error, for example server down or insufficient privileges * * @param url * @param result */ function signalApiAccessFailed(url, result) { return { type: exports.XCAP_API_ACCESS_FAILED, url: url, result: result }; } exports.signalApiAccessFailed = signalApiAccessFailed; //# sourceMappingURL=actions.js.map