ui.shipaid.com 0.0.6 → 0.0.9
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/README.md +70 -0
- package/dist/ui.shipaid.com.es.js +184 -27
- package/dist/ui.shipaid.com.iife.js +189 -23
- package/dist/ui.shipaid.com.umd.js +189 -23
- package/package.json +9 -11
- package/types/shipaid.ts +44 -7
- package/types/shopify.ts +13 -12
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# ShipAid Shopify Widget
|
|
2
|
+
|
|
3
|
+
This is the repository for the ShipAid Shopify (and possibly others in future) widget. It uses the [Lit](https://lit.dev/) web components library, so it is recommended to become familiar with it before contributing to this repository.
|
|
4
|
+
|
|
5
|
+
Using Lit provides a framework that allows us to build a reactive UI, using JSX-like syntax - no need to use JQuery etc. And it can easily be installed in a page by using the custom web component name:
|
|
6
|
+
```html
|
|
7
|
+
<shipaid-widget>Fallback Content</shipaid-widget>
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
This widget provides an interface where a user can choose to add or remove ShipAid protection - this is actually a product in their store that can be added to cart. When the component is loaded, we immediately run a request to fetch the ShipAid protection product details from our API, as well as the customers current cart from the [Shopify AJAX API](https://shopify.dev/api/ajax/reference/cart).
|
|
13
|
+
Once we have this data, we can check whether the customer currently has the ShipAid product in their cart, and show either the add/remove product buttons based on this.
|
|
14
|
+
|
|
15
|
+
We also emit various [custom events](https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events#adding_custom_data_%E2%80%93_customevent) when we add or remove the ShipAid product from the cart, so other developers can listen to these events, and update AJAX carts.
|
|
16
|
+
|
|
17
|
+
### Events
|
|
18
|
+
|
|
19
|
+
This is a list of the events emitted by the widget:
|
|
20
|
+
|
|
21
|
+
| Event | Description | Payload |
|
|
22
|
+
|-------|-------------|---------|
|
|
23
|
+
| `shipaid-loaded` | Dispatched when the widget has finished fetching data from the API, and has successfully rendered. | Contains the data from the ShipAid API: [`ShipAidStore`](/types/shipaid.ts) |
|
|
24
|
+
| `shipaid-protection-status` | Dispatched a user either adds or removes the protection product from their cart. | `{ protection: boolean, cart: ShopifyCart, lineItem: ShopifyCartItem }` |
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
### Requirements
|
|
30
|
+
|
|
31
|
+
- Node v16+
|
|
32
|
+
- PNPM
|
|
33
|
+
- Development Shopify store (with the ShipAid app installed)
|
|
34
|
+
- ShipAid API development/staging instance
|
|
35
|
+
|
|
36
|
+
### Development
|
|
37
|
+
|
|
38
|
+
You will need to make sure your development store has the ShipAid app installed, so the store and its protection product is added to the DB. You will also need to ensure the Shopify app you are testing this with has an app proxy added, and pointed towards an API instance.
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
pnpm install
|
|
42
|
+
pnpm run develop
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Once the project is running, add the below to your development Shopify store `theme.liquid`:
|
|
46
|
+
|
|
47
|
+
```html
|
|
48
|
+
<!-- Dev -->
|
|
49
|
+
<script src="http://localhost:3000/src/shipaid-widget.ts"type="module" ></script>
|
|
50
|
+
<!-- Prod -->
|
|
51
|
+
<script src="https://unpkg.com/ui.shipaid.com?module" type="module"></script>
|
|
52
|
+
```
|
|
53
|
+
And add the widget element in your cart (likely `/sections/main-cart-items.liquid`):
|
|
54
|
+
|
|
55
|
+
```html
|
|
56
|
+
<shipaid-widget>
|
|
57
|
+
<p>Loading ShipAid Protection</p>
|
|
58
|
+
</shipaid-widget>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Build
|
|
62
|
+
|
|
63
|
+
Once the project has been built, you can publish the project to NPM, and users can add the script to their store using a package CDN (I.e. [Unpkg](https://unpkg.com/)).
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
pnpm install
|
|
67
|
+
pnpm run lint
|
|
68
|
+
pnpm run build
|
|
69
|
+
pnpm publish
|
|
70
|
+
```
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Et=Object.defineProperty,St=Object.defineProperties;var xt=Object.getOwnPropertyDescriptors;var it=Object.getOwnPropertySymbols;var Ct=Object.prototype.hasOwnProperty,Pt=Object.prototype.propertyIsEnumerable;var ot=(o,t,e)=>t in o?Et(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,b=(o,t)=>{for(var e in t||(t={}))Ct.call(t,e)&&ot(o,e,t[e]);if(it)for(var e of it(t))Pt.call(t,e)&&ot(o,e,t[e]);return o},A=(o,t)=>St(o,xt(t));/**
|
|
2
2
|
* @license
|
|
3
3
|
* Copyright 2017 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
5
|
-
*/const
|
|
5
|
+
*/const Tt=o=>t=>typeof t=="function"?((e,i)=>(window.customElements.define(e,i),i))(o,t):((e,i)=>{const{kind:r,elements:s}=i;return{kind:r,elements:s,finisher(n){window.customElements.define(e,n)}}})(o,t);/**
|
|
6
6
|
* @license
|
|
7
7
|
* Copyright 2017 Google LLC
|
|
8
8
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
9
|
-
*/const
|
|
9
|
+
*/const Ut=(o,t)=>t.kind==="method"&&t.descriptor&&!("value"in t.descriptor)?A(b({},t),{finisher(e){e.createProperty(t.key,o)}}):{kind:"field",key:Symbol(),placement:"own",descriptor:{},originalKey:t.key,initializer(){typeof t.initializer=="function"&&(this[t.key]=t.initializer.call(this))},finisher(e){e.createProperty(t.key,o)}};function kt(o){return(t,e)=>e!==void 0?((i,r,s)=>{r.constructor.createProperty(s,i)})(o,t,e):Ut(o,t)}/**
|
|
10
10
|
* @license
|
|
11
11
|
* Copyright 2017 Google LLC
|
|
12
12
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
13
|
-
*/function
|
|
13
|
+
*/function _(o){return kt(A(b({},o),{state:!0}))}/**
|
|
14
14
|
* @license
|
|
15
15
|
* Copyright 2021 Google LLC
|
|
16
16
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
@@ -18,26 +18,27 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
18
18
|
* @license
|
|
19
19
|
* Copyright 2019 Google LLC
|
|
20
20
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
21
|
-
*/const
|
|
21
|
+
*/const F=window.ShadowRoot&&(window.ShadyCSS===void 0||window.ShadyCSS.nativeShadow)&&"adoptedStyleSheets"in Document.prototype&&"replace"in CSSStyleSheet.prototype,j=Symbol(),rt=new Map;class st{constructor(t,e){if(this._$cssResult$=!0,e!==j)throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText=t}get styleSheet(){let t=rt.get(this.cssText);return F&&t===void 0&&(rt.set(this.cssText,t=new CSSStyleSheet),t.replaceSync(this.cssText)),t}toString(){return this.cssText}}const Nt=o=>new st(typeof o=="string"?o:o+"",j),Ht=(o,...t)=>{const e=o.length===1?o[0]:t.reduce((i,r,s)=>i+(n=>{if(n._$cssResult$===!0)return n.cssText;if(typeof n=="number")return n;throw Error("Value passed to 'css' function must be a 'css' function result: "+n+". Use 'unsafeCSS' to pass non-literal values, but take care to ensure page security.")})(r)+o[s+1],o[0]);return new st(e,j)},Ot=(o,t)=>{F?o.adoptedStyleSheets=t.map(e=>e instanceof CSSStyleSheet?e:e.styleSheet):t.forEach(e=>{const i=document.createElement("style"),r=window.litNonce;r!==void 0&&i.setAttribute("nonce",r),i.textContent=e.cssText,o.appendChild(i)})},nt=F?o=>o:o=>o instanceof CSSStyleSheet?(t=>{let e="";for(const i of t.cssRules)e+=i.cssText;return Nt(e)})(o):o;/**
|
|
22
22
|
* @license
|
|
23
23
|
* Copyright 2017 Google LLC
|
|
24
24
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
25
|
-
*/var
|
|
25
|
+
*/var V;const at=window.trustedTypes,It=at?at.emptyScript:"",pt=window.reactiveElementPolyfillSupport,B={toAttribute(o,t){switch(t){case Boolean:o=o?It:null;break;case Object:case Array:o=o==null?o:JSON.stringify(o)}return o},fromAttribute(o,t){let e=o;switch(t){case Boolean:e=o!==null;break;case Number:e=o===null?null:Number(o);break;case Object:case Array:try{e=JSON.parse(o)}catch{e=null}}return e}},ht=(o,t)=>t!==o&&(t==t||o==o),q={attribute:!0,type:String,converter:B,reflect:!1,hasChanged:ht};class S extends HTMLElement{constructor(){super(),this._$Et=new Map,this.isUpdatePending=!1,this.hasUpdated=!1,this._$Ei=null,this.o()}static addInitializer(t){var e;(e=this.l)!==null&&e!==void 0||(this.l=[]),this.l.push(t)}static get observedAttributes(){this.finalize();const t=[];return this.elementProperties.forEach((e,i)=>{const r=this._$Eh(i,e);r!==void 0&&(this._$Eu.set(r,i),t.push(r))}),t}static createProperty(t,e=q){if(e.state&&(e.attribute=!1),this.finalize(),this.elementProperties.set(t,e),!e.noAccessor&&!this.prototype.hasOwnProperty(t)){const i=typeof t=="symbol"?Symbol():"__"+t,r=this.getPropertyDescriptor(t,i,e);r!==void 0&&Object.defineProperty(this.prototype,t,r)}}static getPropertyDescriptor(t,e,i){return{get(){return this[e]},set(r){const s=this[t];this[e]=r,this.requestUpdate(t,s,i)},configurable:!0,enumerable:!0}}static getPropertyOptions(t){return this.elementProperties.get(t)||q}static finalize(){if(this.hasOwnProperty("finalized"))return!1;this.finalized=!0;const t=Object.getPrototypeOf(this);if(t.finalize(),this.elementProperties=new Map(t.elementProperties),this._$Eu=new Map,this.hasOwnProperty("properties")){const e=this.properties,i=[...Object.getOwnPropertyNames(e),...Object.getOwnPropertySymbols(e)];for(const r of i)this.createProperty(r,e[r])}return this.elementStyles=this.finalizeStyles(this.styles),!0}static finalizeStyles(t){const e=[];if(Array.isArray(t)){const i=new Set(t.flat(1/0).reverse());for(const r of i)e.unshift(nt(r))}else t!==void 0&&e.push(nt(t));return e}static _$Eh(t,e){const i=e.attribute;return i===!1?void 0:typeof i=="string"?i:typeof t=="string"?t.toLowerCase():void 0}o(){var t;this._$Ep=new Promise(e=>this.enableUpdating=e),this._$AL=new Map,this._$Em(),this.requestUpdate(),(t=this.constructor.l)===null||t===void 0||t.forEach(e=>e(this))}addController(t){var e,i;((e=this._$Eg)!==null&&e!==void 0?e:this._$Eg=[]).push(t),this.renderRoot!==void 0&&this.isConnected&&((i=t.hostConnected)===null||i===void 0||i.call(t))}removeController(t){var e;(e=this._$Eg)===null||e===void 0||e.splice(this._$Eg.indexOf(t)>>>0,1)}_$Em(){this.constructor.elementProperties.forEach((t,e)=>{this.hasOwnProperty(e)&&(this._$Et.set(e,this[e]),delete this[e])})}createRenderRoot(){var t;const e=(t=this.shadowRoot)!==null&&t!==void 0?t:this.attachShadow(this.constructor.shadowRootOptions);return Ot(e,this.constructor.elementStyles),e}connectedCallback(){var t;this.renderRoot===void 0&&(this.renderRoot=this.createRenderRoot()),this.enableUpdating(!0),(t=this._$Eg)===null||t===void 0||t.forEach(e=>{var i;return(i=e.hostConnected)===null||i===void 0?void 0:i.call(e)})}enableUpdating(t){}disconnectedCallback(){var t;(t=this._$Eg)===null||t===void 0||t.forEach(e=>{var i;return(i=e.hostDisconnected)===null||i===void 0?void 0:i.call(e)})}attributeChangedCallback(t,e,i){this._$AK(t,i)}_$ES(t,e,i=q){var r,s;const n=this.constructor._$Eh(t,i);if(n!==void 0&&i.reflect===!0){const c=((s=(r=i.converter)===null||r===void 0?void 0:r.toAttribute)!==null&&s!==void 0?s:B.toAttribute)(e,i.type);this._$Ei=t,c==null?this.removeAttribute(n):this.setAttribute(n,c),this._$Ei=null}}_$AK(t,e){var i,r,s;const n=this.constructor,c=n._$Eu.get(t);if(c!==void 0&&this._$Ei!==c){const a=n.getPropertyOptions(c),p=a.converter,l=(s=(r=(i=p)===null||i===void 0?void 0:i.fromAttribute)!==null&&r!==void 0?r:typeof p=="function"?p:null)!==null&&s!==void 0?s:B.fromAttribute;this._$Ei=c,this[c]=l(e,a.type),this._$Ei=null}}requestUpdate(t,e,i){let r=!0;t!==void 0&&(((i=i||this.constructor.getPropertyOptions(t)).hasChanged||ht)(this[t],e)?(this._$AL.has(t)||this._$AL.set(t,e),i.reflect===!0&&this._$Ei!==t&&(this._$EC===void 0&&(this._$EC=new Map),this._$EC.set(t,i))):r=!1),!this.isUpdatePending&&r&&(this._$Ep=this._$E_())}async _$E_(){this.isUpdatePending=!0;try{await this._$Ep}catch(e){Promise.reject(e)}const t=this.scheduleUpdate();return t!=null&&await t,!this.isUpdatePending}scheduleUpdate(){return this.performUpdate()}performUpdate(){var t;if(!this.isUpdatePending)return;this.hasUpdated,this._$Et&&(this._$Et.forEach((r,s)=>this[s]=r),this._$Et=void 0);let e=!1;const i=this._$AL;try{e=this.shouldUpdate(i),e?(this.willUpdate(i),(t=this._$Eg)===null||t===void 0||t.forEach(r=>{var s;return(s=r.hostUpdate)===null||s===void 0?void 0:s.call(r)}),this.update(i)):this._$EU()}catch(r){throw e=!1,this._$EU(),r}e&&this._$AE(i)}willUpdate(t){}_$AE(t){var e;(e=this._$Eg)===null||e===void 0||e.forEach(i=>{var r;return(r=i.hostUpdated)===null||r===void 0?void 0:r.call(i)}),this.hasUpdated||(this.hasUpdated=!0,this.firstUpdated(t)),this.updated(t)}_$EU(){this._$AL=new Map,this.isUpdatePending=!1}get updateComplete(){return this.getUpdateComplete()}getUpdateComplete(){return this._$Ep}shouldUpdate(t){return!0}update(t){this._$EC!==void 0&&(this._$EC.forEach((e,i)=>this._$ES(i,this[i],e)),this._$EC=void 0),this._$EU()}updated(t){}firstUpdated(t){}}S.finalized=!0,S.elementProperties=new Map,S.elementStyles=[],S.shadowRootOptions={mode:"open"},pt==null||pt({ReactiveElement:S}),((V=globalThis.reactiveElementVersions)!==null&&V!==void 0?V:globalThis.reactiveElementVersions=[]).push("1.3.0");/**
|
|
26
26
|
* @license
|
|
27
27
|
* Copyright 2017 Google LLC
|
|
28
28
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
29
|
-
*/var
|
|
29
|
+
*/var W;const x=globalThis.trustedTypes,ct=x?x.createPolicy("lit-html",{createHTML:o=>o}):void 0,y=`lit$${(Math.random()+"").slice(9)}$`,dt="?"+y,Lt=`<${dt}>`,C=document,N=(o="")=>C.createComment(o),H=o=>o===null||typeof o!="object"&&typeof o!="function",lt=Array.isArray,zt=o=>{var t;return lt(o)||typeof((t=o)===null||t===void 0?void 0:t[Symbol.iterator])=="function"},O=/<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g,ut=/-->/g,ft=/>/g,E=/>|[ \n\r](?:([^\s"'>=/]+)([ \n\r]*=[ \n\r]*(?:[^ \n\r"'`<>=]|("|')|))|$)/g,mt=/'/g,vt=/"/g,_t=/^(?:script|style|textarea|title)$/i,Mt=o=>(t,...e)=>({_$litType$:o,strings:t,values:e}),P=Mt(1),T=Symbol.for("lit-noChange"),u=Symbol.for("lit-nothing"),gt=new WeakMap,Rt=(o,t,e)=>{var i,r;const s=(i=e==null?void 0:e.renderBefore)!==null&&i!==void 0?i:t;let n=s._$litPart$;if(n===void 0){const c=(r=e==null?void 0:e.renderBefore)!==null&&r!==void 0?r:null;s._$litPart$=n=new z(t.insertBefore(N(),c),c,void 0,e!=null?e:{})}return n._$AI(o),n},U=C.createTreeWalker(C,129,null,!1),Dt=(o,t)=>{const e=o.length-1,i=[];let r,s=t===2?"<svg>":"",n=O;for(let a=0;a<e;a++){const p=o[a];let l,h,d=-1,f=0;for(;f<p.length&&(n.lastIndex=f,h=n.exec(p),h!==null);)f=n.lastIndex,n===O?h[1]==="!--"?n=ut:h[1]!==void 0?n=ft:h[2]!==void 0?(_t.test(h[2])&&(r=RegExp("</"+h[2],"g")),n=E):h[3]!==void 0&&(n=E):n===E?h[0]===">"?(n=r!=null?r:O,d=-1):h[1]===void 0?d=-2:(d=n.lastIndex-h[2].length,l=h[1],n=h[3]===void 0?E:h[3]==='"'?vt:mt):n===vt||n===mt?n=E:n===ut||n===ft?n=O:(n=E,r=void 0);const $=n===E&&o[a+1].startsWith("/>")?" ":"";s+=n===O?p+Lt:d>=0?(i.push(l),p.slice(0,d)+"$lit$"+p.slice(d)+y+$):p+y+(d===-2?(i.push(void 0),a):$)}const c=s+(o[e]||"<?>")+(t===2?"</svg>":"");if(!Array.isArray(o)||!o.hasOwnProperty("raw"))throw Error("invalid template strings array");return[ct!==void 0?ct.createHTML(c):c,i]};class L{constructor({strings:t,_$litType$:e},i){let r;this.parts=[];let s=0,n=0;const c=t.length-1,a=this.parts,[p,l]=Dt(t,e);if(this.el=L.createElement(p,i),U.currentNode=this.el.content,e===2){const h=this.el.content,d=h.firstChild;d.remove(),h.append(...d.childNodes)}for(;(r=U.nextNode())!==null&&a.length<c;){if(r.nodeType===1){if(r.hasAttributes()){const h=[];for(const d of r.getAttributeNames())if(d.endsWith("$lit$")||d.startsWith(y)){const f=l[n++];if(h.push(d),f!==void 0){const $=r.getAttribute(f.toLowerCase()+"$lit$").split(y),w=/([.?@])?(.*)/.exec(f);a.push({type:1,index:s,name:w[2],strings:$,ctor:w[1]==="."?jt:w[1]==="?"?Bt:w[1]==="@"?qt:M})}else a.push({type:6,index:s})}for(const d of h)r.removeAttribute(d)}if(_t.test(r.tagName)){const h=r.textContent.split(y),d=h.length-1;if(d>0){r.textContent=x?x.emptyScript:"";for(let f=0;f<d;f++)r.append(h[f],N()),U.nextNode(),a.push({type:2,index:++s});r.append(h[d],N())}}}else if(r.nodeType===8)if(r.data===dt)a.push({type:2,index:s});else{let h=-1;for(;(h=r.data.indexOf(y,h+1))!==-1;)a.push({type:7,index:s}),h+=y.length-1}s++}}static createElement(t,e){const i=C.createElement("template");return i.innerHTML=t,i}}function k(o,t,e=o,i){var r,s,n,c;if(t===T)return t;let a=i!==void 0?(r=e._$Cl)===null||r===void 0?void 0:r[i]:e._$Cu;const p=H(t)?void 0:t._$litDirective$;return(a==null?void 0:a.constructor)!==p&&((s=a==null?void 0:a._$AO)===null||s===void 0||s.call(a,!1),p===void 0?a=void 0:(a=new p(o),a._$AT(o,e,i)),i!==void 0?((n=(c=e)._$Cl)!==null&&n!==void 0?n:c._$Cl=[])[i]=a:e._$Cu=a),a!==void 0&&(t=k(o,a._$AS(o,t.values),a,i)),t}class Ft{constructor(t,e){this.v=[],this._$AN=void 0,this._$AD=t,this._$AM=e}get parentNode(){return this._$AM.parentNode}get _$AU(){return this._$AM._$AU}p(t){var e;const{el:{content:i},parts:r}=this._$AD,s=((e=t==null?void 0:t.creationScope)!==null&&e!==void 0?e:C).importNode(i,!0);U.currentNode=s;let n=U.nextNode(),c=0,a=0,p=r[0];for(;p!==void 0;){if(c===p.index){let l;p.type===2?l=new z(n,n.nextSibling,this,t):p.type===1?l=new p.ctor(n,p.name,p.strings,this,t):p.type===6&&(l=new Wt(n,this,t)),this.v.push(l),p=r[++a]}c!==(p==null?void 0:p.index)&&(n=U.nextNode(),c++)}return s}m(t){let e=0;for(const i of this.v)i!==void 0&&(i.strings!==void 0?(i._$AI(t,i,e),e+=i.strings.length-2):i._$AI(t[e])),e++}}class z{constructor(t,e,i,r){var s;this.type=2,this._$AH=u,this._$AN=void 0,this._$AA=t,this._$AB=e,this._$AM=i,this.options=r,this._$Cg=(s=r==null?void 0:r.isConnected)===null||s===void 0||s}get _$AU(){var t,e;return(e=(t=this._$AM)===null||t===void 0?void 0:t._$AU)!==null&&e!==void 0?e:this._$Cg}get parentNode(){let t=this._$AA.parentNode;const e=this._$AM;return e!==void 0&&t.nodeType===11&&(t=e.parentNode),t}get startNode(){return this._$AA}get endNode(){return this._$AB}_$AI(t,e=this){t=k(this,t,e),H(t)?t===u||t==null||t===""?(this._$AH!==u&&this._$AR(),this._$AH=u):t!==this._$AH&&t!==T&&this.$(t):t._$litType$!==void 0?this.T(t):t.nodeType!==void 0?this.k(t):zt(t)?this.S(t):this.$(t)}A(t,e=this._$AB){return this._$AA.parentNode.insertBefore(t,e)}k(t){this._$AH!==t&&(this._$AR(),this._$AH=this.A(t))}$(t){this._$AH!==u&&H(this._$AH)?this._$AA.nextSibling.data=t:this.k(C.createTextNode(t)),this._$AH=t}T(t){var e;const{values:i,_$litType$:r}=t,s=typeof r=="number"?this._$AC(t):(r.el===void 0&&(r.el=L.createElement(r.h,this.options)),r);if(((e=this._$AH)===null||e===void 0?void 0:e._$AD)===s)this._$AH.m(i);else{const n=new Ft(s,this),c=n.p(this.options);n.m(i),this.k(c),this._$AH=n}}_$AC(t){let e=gt.get(t.strings);return e===void 0&>.set(t.strings,e=new L(t)),e}S(t){lt(this._$AH)||(this._$AH=[],this._$AR());const e=this._$AH;let i,r=0;for(const s of t)r===e.length?e.push(i=new z(this.A(N()),this.A(N()),this,this.options)):i=e[r],i._$AI(s),r++;r<e.length&&(this._$AR(i&&i._$AB.nextSibling,r),e.length=r)}_$AR(t=this._$AA.nextSibling,e){var i;for((i=this._$AP)===null||i===void 0||i.call(this,!1,!0,e);t&&t!==this._$AB;){const r=t.nextSibling;t.remove(),t=r}}setConnected(t){var e;this._$AM===void 0&&(this._$Cg=t,(e=this._$AP)===null||e===void 0||e.call(this,t))}}class M{constructor(t,e,i,r,s){this.type=1,this._$AH=u,this._$AN=void 0,this.element=t,this.name=e,this._$AM=r,this.options=s,i.length>2||i[0]!==""||i[1]!==""?(this._$AH=Array(i.length-1).fill(new String),this.strings=i):this._$AH=u}get tagName(){return this.element.tagName}get _$AU(){return this._$AM._$AU}_$AI(t,e=this,i,r){const s=this.strings;let n=!1;if(s===void 0)t=k(this,t,e,0),n=!H(t)||t!==this._$AH&&t!==T,n&&(this._$AH=t);else{const c=t;let a,p;for(t=s[0],a=0;a<s.length-1;a++)p=k(this,c[i+a],e,a),p===T&&(p=this._$AH[a]),n||(n=!H(p)||p!==this._$AH[a]),p===u?t=u:t!==u&&(t+=(p!=null?p:"")+s[a+1]),this._$AH[a]=p}n&&!r&&this.C(t)}C(t){t===u?this.element.removeAttribute(this.name):this.element.setAttribute(this.name,t!=null?t:"")}}class jt extends M{constructor(){super(...arguments),this.type=3}C(t){this.element[this.name]=t===u?void 0:t}}const Vt=x?x.emptyScript:"";class Bt extends M{constructor(){super(...arguments),this.type=4}C(t){t&&t!==u?this.element.setAttribute(this.name,Vt):this.element.removeAttribute(this.name)}}class qt extends M{constructor(t,e,i,r,s){super(t,e,i,r,s),this.type=5}_$AI(t,e=this){var i;if((t=(i=k(this,t,e,0))!==null&&i!==void 0?i:u)===T)return;const r=this._$AH,s=t===u&&r!==u||t.capture!==r.capture||t.once!==r.once||t.passive!==r.passive,n=t!==u&&(r===u||s);s&&this.element.removeEventListener(this.name,this,r),n&&this.element.addEventListener(this.name,this,t),this._$AH=t}handleEvent(t){var e,i;typeof this._$AH=="function"?this._$AH.call((i=(e=this.options)===null||e===void 0?void 0:e.host)!==null&&i!==void 0?i:this.element,t):this._$AH.handleEvent(t)}}class Wt{constructor(t,e,i){this.element=t,this.type=6,this._$AN=void 0,this._$AM=e,this.options=i}get _$AU(){return this._$AM._$AU}_$AI(t){k(this,t)}}const yt=window.litHtmlPolyfillSupport;yt==null||yt(L,z),((W=globalThis.litHtmlVersions)!==null&&W!==void 0?W:globalThis.litHtmlVersions=[]).push("2.2.0");/**
|
|
30
30
|
* @license
|
|
31
31
|
* Copyright 2017 Google LLC
|
|
32
32
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
33
|
-
*/var
|
|
33
|
+
*/var J,K;class I extends S{constructor(){super(...arguments),this.renderOptions={host:this},this._$Dt=void 0}createRenderRoot(){var t,e;const i=super.createRenderRoot();return(t=(e=this.renderOptions).renderBefore)!==null&&t!==void 0||(e.renderBefore=i.firstChild),i}update(t){const e=this.render();this.hasUpdated||(this.renderOptions.isConnected=this.isConnected),super.update(t),this._$Dt=Rt(e,this.renderRoot,this.renderOptions)}connectedCallback(){var t;super.connectedCallback(),(t=this._$Dt)===null||t===void 0||t.setConnected(!0)}disconnectedCallback(){var t;super.disconnectedCallback(),(t=this._$Dt)===null||t===void 0||t.setConnected(!1)}render(){return T}}I.finalized=!0,I._$litElement$=!0,(J=globalThis.litElementHydrateSupport)===null||J===void 0||J.call(globalThis,{LitElement:I});const $t=globalThis.litElementPolyfillSupport;$t==null||$t({LitElement:I}),((K=globalThis.litElementVersions)!==null&&K!==void 0?K:globalThis.litElementVersions=[]).push("3.2.0");function Jt(o){if(!o.dispatchEvent||!o.requestUpdate)throw new Error("Element missing required functions (dispatchEvent/requestUpdate)");return o}const g="__registered_effects";function Kt(o){const t=o;if(t[g])return t;const e=Jt(o),i=e.updated;return t[g]={index:0,count:0,effects:[]},e.updated=r=>(t[g].index=0,i(r)),t}function Yt(o,t){const e=Kt(o),{index:i,count:r}=e[g];return i===r?(e[g].index++,e[g].count++,e[g].effects.push(t),t):(e[g].index++,e[g].effects[i])}function wt(o,t,e){const i=Yt(o,{on:t,observe:["__initial__dirty"]});i.observe.some((r,s)=>e[s]!==r)&&i.on(),i.observe=e}const Zt=(o,t)=>wt(o,t,[]);/**
|
|
34
34
|
* @license
|
|
35
35
|
* Copyright 2021 Google LLC
|
|
36
36
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
37
|
-
*/function
|
|
38
|
-
|
|
37
|
+
*/function Y(o,t,e){return o?t():e==null?void 0:e()}var Gt=Ht`
|
|
38
|
+
:host {
|
|
39
39
|
--shipaid-primary: #002bd6;
|
|
40
40
|
--shipaid-secondary: #0076ff;
|
|
41
|
+
--shipaid-danger: #f44336;
|
|
41
42
|
--shipaid-text: #000000;
|
|
42
43
|
--shipaid-text-muted: #cccccc;
|
|
43
44
|
--shipaid-text-grey: #aaaaaa;
|
|
@@ -48,7 +49,127 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
48
49
|
--shipaid-font-lg: 18px;
|
|
49
50
|
--shipaid-font-regular: 400;
|
|
50
51
|
--shipaid-font-heavy: 700;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
* {
|
|
55
|
+
font-family: var(--shipaid-font);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.error {
|
|
59
|
+
color: var(--shipaid-danger);
|
|
60
|
+
font-size: var(--shipaid-font-sm);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* Popups */
|
|
64
|
+
|
|
65
|
+
.shipaid-popup {
|
|
66
|
+
position: fixed;
|
|
67
|
+
top: 0;
|
|
68
|
+
bottom: 0;
|
|
69
|
+
left: 0;
|
|
70
|
+
right: 0;
|
|
71
|
+
margin: auto;
|
|
72
|
+
max-width: 500px;
|
|
73
|
+
max-height: 100vh;
|
|
74
|
+
height: fit-content;
|
|
75
|
+
border: var(--shipaid-light-grey) 1px solid;
|
|
76
|
+
background-color: #fff;
|
|
77
|
+
z-index: 10000;
|
|
78
|
+
overflow: auto;
|
|
79
|
+
visibility: hidden;
|
|
80
|
+
opacity: 0;
|
|
81
|
+
transition: all 250ms ease-in-out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.shipaid-popup.active {
|
|
85
|
+
visibility: visible;
|
|
86
|
+
opacity: 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@media (max-width: 600px) {
|
|
90
|
+
.shipaid-popup {
|
|
91
|
+
width: 100vw;
|
|
92
|
+
height: 100vh;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
51
95
|
|
|
96
|
+
.shipaid-popup .popup {
|
|
97
|
+
display: inline-flex;
|
|
98
|
+
flex-direction: column;
|
|
99
|
+
gap: 1rem;
|
|
100
|
+
padding: 5rem;
|
|
101
|
+
height: fit-content;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@media (max-width: 600px) {
|
|
105
|
+
.shipaid-popup .popup {
|
|
106
|
+
padding: 3rem;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.popup .popup-close {
|
|
111
|
+
width: fit-content;
|
|
112
|
+
margin-left: auto;
|
|
113
|
+
background-color: transparent;
|
|
114
|
+
cursor: pointer;
|
|
115
|
+
border: none;
|
|
116
|
+
font-family: var(--shipaid-font);
|
|
117
|
+
font-weight: var(--shipaid-font-heavy);
|
|
118
|
+
text-transform: uppercase;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.popup .popup-title {
|
|
122
|
+
font-size: clamp(1.8rem, 2vw + 1rem, 2.8rem);;
|
|
123
|
+
letter-spacing: 1px;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.popup p {
|
|
127
|
+
font-size: clamp(1.125rem, 1vw + 1rem, 1.6rem);
|
|
128
|
+
margin: 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.popup .popup-disclaimer {
|
|
132
|
+
font-size: 10px;
|
|
133
|
+
color: var(--shipaid-text-muted);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.popup .popup-footer {
|
|
137
|
+
display: flex;
|
|
138
|
+
flex-direction: row;
|
|
139
|
+
justify-content: space-between;
|
|
140
|
+
align-items: flex-start;
|
|
141
|
+
margin-top: 2rem;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
@media (max-width: 600px) {
|
|
145
|
+
.popup .popup-footer {
|
|
146
|
+
flex-direction: column;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.popup .popup-footer .footer-links {
|
|
151
|
+
display: flex;
|
|
152
|
+
flex-direction: row;
|
|
153
|
+
gap: 2rem;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.popup .popup-footer .footer-links a {
|
|
157
|
+
font-family: var(--shipaid-font);
|
|
158
|
+
font-size: var(--shipaid-font-sm);
|
|
159
|
+
color: var(--shipaid-text-muted);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.popup .popup-footer .footer-date p {
|
|
163
|
+
font-family: var(--shipaid-font);
|
|
164
|
+
font-weight: var(--shipaid-font-heavy);
|
|
165
|
+
font-size: var(--shipaid-font-sm);
|
|
166
|
+
color: var(--shipaid-text);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
/* Widget */
|
|
171
|
+
|
|
172
|
+
.shipaid-prompt {
|
|
52
173
|
width: 400px;
|
|
53
174
|
font-family: var(--shipaid-font);
|
|
54
175
|
font-size: var(--shipaid-font-base);
|
|
@@ -58,6 +179,13 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
58
179
|
gap: 1rem;
|
|
59
180
|
margin: 2rem 0 2rem auto;
|
|
60
181
|
}
|
|
182
|
+
|
|
183
|
+
@media (max-width: 600px) {
|
|
184
|
+
.shipaid-prompt {
|
|
185
|
+
width: 100%;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
61
189
|
.shipaid-prompt p, .shipaid-prompt a {
|
|
62
190
|
margin: 0;
|
|
63
191
|
line-height: 1;
|
|
@@ -74,6 +202,14 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
74
202
|
height: 55px;
|
|
75
203
|
margin-top: 1rem;
|
|
76
204
|
}
|
|
205
|
+
.shipaid-prompt .prompt-product .prompt-product-image {
|
|
206
|
+
position: relative;
|
|
207
|
+
}
|
|
208
|
+
.shipaid-prompt .prompt-product .prompt-product-image img {
|
|
209
|
+
height: 30px;
|
|
210
|
+
width: auto;
|
|
211
|
+
object-fit: contain;
|
|
212
|
+
}
|
|
77
213
|
.shipaid-prompt .prompt-product .prompt-product-details .prompt-product-details-title {
|
|
78
214
|
font-size: var(--shipaid-font-base);
|
|
79
215
|
font-weight: var(--shipaid-font-heavy);
|
|
@@ -113,6 +249,9 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
113
249
|
cursor: pointer;
|
|
114
250
|
text-decoration: underline;
|
|
115
251
|
font-size: var(--shipaid-font-sm);
|
|
252
|
+
background-color: transparent;
|
|
253
|
+
cursor: pointer;
|
|
254
|
+
border: none;
|
|
116
255
|
}
|
|
117
256
|
.shipaid-prompt .prompt-footer .prompt-footer-badge {
|
|
118
257
|
background-color: var(--shipaid-light-grey);
|
|
@@ -133,26 +272,50 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
133
272
|
color: var(--shipaid-text-grey);
|
|
134
273
|
font-weight: var(--shipaid-font-heavy);
|
|
135
274
|
}
|
|
136
|
-
`,
|
|
275
|
+
`,Xt=Object.defineProperty,Qt=Object.getOwnPropertyDescriptor,v=(o,t,e,i)=>{for(var r=i>1?void 0:i?Qt(t,e):t,s=o.length-1,n;s>=0;s--)(n=o[s])&&(r=(i?n(t,e,r):n(r))||r);return i&&r&&Xt(t,e,r),r},te=(o=>(o.LOADED="shipaid-loaded",o.STATUS_UPDATE="shipaid-protection-status",o))(te||{});const bt=async(o,t)=>{try{const e=await fetch(o,t);if(!e.ok)throw new Error(await e.text());return await e.json()}catch(e){throw console.error(e),new Error("Failed to complete fetch request.")}};let m=class extends I{constructor(){super(...arguments);var o;this._apiEndpoint="/apps/shipaid",this._storeDomain=(o=window.Shopify)==null?void 0:o.shop,this._hasFinishedSetup=!1,this._hasProtectionInCart=!1,this._state={loading:!1,success:null,error:!1},this._popup=null,this._fetch={get:t=>bt(t),post:(t,e)=>bt(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})}}render(){var e;Zt(this,async()=>{var r,s,n,c;const i=document.createElement("link");i.setAttribute("href","https://fonts.googleapis.com/css2?family=Lato&display=swap"),i.setAttribute("rel","stylesheet"),document.head.appendChild(i);try{const[a,p]=await Promise.all([this._fetchShipAidData(),this._fetchCart()]);this._store=a,this._cart=p}catch{this._hasFinishedSetup=!0;return}if(!((s=(r=this._store)==null?void 0:r.plan)!=null&&s.active)){console.warn("No ShipAid plan is active for this store - skipping setup."),this._hasFinishedSetup=!0;return}if(!((c=(n=this._store)==null?void 0:n.protectionSettings)!=null&&c.protectionType)){console.warn("No protection settings for this store - skipping setup."),this._hasFinishedSetup=!0;return}this._hasFinishedSetup=!0,this._dispatchEvent("shipaid-loaded",this._store)}),wt(this,async()=>{var h,d,f,$,w;if(!((h=this._cart)!=null&&h.items))return;const i=await this.calculateProtectionTotal(this._cart),r=this._findProtectionVariant(i);if(this._protectionVariant=r,!(r!=null&&r.id))throw new Error("No matching protection variant found.");const s=(f=(d=this._cart.items)==null?void 0:d.findIndex(At=>{var Z,G,X,Q,tt;return(tt=(Q=(X=(G=(Z=this._store)==null?void 0:Z.protectionSettings)==null?void 0:G.product)==null?void 0:X.variants)==null?void 0:Q.edges)==null?void 0:tt.some(({node:R})=>{var et;return(et=R==null?void 0:R.id)==null?void 0:et.includes(At.variant_id.toString())})}))!=null?f:[],n=($=this._cart)==null?void 0:$.items[s];if(this._hasProtectionInCart=!!n,!n)return;if((w=r.id)!=null&&w.includes(n.id.toString())){this._protectionCartItem=A(b({},n),{index:s,position:s+1});return}const c={id:n.key,quantity:0};await this._fetch.post("/cart/change.js",c);const a={id:r.id.replace("gid://shopify/ProductVariant/",""),quantity:1},p=await this._fetch.post("/cart/add.js",a),l=A(b({},this._cart),{items:[p,...this._cart.items]});this._cart=l,this._dispatchEvent("shipaid-protection-status",{protection:!0,cart:l,lineItem:p})},[this._store,this._cart]);const o=P`
|
|
276
|
+
<div class=${`shipaid-popup ${this._popup==="learn-more"&&"active"}`}>
|
|
277
|
+
<div class="popup">
|
|
278
|
+
<button class="popup-close" @click=${()=>{this._popup=null}}>Close</button>
|
|
279
|
+
<p class="popup-title">Instant Package Protection</p>
|
|
280
|
+
<p>We empower your favorite brands to protect their packages, because every order is precious!</p>
|
|
281
|
+
<p class="popup-title">Shop Care-Free With ShipAid</p>
|
|
282
|
+
<p>Brands and shipment carriers cannot always guarantee your order will arrive at your doorstep safely. Things happen! We allow the brand you are purchasing from to protect your order in the chance of an issue in transit. If you encounter an issue, simply reach out to the brand and they will handle your claim as soon as possible</p>
|
|
283
|
+
<p class="popup-disclaimer">By purchasing this protection, you are agreeing to our Terms Of Service and Privacy Policy. You are not obligated to purchase this protection. This protection is NOT insurance. Purchasing this protection does not guarantee you will be reimbursed for any product or shipping costs. The claim process and decision for compensation is strictly decided by the brand you a purchasing from.</p>
|
|
284
|
+
<div class="popup-footer">
|
|
285
|
+
<div class="footer-links">
|
|
286
|
+
<a>Terms of Service</a>
|
|
287
|
+
<a>Privacy Policy</a>
|
|
288
|
+
</div>
|
|
289
|
+
<div class="footer-date">
|
|
290
|
+
<p>ShipAid © ${new Date().getFullYear()}</p>
|
|
291
|
+
</div>
|
|
292
|
+
</div>
|
|
293
|
+
</div>
|
|
294
|
+
</div>
|
|
295
|
+
`,t=P`
|
|
137
296
|
<div class="shipaid-prompt">
|
|
138
297
|
<p class="prompt-title">${this._hasProtectionInCart?"Active":"Inactive"}</p>
|
|
139
298
|
<div class="prompt-product">
|
|
140
|
-
|
|
141
|
-
<img
|
|
142
|
-
|
|
299
|
+
<div class="prompt-product-image">
|
|
300
|
+
<img
|
|
301
|
+
src="https://uploads-ssl.webflow.com/62295eacde7b7e37b63b0d49/62298401de7b7eeffd3c36c7_Shipaid_emblem-p-500.png"
|
|
302
|
+
alt="ShipAid logo"
|
|
303
|
+
lazy />
|
|
304
|
+
</div>
|
|
143
305
|
<div class="prompt-product-details">
|
|
144
306
|
<p class="prompt-product-details-title">Instant Package Protection</p>
|
|
145
307
|
<p class="prompt-product-details-description">Resolve shipping issues care-free</p>
|
|
146
308
|
</div>
|
|
147
309
|
<div class="prompt-product-actions">
|
|
148
|
-
<p class="prompt-product-actions-price">${
|
|
310
|
+
<p class="prompt-product-actions-price">${((e=this._protectionVariant)==null?void 0:e.price)&&this._currencyFormat(this._protectionVariant.price)}</p>
|
|
149
311
|
<button class="prompt-product-actions-button" @click=${this._updateProtection} ?disabled=${this._state.loading}>
|
|
150
312
|
${this._state.loading?"Loading...":this._hasProtectionInCart?"Remove":"+ Add"}
|
|
151
313
|
</button>
|
|
152
314
|
</div>
|
|
153
315
|
</div>
|
|
316
|
+
${Y(this._state.error,()=>P`<p class="error">${this._state.error}</p>`)}
|
|
154
317
|
<div class="prompt-footer">
|
|
155
|
-
<
|
|
318
|
+
<button class="prompt-footer-about" @click=${()=>{this._popup="learn-more"}}>Learn More</button>
|
|
156
319
|
<a class="prompt-footer-badge" href="/">
|
|
157
320
|
<span>Powered by</span>
|
|
158
321
|
<span class="ship">Ship</span>
|
|
@@ -160,14 +323,8 @@ var vt=Object.defineProperty,mt=Object.defineProperties;var _t=Object.getOwnProp
|
|
|
160
323
|
</a>
|
|
161
324
|
</div>
|
|
162
325
|
</div>
|
|
163
|
-
`;return
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
productId
|
|
169
|
-
productVariantId
|
|
170
|
-
productVariantNumericId
|
|
171
|
-
productPrice
|
|
172
|
-
}
|
|
173
|
-
}`,{data:e}=await this._fetch.post("http://localhost:1337/v1/graphql",{query:t,variables:s});if(!e.store)throw new Error(`Could not find a store for ${this._storeDomain}`);if(!e.store.productVariantNumericId)throw new Error("Missing variant ID for ShipAid product.");return e.store}catch(s){throw console.error(s),new Error(`Could not find a store for ${this._storeDomain}`)}}async _fetchCart(){try{return await this._fetch.get("/cart.js")}catch(s){throw console.error(s),new Error("Could not fetch cart for current domain.")}}hasProtection(){return this._hasProtectionInCart}async addProtection(){var s,t;try{if(!((s=this._store)!=null&&s.productVariantNumericId))throw new Error("Store has not been loaded.");if(!((t=this._cart)!=null&&t.items))throw new Error("Cart has not been loaded.");this._setState("loading");const e={id:this._store.productVariantNumericId,quantity:1},i=await this._fetch.post("/cart/add.js",e),r=P(C({},this._cart),{items:[i,...this._cart.items]});this._cart=r,this._setState("success"),this._dispatchEvent("shipaid-protection-status",{protection:!0,cart:r,lineItem:i})}catch(e){console.error(e),this._setState("error","Failed to add protection to cart - please try again, or contact us for help.")}}async removeProtection(){var s;try{if(!((s=this._store)!=null&&s.productVariantNumericId))throw new Error("Store has not been loaded.");if(!this._protectionProduct)throw new Error("Protection product not found.");this._setState("loading");const t={id:this._protectionProduct.key,quantity:0},e=await this._fetch.post("/cart/change.js",t);this._dispatchEvent("shipaid-protection-status",{protection:!1,cart:e,lineItem:this._protectionProduct}),this._cart=e,this._setState("success")}catch(t){console.error(t),this._setState("error","Failed to add protection to cart - please try again, or contact us for help.")}}};v.styles=Bt,$([g()],v.prototype,"_storeDomain",2),$([g()],v.prototype,"_store",2),$([g()],v.prototype,"_cart",2),$([g()],v.prototype,"_hasFinishedSetup",2),$([g()],v.prototype,"_hasProtectionInCart",2),$([g()],v.prototype,"_protectionProduct",2),$([g()],v.prototype,"_state",2),v=$([yt("shipaid-widget")],v);export{v as ShipAidWidget};
|
|
326
|
+
`;return P`
|
|
327
|
+
<div class="shipaid">
|
|
328
|
+
${Y(this._hasFinishedSetup,()=>{var i,r;return Y((r=(i=this._store)==null?void 0:i.plan)==null?void 0:r.active,()=>[o,t],()=>P``)},()=>P`<p><slot>Loading ShipAid Widget...</slot></p>`)}
|
|
329
|
+
</div>
|
|
330
|
+
`}_currencyFormat(o){var t,e,i,r,s;return new Intl.NumberFormat((e=(t=window.Shopify)==null?void 0:t.locale)!=null?e:void 0,{currency:((i=this._store)==null?void 0:i.currency)||((s=(r=window.Shopify)==null?void 0:r.currency)==null?void 0:s.active)||"USD",style:"currency"}).format(Number(o))}_dispatchEvent(o,t={}){this.dispatchEvent(new CustomEvent(o,{bubbles:!0,composed:!0,detail:t}))}async calculateProtectionTotal(o){var r,s,n,c;if(o||(o=await this._fetchCart()),!o)throw new Error("Could not fetch cart.");const t=(r=this._store)==null?void 0:r.protectionSettings;if(!t||!t.product)throw new Error("Tried to find protection variant, but protection settings for this store are missing.");const e=((n=(s=o.items)==null?void 0:s.filter(a=>{var p,l,h;return(h=(l=(p=t.product)==null?void 0:p.variants)==null?void 0:l.edges)==null?void 0:h.some(({node:d})=>{var f;return(f=d==null?void 0:d.id)==null?void 0:f.includes(a.variant_id.toString())})}))!=null?n:[]).reduce((a,p)=>a+=p.final_line_price,0),i=o.total_price-e;if(t.protectionType==="FIXED"){if(!t.defaultFee)throw new Error("Missing default fee amount.");if(!((c=t.rules)!=null&&c.length))return t.defaultFee;const a=i/100,p=t.rules.sort((l,h)=>!l.rangeLower||!h.rangeLower?0:l.rangeLower-h.rangeLower).find(l=>{const h=Boolean(l.rangeLower&&l.rangeLower<a);return l.rangeUpper?h&&l.rangeUpper>=a:h});return typeof(p==null?void 0:p.fee)=="number"?p.fee:t.defaultFee}if(t.protectionType==="PERCENTAGE"){const a=i*t.percentage/100;return a>=t.minimumFee?a:t.minimumFee}throw new Error("No protection type handler found for this store.")}_findProtectionVariant(o){var e,i,r;const t=(e=this._store)==null?void 0:e.protectionSettings;if(!t||!((r=(i=t.product)==null?void 0:i.variants)!=null&&r.edges))throw new Error("Missing product and variants from protection settings.");return t.product.variants.edges.flatMap(({node:s})=>{if(!s||!s.price)return[];const n=Number(s.price);return[A(b({},s),{formattedPrice:n})]}).sort((s,n)=>s.formattedPrice-n.formattedPrice).find(s=>s.formattedPrice>=o)}_setState(o,t){this._state={loading:o==="loading",success:o==="success",error:o==="error"?t||!0:!1}}async _updateProtection(){return this._hasProtectionInCart?this.removeProtection():this.addProtection()}async _fetchShipAidData(){try{const o=new URL(window.location.href);o.pathname=this._apiEndpoint;const t=await this._fetch.get(o.toString());if(!t)throw new Error(`Failed to fetch store data for ${o.toString()}`);const[e]=t.stores;if(!e)throw new Error(`Could not find a store for this URL ${o.toString()}.`);return e}catch(o){throw console.error(o),new Error(`Could not find a store for ${this._storeDomain}`)}}async _fetchCart(){try{return await this._fetch.get("/cart.js")}catch(o){throw console.error(o),new Error("Could not fetch cart for current domain.")}}hasProtection(){return this._hasProtectionInCart}async updateCart(o){o||(o=await this._fetchCart()),this._cart=o}async addProtection(){var o,t;try{if(!this._store)throw new Error("Store has not been loaded.");if(!((o=this._cart)!=null&&o.items))throw new Error("Cart has not been loaded.");if(!((t=this._protectionVariant)!=null&&t.id))throw new Error("No protection variant found.");this._setState("loading");const e={id:this._protectionVariant.id.replace("gid://shopify/ProductVariant/",""),quantity:1},i=await this._fetch.post("/cart/add.js",e),r=A(b({},this._cart),{items:[i,...this._cart.items]});this._cart=r,this._setState("success"),this._dispatchEvent("shipaid-protection-status",{protection:!0,cart:r,lineItem:i})}catch(e){console.error(e),this._setState("error","Failed to add protection to cart - please try again, or contact us for help.")}}async removeProtection(){try{if(!this._store)throw new Error("Store has not been loaded.");if(!this._protectionCartItem)throw new Error("Protection product not found.");this._setState("loading");const o={id:this._protectionCartItem.key,quantity:0},t=await this._fetch.post("/cart/change.js",o);this._dispatchEvent("shipaid-protection-status",{protection:!1,cart:t,lineItem:this._protectionCartItem}),this._cart=t,this._setState("success")}catch(o){console.error(o),this._setState("error","Failed to add protection to cart - please try again, or contact us for help.")}}};m.styles=Gt,v([_()],m.prototype,"_storeDomain",2),v([_()],m.prototype,"_store",2),v([_()],m.prototype,"_cart",2),v([_()],m.prototype,"_hasFinishedSetup",2),v([_()],m.prototype,"_hasProtectionInCart",2),v([_()],m.prototype,"_protectionCartItem",2),v([_()],m.prototype,"_protectionVariant",2),v([_()],m.prototype,"_state",2),v([_()],m.prototype,"_popup",2),m=v([Tt("shipaid-widget")],m);export{m as ShipAidWidget};
|