1 line
3.9 KiB
JavaScript
1 line
3.9 KiB
JavaScript
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[131],{4058:function(t,e,n){Promise.resolve().then(n.bind(n,9229))},9229:function(t,e,n){"use strict";n.r(e),n.d(e,{default:function(){return c}});var r=n(7437),o=n(2265);function c(t){let{params:e}=t,{accountId:n}=e,[c,i]=(0,o.useState)([]),[a,s]=(0,o.useState)(!1),[d,l]=(0,o.useState)(""),[u,p]=(0,o.useState)("");async function h(t){if(!t.unitAmount||!t.currency||!u){l("Product is missing a price.");return}s(!0),l("");try{let e=await fetch("/api/connect/checkout",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({accountId:u,name:t.name,description:t.description,unitAmount:t.unitAmount,currency:t.currency,quantity:1})}),n=await e.json();if(!e.ok)throw Error(n.error||"Failed to start checkout");window.location.href=n.url}catch(t){l(t.message||"Checkout failed.")}finally{s(!1)}}async function m(t){if(!t.unitAmount||!t.currency||!t.stripePriceId){l("Product is missing a price.");return}s(!0),l("");try{let e=await fetch("/api/cart/add",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({slug:n,productId:t.id,priceId:t.stripePriceId,name:t.name,unitAmount:t.unitAmount,currency:t.currency,quantity:1})}),r=await e.json();if(!e.ok)throw Error(r.error||"Failed to add to cart");l("Added to cart.")}catch(t){l(t.message||"Failed to add to cart.")}finally{s(!1)}}return(0,o.useEffect)(()=>{n&&window.localStorage.setItem("connectStoreSlug",n),async function(){s(!0),l("");try{var t;let e=await fetch("/api/connect/account/lookup?slug=".concat(n)),r=await e.json();if(!e.ok)throw Error(r.error||"Store not found");p(r.accountId);let o=await fetch("/api/storefront/products?slug=".concat(n)),c=await o.json();if(!o.ok)throw Error(c.error||"Failed to load products");let a=(null===(t=c.products)||void 0===t?void 0:t.map(t=>{var e,n,r,o,c,i,a,s,d,l;return{id:null!==(o=t.stripeProductId)&&void 0!==o?o:t.id,name:t.name,description:t.description,unitAmount:null!==(i=null!==(c=t.unitAmount)&&void 0!==c?c:null===(e=t.default_price)||void 0===e?void 0:e.unit_amount)&&void 0!==i?i:null,currency:null!==(s=null!==(a=t.currency)&&void 0!==a?a:null===(n=t.default_price)||void 0===n?void 0:n.currency)&&void 0!==s?s:null,stripePriceId:null!==(l=null!==(d=t.stripePriceId)&&void 0!==d?d:null===(r=t.default_price)||void 0===r?void 0:r.id)&&void 0!==l?l:null}}))||[];i(a)}catch(t){l(t.message||"Failed to load products.")}finally{s(!1)}}()},[n]),(0,r.jsx)("main",{className:"storefront-page",children:(0,r.jsx)("section",{className:"section",children:(0,r.jsxs)("div",{className:"container storefront-page__inner",children:[(0,r.jsx)("h1",{className:"page-title",children:"Storefront"}),(0,r.jsxs)("p",{className:"storefront-muted",children:["Store: ",n," (mapped to a connected account)"]}),d?(0,r.jsx)("div",{className:"connect-message",children:d}):null,(0,r.jsxs)("div",{className:"storefront-grid",children:[c.map(t=>{var e;return(0,r.jsxs)("div",{className:"storefront-card",children:[(0,r.jsx)("h3",{children:t.name}),(0,r.jsx)("p",{children:t.description||"No description provided."}),(0,r.jsx)("div",{className:"storefront-price",children:t.unitAmount?"".concat((t.unitAmount/100).toFixed(2)," ").concat((null===(e=t.currency)||void 0===e?void 0:e.toUpperCase())||"USD"):"No price"}),(0,r.jsxs)("div",{className:"storefront-actions",children:[(0,r.jsx)("button",{className:"btn",onClick:()=>h(t),disabled:a,children:"Buy now (Checkout)"}),(0,r.jsx)("a",{className:"btn btn--ghost",href:"/storefront/".concat(n,"/pay?productId=").concat(t.id),children:"Pay with card"}),(0,r.jsx)("button",{className:"btn btn--ghost",onClick:()=>m(t),disabled:a,children:"Add to cart"}),(0,r.jsx)("a",{className:"btn btn--ghost",href:"/cart",children:"View cart"})]})]},t.id)}),c.length||a?null:(0,r.jsx)("div",{className:"storefront-muted",children:"No products yet."})]})]})})})}}},function(t){t.O(0,[971,117,744],function(){return t(t.s=4058)}),_N_E=t.O()}]); |