Skip to content

Commit fc9bfe1

Browse files
committed
docs
1 parent 74bc5e0 commit fc9bfe1

3 files changed

Lines changed: 38 additions & 41 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function extractCreemProductId(url) {
2+
if (!url || typeof url !== "string") return null
3+
const match = url.match(/\/payment\/(prod_[^/?#]+)/)
4+
return match ? match[1] : null
5+
}
6+
7+
export function getProductCreemIds(product) {
8+
const ids = []
9+
10+
if (product?.buy_now_url) {
11+
const id = extractCreemProductId(product.buy_now_url)
12+
if (id) ids.push(id)
13+
}
14+
15+
if (product?.packages) {
16+
const checkoutRow = product.packages.slice(1).find((row) => row[0] === "checkout")
17+
if (checkoutRow) {
18+
for (const url of checkoutRow.slice(1)) {
19+
const id = extractCreemProductId(url)
20+
if (id) ids.push(id)
21+
}
22+
}
23+
}
24+
25+
return ids
26+
}
27+
28+
export function isDiscountApplicableToProduct(discount, productIds) {
29+
const appliesTo = discount?.data?.attributes?.applies_to_products
30+
if (!appliesTo?.length) return true
31+
return appliesTo.some((id) => productIds.includes(id))
32+
}

packages/docs/src/routes/(routes)/store/+page.svelte

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import StoreProduct from "$components/StoreProduct.svelte"
66
import Countdown from "svelte-countdown"
77
import { fade, slide } from "svelte/transition"
8+
import { getProductCreemIds, isDiscountApplicableToProduct } from "$lib/storeDiscount.js"
89
let { data } = $props()
910
let currentDate = $state(new Date().toISOString())
1011
$effect(() => {
@@ -23,25 +24,6 @@
2324
return false
2425
}
2526
26-
function extractCreemProductId(url) {
27-
if (!url || typeof url !== "string") return null
28-
const match = url.match(/\/payment\/(prod_[^/?#]+)/)
29-
return match ? match[1] : null
30-
}
31-
32-
function getCheckoutProductIds(packages) {
33-
if (!packages) return []
34-
const checkoutRow = packages.slice(1).find((row) => row[0] === "checkout")
35-
if (!checkoutRow) return []
36-
return checkoutRow.slice(1).map(extractCreemProductId).filter(Boolean)
37-
}
38-
39-
function isDiscountApplicableToProduct(discount, checkoutProductIds) {
40-
const appliesTo = discount?.data?.attributes?.applies_to_products
41-
if (!appliesTo?.length) return true
42-
return appliesTo.some((id) => checkoutProductIds.includes(id))
43-
}
44-
4527
let discount = $state(null)
4628
const fetchDiscount = (async () => {
4729
// Fetch both discount types
@@ -74,8 +56,8 @@
7456
function getProductDiscount(product) {
7557
if (!discount?.data?.attributes?.expires_at) return null
7658
if (new Date(discount.data.attributes.expires_at).toISOString() <= currentDate) return null
77-
const checkoutProductIds = getCheckoutProductIds(product.packages)
78-
if (!isDiscountApplicableToProduct(discount, checkoutProductIds)) return null
59+
const productIds = getProductCreemIds(product)
60+
if (!isDiscountApplicableToProduct(discount, productIds)) return null
7961
return discount.data.attributes
8062
}
8163

packages/docs/src/routes/(routes)/store/[productId]/+page.svelte

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import StoreProduct from "$components/StoreProduct.svelte"
55
import Countdown from "svelte-countdown"
66
import { fade, slide } from "svelte/transition"
7+
import { getProductCreemIds, isDiscountApplicableToProduct } from "$lib/storeDiscount.js"
78
89
let { data } = $props()
910
@@ -151,24 +152,6 @@
151152
return false
152153
}
153154
154-
function extractCreemProductId(url) {
155-
if (!url || typeof url !== "string") return null
156-
const match = url.match(/\/payment\/(prod_[^/?#]+)/)
157-
return match ? match[1] : null
158-
}
159-
160-
function getCheckoutProductIds(packages) {
161-
if (!packages) return []
162-
const checkoutRow = packages.slice(1).find((row) => row[0] === "checkout")
163-
if (!checkoutRow) return []
164-
return checkoutRow.slice(1).map(extractCreemProductId).filter(Boolean)
165-
}
166-
167-
function isDiscountApplicableToProduct(discount, checkoutProductIds) {
168-
const appliesTo = discount?.data?.attributes?.applies_to_products
169-
if (!appliesTo?.length) return true
170-
return appliesTo.some((id) => checkoutProductIds.includes(id))
171-
}
172155
const fetchDiscount = (async () => {
173156
// Fetch both discount types
174157
const [shorttimeDiscountResponse, specialDiscountResponse] = await Promise.all([
@@ -237,8 +220,8 @@
237220
</div>
238221
239222
{#await fetchDiscount then discount}
240-
{@const checkoutProductIds = getCheckoutProductIds(data.product.packages)}
241-
{#if discount?.data.attributes.expires_at && new Date(discount?.data.attributes.expires_at).toISOString() > currentDate && isDiscountApplicableToProduct(discount, checkoutProductIds)}
223+
{@const productIds = getProductCreemIds(data.product)}
224+
{#if discount?.data.attributes.expires_at && new Date(discount?.data.attributes.expires_at).toISOString() > currentDate && isDiscountApplicableToProduct(discount, productIds)}
242225
<div class="my-12">
243226
<div
244227
class="alert bg-neutral text-neutral-content min-h-24 border-transparent"

0 commit comments

Comments
 (0)