@polls-platform/core
Version:
Polls Platform core library
95 lines (94 loc) • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.constructPollUrl = void 0;
const types_1 = require("../types");
const config_1 = require("../config");
const constants_1 = require("../constants");
const url_params_1 = require("./url-params");
const shopify_config_1 = require("../shopify-config");
// endregion
/* ****************************************************************************************************************** */
// region: Utils
/* ****************************************************************************************************************** */
function constructPollUrl({ pollId, flow, pollState, themeName: passedThemeName, themeObject: passedThemeObject, storeId: passedStoreId, }) {
const { domainConfig, environment } = (0, config_1.getConfig)();
// check for shopify config
const shopifyConfig = (0, shopify_config_1.getShopifyConfig)();
let storeId = passedStoreId;
let themeObject = passedThemeObject;
if (shopifyConfig) {
storeId = shopifyConfig.storeId;
themeObject = shopifyConfig.dynamicPollTheme;
}
// there isn't a global theme name yet
const themeName = passedThemeName;
let urlString;
switch (domainConfig.type) {
default:
case types_1.DomainConfigType.subdomain: {
let subdomainSuffix;
switch (environment) {
default:
case types_1.Environment.production: {
subdomainSuffix = '';
break;
}
case types_1.Environment.qa: {
subdomainSuffix = constants_1.QA_DOMAIN_SUFFIX;
break;
}
}
let pollPath = constants_1.POLL_PATH;
let share = true;
switch (flow) {
case types_1.OpenPollFlow.openPoll: {
share = false;
break;
}
case types_1.OpenPollFlow.createPoll: {
switch (pollState) {
case types_1.PollState.draft: {
pollPath = constants_1.DRAFT_POLL_PATH;
share = false;
break;
}
default:
case types_1.PollState.live: {
pollPath = constants_1.POLL_PATH;
share = true;
break;
}
}
break;
}
}
urlString = `${constants_1.DEFAULT_URL_SCHEME}${domainConfig.subdomain}${subdomainSuffix}.${constants_1.TOP_LEVEL_DOMAIN}/${pollPath}/${pollId}`;
const urlParamOptions = {
pending: true,
share: share,
flow: flow,
};
if (storeId) {
urlParamOptions.storeId = storeId;
}
if (themeObject) {
try {
const themeJsonString = JSON.stringify(themeObject);
const encodedThemeString = btoa(unescape(encodeURIComponent(themeJsonString)));
urlParamOptions.theme = encodedThemeString;
}
catch (error) {
// do nothing
}
}
else if (themeName) {
urlParamOptions.theme = themeName;
}
urlString = (0, url_params_1.setPollUrlParams)(urlString, urlParamOptions);
break;
}
}
return urlString;
}
exports.constructPollUrl = constructPollUrl;
// endregion