@claspo/renderer 18.8.1 → 18.9.0-sp1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claspo/renderer",
3
- "version": "18.8.1",
3
+ "version": "18.9.0-sp1",
4
4
  "scripts": {
5
5
  "dev": " tsc --project tsconfig.json --watch",
6
6
  "build": "rm -rf out && tsc --project tsconfig.json && npm run minify",
@@ -15,7 +15,7 @@
15
15
  "node": ">=18.16.0"
16
16
  },
17
17
  "dependencies": {
18
- "@claspo/common": "7.4.0",
18
+ "@claspo/common": "7.5.0-sp1",
19
19
  "@testing-library/dom": "^10.4.0"
20
20
  },
21
21
  "devDependencies": {
@@ -15,5 +15,7 @@ export default class PrizePool {
15
15
  value: any;
16
16
  };
17
17
  _calculatePrize(): any;
18
+ _mapShopifyDiscountLabel(option: any): string;
19
+ _hasDynamicCodeOption(): any;
18
20
  _mapLabelSuffix(value: any, valueType: any): any;
19
21
  }
@@ -1 +1 @@
1
- import sum from"@claspo/common/math/sum";import RandomDistributor from"@claspo/common/math/RandomDistributor";const PrizePoolSourceType={MANUAL:"MANUAL",FROM_EXTERNAL_SOURCE:"FROM_EXTERNAL_SOURCE"};export default class PrizePool{constructor(i,t,e){if(this.instanceModel=i,this.httpClient=t,this.configService=e,this.configService.getConfig("prizePoolModels")){const i=this.configService.getConfig("prizePoolModels").find(i=>i.id===this.instanceModel.id);i&&(this.instanceModel=i)}this.prizeOption=null,this.randomDistributor=new RandomDistributor}async load(){if(this.instanceModel.sourceType===PrizePoolSourceType.MANUAL&&!this.instanceModel.options){const i=this.configService.getConfig("widgetId"),t=this.configService.getConfig("hostUrl"),e=this.instanceModel.id,o=`${t}forms/v2/prize/pool/${e}`,s=`${t}forms/v1/prizePool/${e}/reward?widgetId=${i}`;try{const[i,t]=await Promise.all([await this.httpClient.execute(o,"GET").then(i=>i.json()),await this.httpClient.execute(s,"GET",null,{"cl-customer-id":this.configService.getConfig("clCustomerId")}).then(i=>i.json())]);this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{options:i.options});const e=i.options.find(i=>i.id===t.id);this.prizeOption=Object.assign(Object.assign({},e),t)}catch(t){console.error(`PrizePool: failed to load pool or reward for widgetId "${i}" with poolId "${e}"`,t)}}}getModel(){return this.instanceModel}async getPrize(){return this.instanceModel?"FIXED"===this.instanceModel.model?this.instanceModel.data:this.instanceModel.sourceType===PrizePoolSourceType.MANUAL?(this.instanceModel.options&&!this.prizeOption&&(this.prizeOption=this._calculatePrize()),this.prizeOption?this.mapPrizeOption(this.prizeOption):null):void 0:null}submitPrize(){}mapPrizeOption(i){let t=this._mapLabelSuffix(i.value,i.valueType),e=i.code;return{id:i.id,label:t,value:e}}_calculatePrize(){const i=sum(this.instanceModel.options.map(i=>parseFloat(i.weight)));return this.randomDistributor.calculateFromWeights(this.instanceModel.options,i)}_mapLabelSuffix(i,t){return"DISCOUNT_PERCENTAGE"===t?`${i}%`:i}}
1
+ import sum from"@claspo/common/math/sum";import RandomDistributor from"@claspo/common/math/RandomDistributor";import{PrizeOptionValueType,PrizeSourceType,ShopifyDiscountType,ShopifyPromoCodeMode}from"@claspo/common/PrizePool.interface";const FETCHABLE_SOURCE_TYPES=[PrizeSourceType.MANUAL,PrizeSourceType.SHOPIFY];export default class PrizePool{constructor(i,e,o){if(this.httpClient=e,this.configService=o,this.instanceModel=i||{},"FIXED"!==this.instanceModel.model){const i=(this.configService.getConfig("prizePoolIds")||[])[0];i&&i!==this.instanceModel.id&&(this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{id:i,options:void 0}));const e=this.configService.getConfig("prizePoolModels");if(e){const i=this.instanceModel.id&&e.find(i=>i.id===this.instanceModel.id)||e[0];i&&(this.instanceModel=i)}}this.prizeOption=null,this.randomDistributor=new RandomDistributor}async load(){const{id:i,model:e,sourceType:o,options:t}=this.instanceModel;if(!(!t&&!!i&&"FIXED"!==e&&(!o||FETCHABLE_SOURCE_TYPES.includes(o))))return;const n=this.configService.getConfig("widgetId");try{const e=this.configService.getConfig("hostUrl"),t=`${e}forms/v2/prize/pool/${i}`,s=`${e}forms/v1/prizePool/${i}/reward?widgetId=${n}`,[r,l]=await Promise.all([await this.httpClient.execute(t,"GET").then(i=>i.json()),await this.httpClient.execute(s,"GET",null,{"cl-customer-id":this.configService.getConfig("clCustomerId")}).then(i=>i.json())]);this.instanceModel=Object.assign(Object.assign({},this.instanceModel),{sourceType:o||r.sourceType,options:r.options});const c=r.options.find(i=>i.id===l.id);this.prizeOption=Object.assign(Object.assign({},c),l)}catch(e){console.error(`PrizePool: failed to load pool or reward for widgetId "${n}" with poolId "${i}"`,e)}}getModel(){return this.instanceModel}async getPrize(){if(!this.instanceModel)return null;if("FIXED"===this.instanceModel.model)return this.instanceModel.data;if(FETCHABLE_SOURCE_TYPES.includes(this.instanceModel.sourceType)){if(this.instanceModel.options&&!this.prizeOption){if(this._hasDynamicCodeOption())return null;this.prizeOption=this._calculatePrize()}return this.prizeOption?this.mapPrizeOption(this.prizeOption):null}return null}submitPrize(){}mapPrizeOption(i){const e=this._mapShopifyDiscountLabel(i);let o=null!==e?e:i.valueType===PrizeOptionValueType.PROMOCODE?i.value||"":this._mapLabelSuffix(i.value,i.valueType),t=i.code;return{id:i.id,label:o,value:t}}_calculatePrize(){const i=sum(this.instanceModel.options.map(i=>parseFloat(i.weight)));return this.randomDistributor.calculateFromWeights(this.instanceModel.options,i)}_mapShopifyDiscountLabel(i){var e;const o=null===(e=null==i?void 0:i.config)||void 0===e?void 0:e.shopify;return o&&void 0!==o.discountValue&&null!==o.discountValue?o.discountType===ShopifyDiscountType.PERCENTAGE?`${o.discountValue}%`:`${o.discountValue}`:null}_hasDynamicCodeOption(){return(this.instanceModel.options||[]).some(i=>{var e,o;return(null===(o=null===(e=null==i?void 0:i.config)||void 0===e?void 0:e.shopify)||void 0===o?void 0:o.mode)===ShopifyPromoCodeMode.UNIQUE})}_mapLabelSuffix(i,e){return e===PrizeOptionValueType.DISCOUNT_PERCENTAGE?`${i}%`:i}}
@@ -1 +1 @@
1
- import{ContextEvents}from"../context/ContextEvents";export default class AbstractMergeTagsProcessor{constructor(e,t){this.context=e,this.configService=t,this.element=null,this.mergeTags=null,this.componentId=null,this.contextRecordUpdatesSubscription=null}destroy(){this.contextRecordUpdatesSubscription&&(this.contextRecordUpdatesSubscription.off(),this.contextRecordUpdatesSubscription=null)}process(e,t,s){this.element=e,this.mergeTags=t,this.componentId=s,this._subscribeToDynamicMergeTagUpdates(),this.mergeTags&&0!==Object.keys(this.mergeTags).length&&this.element.childNodes.forEach(e=>{if(e.nodeType!==Node.TEXT_NODE&&"MERGE_TAG"===e.getAttribute("cl-type")){const t=e.getAttribute("cl-id"),s=this.mergeTags[t];if(!s)return;this.processSingleMergeTag(e,s)}})}processSingleMergeTag(e,t){throw new Error("BaseMergeTagsProcessor: processSingleMergeTag is not implemented")}_subscribeToDynamicMergeTagUpdates(){this.contextRecordUpdatesSubscription&&(this.contextRecordUpdatesSubscription.off(),this.contextRecordUpdatesSubscription=null),this.contextRecordUpdatesSubscription=this.context.on(ContextEvents.RECORD_UPDATED,e=>{this._processDynamicMergeTags(e)})}_processDynamicMergeTags(e){if(!this.element||!this.mergeTags)return;const t=(null==e?void 0:e.key)||(null==e?void 0:e.id);if(!t)return;const s=this.mergeTags[t];if(!s)return;const i=this._findMergeTagElement(s.id);i&&this.processSingleMergeTag(i,s)}_findMergeTagElement(e){return this.element?this.element.getAttribute("cl-id")===e?this.element:this.element.querySelector(`[cl-id="${e}"]`):null}}
1
+ import{ContextEvents}from"../context/ContextEvents";export default class AbstractMergeTagsProcessor{constructor(e,t){this.context=e,this.configService=t,this.element=null,this.mergeTags=null,this.componentId=null,this.contextRecordUpdatesSubscription=null}destroy(){this.contextRecordUpdatesSubscription&&(this.contextRecordUpdatesSubscription.off(),this.contextRecordUpdatesSubscription=null)}process(e,t,s){this.element=e,this.mergeTags=t,this.componentId=s,this._subscribeToDynamicMergeTagUpdates(),this.mergeTags&&0!==Object.keys(this.mergeTags).length&&this.element.querySelectorAll('[cl-type="MERGE_TAG"]').forEach(e=>{const t=e.getAttribute("cl-id"),s=this.mergeTags[t];s&&this.processSingleMergeTag(e,s)})}processSingleMergeTag(e,t){throw new Error("BaseMergeTagsProcessor: processSingleMergeTag is not implemented")}_subscribeToDynamicMergeTagUpdates(){this.contextRecordUpdatesSubscription&&(this.contextRecordUpdatesSubscription.off(),this.contextRecordUpdatesSubscription=null),this.contextRecordUpdatesSubscription=this.context.on(ContextEvents.RECORD_UPDATED,e=>{this._processDynamicMergeTags(e)})}_processDynamicMergeTags(e){if(!this.element||!this.mergeTags)return;const t=(null==e?void 0:e.key)||(null==e?void 0:e.id);if(!t)return;const s=this.mergeTags[t];if(!s)return;const i=this._findMergeTagElement(s.id);i&&this.processSingleMergeTag(i,s)}_findMergeTagElement(e){return this.element?this.element.getAttribute("cl-id")===e?this.element:this.element.querySelector(`[cl-id="${e}"]`):null}}