Products
${list('products', (p: Product) => html`-
${p.name} : {
const existing = self.data.cart.find(li => li.productId === p.id);
if (existing) {
const idx = self.data.cart.indexOf(existing);
self.data.cart.splice(idx, 1, {
...existing,
quantity: existing.quantity + 1
});
} else {
self.data.cart.push({
productId: p.id,
productName: p.name,
price: `${renderAmount(p.unitAmount)}`,
quantity: 1
});
}
}}
>add
`)}
Cart
${list('cart', (li: LineItem) => html`-
${li.productName} x ${li.quantity} : {
self.data.cart.splice(self.data.cart.indexOf(li), 1);
}}
>remove
`)}
Transactions
| date |
amount |
items |
${list('transactions', (t: Transaction) => html`
| ${new Date(t.createdAt).toLocaleDateString()} |
${renderAmount(t.amount)} |
${(t.items || []).map(li => html`
| ${li.description} |
x ${li.quantity} |
= ${renderAmount(li.amount)} |
`)}
|
`)}
Subscription Plans
| Name |
Price |
|
${list('plans', (p: Product) => html`
| ${p.name} |
${renderAmount(p.unitAmount)} |
per ${p.interval} |
{
self.data.planCart.push(p);
self.data.plans.splice(self.data.plans.indexOf(p), 1);
}}
>add
|
`)}
Subscription Cart
| Name |
Price |
|
${list('planCart', (p: Product) => html`
| ${p.name} |
${renderAmount(p.unitAmount)} |
per ${p.interval} |
{
self.data.planCart.splice(self.data.planCart.indexOf(p), 1);
self.data.plans.push(p);
}}
>remove |
`)}
Active Subscriptions
| Plan |
Quantity |
Amount |
|
${list('subscriptions', (s: SubscriptionLine) => html`
| ${s.name} |
${s.quantity} |
x |
${renderAmount(s.amount)} |
per ${s.interval} |
{
const yes = confirm("Are you sure?");
if (!yes) return;
try {
await store.cancelSubscription(null, s.id);
self.data.subscriptions.splice(
self.data.subscriptions.indexOf(s), 1
);
} catch (error) {
alert(error);
}
}}
>cancel |
`)}
`.onadd(async self => {
const products = await store.listProducts(null);
self.data.products = products.filter(p => p.type === 'one_time');
self.data.plans = products.filter(p => p.type !== 'one_time');
self.data.transactions = await store.listPayments(null);
self.data.subscriptions = await store.listSubscriptions(null);
});
return self;
}
async function App() {
const self = html`
${await AuthenticatedContent({
authenticated: () => Storefront(),
unauthenticated: () => html`
You need to sign in to buy things.
`
})}
`;
return self;
}
export async function generate() {
return Main({
pageTitle: 'Simple Storefront Demo',
content: await App(),
});
}
export function onload() {
hydrate('app', App as any);
}