JezK
Edit File: sc-orders-list.js
import{proxyCustomElement,HTMLElement,h}from"@stencil/core/internal/client";import"./fetch.js";import{o as onFirstVisible}from"./lazy.js";import{d as defineCustomElement$k}from"./sc-alert2.js";import{d as defineCustomElement$j}from"./sc-block-ui2.js";import{d as defineCustomElement$i}from"./sc-button2.js";import{d as defineCustomElement$h}from"./sc-card2.js";import{d as defineCustomElement$g}from"./sc-dashboard-module2.js";import{d as defineCustomElement$f}from"./sc-divider2.js";import{d as defineCustomElement$e}from"./sc-empty2.js";import{d as defineCustomElement$d}from"./sc-flex2.js";import{d as defineCustomElement$c}from"./sc-icon2.js";import{d as defineCustomElement$b}from"./sc-order-shipment-badge2.js";import{d as defineCustomElement$a}from"./sc-order-status-badge2.js";import{d as defineCustomElement$9}from"./sc-pagination2.js";import{d as defineCustomElement$8}from"./sc-skeleton2.js";import{d as defineCustomElement$7}from"./sc-spinner2.js";import{d as defineCustomElement$6}from"./sc-stacked-list2.js";import{d as defineCustomElement$5}from"./sc-stacked-list-row2.js";import{d as defineCustomElement$4}from"./sc-tag2.js";import{d as defineCustomElement$3}from"./sc-text2.js";import{d as defineCustomElement$2}from"./sc-visually-hidden2.js";import{a as addQueryArgs}from"./add-query-args.js";import{a as apiFetch}from"./index4.js";const scOrdersListCss=":host{display:block}.orders-list{display:grid;gap:0.75em}.orders-list__status{display:flex;align-items:center;gap:var(--sc-spacing-x-small)}.orders-list__heading{display:flex;flex-wrap:wrap;align-items:flex-end;justify-content:space-between}.orders-list__title{font-size:var(--sc-font-size-x-large);font-weight:var(--sc-font-weight-bold);line-height:var(--sc-line-height-dense)}.orders-list a{text-decoration:none;font-weight:var(--sc-font-weight-semibold);display:inline-flex;align-items:center;gap:0.25em;color:var(--sc-color-primary-500)}.order__row{color:var(--sc-color-gray-800);text-decoration:none;display:grid;align-items:center;justify-content:space-between;gap:0;grid-template-columns:1fr 1fr 1fr auto;margin:0;padding:var(--sc-spacing-small) var(--sc-spacing-large)}.order__row:not(:last-child){border-bottom:1px solid var(--sc-color-gray-200)}.order__row:hover{background:var(--sc-color-gray-50)}.order__date{font-weight:var(--sc-font-weight-semibold)}",ScOrdersListStyle0=scOrdersListCss,ScOrdersList$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.query={page:1,per_page:10},this.allLink=void 0,this.heading=void 0,this.isCustomer=void 0,this.orders=[],this.loading=void 0,this.busy=void 0,this.error=void 0,this.pagination={total:0,total_pages:0}}componentWillLoad(){onFirstVisible(this.el,(()=>{this.initialFetch()}))}async initialFetch(){try{this.loading=!0,await this.getOrders()}catch(e){console.error(this.error),this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.loading=!1}}async fetchOrders(){try{this.busy=!0,await this.getOrders()}catch(e){console.error(this.error),this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.busy=!1}}async getOrders(){if(!this.isCustomer)return;const e=await await apiFetch({path:addQueryArgs("surecart/v1/orders/",{expand:["checkout","checkout.line_items","checkout.charge"],...this.query}),parse:!1});return this.pagination={total:parseInt(e.headers.get("X-WP-Total")),total_pages:parseInt(e.headers.get("X-WP-TotalPages"))},this.orders=await e.json(),this.orders}nextPage(){this.query.page=this.query.page+1,this.fetchOrders()}prevPage(){this.query.page=this.query.page-1,this.fetchOrders()}renderStatusBadge(e){const{status:t,checkout:s}=e,{charge:r}=s;if(r&&"object"==typeof r){if(null==r?void 0:r.fully_refunded)return h("sc-tag",{type:"danger"},wp.i18n.__("Refunded","surecart"));if(null==r?void 0:r.refunded_amount)return h("sc-tag",{type:"info"},wp.i18n.__("Partially Refunded","surecart"))}return h("sc-order-status-badge",{status:t})}renderLoading(){return h("sc-card",{noPadding:!0},h("sc-stacked-list",null,h("sc-stacked-list-row",{style:{"--columns":"4"},"mobile-size":500},[...Array(4)].map((()=>h("sc-skeleton",{style:{width:"100px",display:"inline-block"}}))))))}renderEmpty(){return h("div",null,h("sc-divider",{style:{"--spacing":"0"}}),h("slot",{name:"empty"},h("sc-empty",{icon:"shopping-bag"},wp.i18n.__("You don't have any orders.","surecart"))))}renderList(){return this.orders.map((e=>{const{checkout:t,created_at_date:s,id:r}=e;if(!t)return null;const{amount_due_display_amount:i,charge:n,delivered_items_count:a}=t,o=a||0;return h("sc-stacked-list-row",{href:addQueryArgs(window.location.href,{action:"show",model:"order",id:r}),style:{"--columns":"4"},"mobile-size":500},h("div",{class:"order__date"},"string"!=typeof n&&((null==n?void 0:n.created_at_date)||s)),h("div",null,h("sc-text",{truncate:!0,style:{"--color":"var(--sc-color-gray-500)"}},wp.i18n.sprintf(wp.i18n._n("%s item","%s items",o,"surecart"),o))),h("div",{class:"orders-list__status"},this.renderStatusBadge(e),h("sc-order-shipment-badge",{status:null==e?void 0:e.shipment_status})),h("div",null,i))}))}renderContent(){var e;return this.loading?this.renderLoading():0===(null===(e=this.orders)||void 0===e?void 0:e.length)?this.renderEmpty():h("sc-card",{"no-padding":!0},h("sc-stacked-list",null,this.renderList()))}render(){var e,t;return h("sc-dashboard-module",{key:"49e3e9f72ccb4b87b39db259c6474f02a4b6b5c1",class:"orders-list",error:this.error},h("span",{key:"02b600eff77b0780d16da94c778d9f8b0b206db4",slot:"heading"},h("slot",{key:"aab70df7f0eec3dd1b52004fde6a31ed28eb209c",name:"heading"},this.heading||wp.i18n.__("Order History","surecart"))),!!this.allLink&&!!(null===(e=this.orders)||void 0===e?void 0:e.length)&&h("sc-button",{key:"f0c54a01429e0cb8f990bf453c1e85a8274851c8",type:"link",href:this.allLink,slot:"end","aria-label":wp.i18n.sprintf(wp.i18n.__("View all %s","surecart"),this.heading||wp.i18n.__("Order History","surecart"))},wp.i18n.__("View all","surecart"),h("sc-icon",{key:"fc3dba1185452a05f01a4ac8afed34b56cd99b13","aria-hidden":"true",name:"chevron-right",slot:"suffix"})),this.renderContent(),!this.allLink&&h("sc-pagination",{key:"bb7f8512622c4854373a3f1dc344f3d813bbe2f5",page:this.query.page,perPage:this.query.per_page,total:this.pagination.total,totalPages:this.pagination.total_pages,totalShowing:null===(t=null==this?void 0:this.orders)||void 0===t?void 0:t.length,onScNextPage:()=>this.nextPage(),onScPrevPage:()=>this.prevPage()}),this.busy&&h("sc-block-ui",{key:"cc6ad791df48597401da143e261e716f97c0b311"}))}get el(){return this}static get style(){return ScOrdersListStyle0}},[1,"sc-orders-list",{query:[1040],allLink:[1,"all-link"],heading:[1],isCustomer:[4,"is-customer"],orders:[32],loading:[32],busy:[32],error:[32],pagination:[32]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-orders-list","sc-alert","sc-block-ui","sc-button","sc-card","sc-dashboard-module","sc-divider","sc-empty","sc-flex","sc-icon","sc-order-shipment-badge","sc-order-status-badge","sc-pagination","sc-skeleton","sc-spinner","sc-stacked-list","sc-stacked-list-row","sc-tag","sc-text","sc-visually-hidden"].forEach((e=>{switch(e){case"sc-orders-list":customElements.get(e)||customElements.define(e,ScOrdersList$1);break;case"sc-alert":customElements.get(e)||defineCustomElement$k();break;case"sc-block-ui":customElements.get(e)||defineCustomElement$j();break;case"sc-button":customElements.get(e)||defineCustomElement$i();break;case"sc-card":customElements.get(e)||defineCustomElement$h();break;case"sc-dashboard-module":customElements.get(e)||defineCustomElement$g();break;case"sc-divider":customElements.get(e)||defineCustomElement$f();break;case"sc-empty":customElements.get(e)||defineCustomElement$e();break;case"sc-flex":customElements.get(e)||defineCustomElement$d();break;case"sc-icon":customElements.get(e)||defineCustomElement$c();break;case"sc-order-shipment-badge":customElements.get(e)||defineCustomElement$b();break;case"sc-order-status-badge":customElements.get(e)||defineCustomElement$a();break;case"sc-pagination":customElements.get(e)||defineCustomElement$9();break;case"sc-skeleton":customElements.get(e)||defineCustomElement$8();break;case"sc-spinner":customElements.get(e)||defineCustomElement$7();break;case"sc-stacked-list":customElements.get(e)||defineCustomElement$6();break;case"sc-stacked-list-row":customElements.get(e)||defineCustomElement$5();break;case"sc-tag":customElements.get(e)||defineCustomElement$4();break;case"sc-text":customElements.get(e)||defineCustomElement$3();break;case"sc-visually-hidden":customElements.get(e)||defineCustomElement$2()}}))}const ScOrdersList=ScOrdersList$1,defineCustomElement=defineCustomElement$1;export{ScOrdersList,defineCustomElement};