ferst-core 0.11.0 → 0.11.1

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.
@@ -2,15 +2,16 @@
2
2
  /**
3
3
  * Pay — a payment for one of the organisation's items from the Ferst Payments module ("Course fee",
4
4
  * £40). The item's name and price are read from the platform at build time (lib/pay) and again in
5
- * the browser, so a new price or a sale shows without a rebuild; the page never says what to
6
- * charge. The visitor gives a name and email; the form posts to the platform, which sends them to
5
+ * the browser, so a new name, a new price or a sale shows (and a sale ends) without a rebuild; the
6
+ * page never says what to charge. A sale shows as a sale, in one standard form: the usual price
7
+ * struck through, the sale price, and a "Sale" tag with its last day. The visitor gives a name and email; the form posts to the platform, which sends them to
7
8
  * Stripe's page on the organisation's own account, and Stripe brings them back here with
8
9
  * `?payment=done` or `?payment=cancelled` (and this block's anchor), where the block says so.
9
10
  * Works without JavaScript: a plain form post, answered with a redirect to Stripe.
10
11
  */
11
12
  import ContactForm from './ContactForm.astro';
12
13
  import Heading from '../blocks/Heading.astro';
13
- import { formatPrice, loadPayItem } from '../../lib/pay';
14
+ import { formatPrice, loadPayItem, saleLine } from '../../lib/pay';
14
15
  import { resolveGitBase, resolveSiteId } from '../../lib/git-base.mjs';
15
16
 
16
17
  interface Props {
@@ -24,14 +25,20 @@ const { item, title, intro, note, buttonLabel = 'Pay' } = Astro.props;
24
25
  const def = await loadPayItem(item, { base: resolveGitBase(), siteId: resolveSiteId() });
25
26
  const anchor = `pay-${def.id}`;
26
27
  const price = formatPrice(def.amount, def.currency);
28
+ const was = def.sale ? formatPrice(def.sale.usualAmount, def.currency) : '';
29
+ const saleText = def.sale ? saleLine(def.sale.lastDay) : '';
27
30
  const { align: blockAlign, headingAlign } = Astro.props as { align?: 'start' | 'center' | 'end'; headingAlign?: 'start' | 'center' | 'end' };
28
31
  const headAlign = headingAlign ?? blockAlign ?? 'center';
29
32
  ---
30
33
 
31
- <section class="b-pay" id={anchor} data-pay={def.definition} data-label={buttonLabel} data-amount={def.amount}>
34
+ <section class="b-pay" id={anchor} data-pay={def.definition} data-label={buttonLabel} data-item-title={title ? undefined : ''}>
32
35
  <div class="b-pay__head" data-recipe-head data-content-align={headAlign}>
33
36
  <Heading text={title ?? def.title} level={2} />
34
- <p class="b-pay__price" data-pay-price>{price}</p>
37
+ <p class="b-pay__price">
38
+ <del class="b-pay__was" data-pay-was hidden={!def.sale}><span class="b-pay__sr">Usual price: </span><span data-pay-was-amount>{was}</span></del>
39
+ <span><span class="b-pay__sr" data-pay-now-label hidden={!def.sale}>Sale price: </span><span data-pay-now>{price}</span></span>
40
+ </p>
41
+ <p class="b-pay__sale" data-pay-sale hidden={!def.sale}><span class="b-pay__tag">Sale</span> <span data-pay-sale-text>{saleText}</span></p>
35
42
  {intro && <p class="b-pay__intro">{intro}</p>}
36
43
  </div>
37
44
  <div class="b-pay__notice b-pay__notice--done" role="status" data-pay-done hidden>
@@ -59,11 +66,7 @@ const headAlign = headingAlign ?? blockAlign ?? 'center';
59
66
  </section>
60
67
 
61
68
  <script>
62
- const price = (amount: number, currency: string) => {
63
- const units = amount / 100;
64
- const text = Number.isInteger(units) ? String(units) : units.toFixed(2);
65
- return currency.toLowerCase() === 'gbp' ? `£${text}` : `${text} ${currency.toUpperCase()}`;
66
- };
69
+ import { formatPrice, readSale, saleLine, type PayItemAnswer } from '../../lib/pay';
67
70
 
68
71
  function setUp(block: HTMLElement, onlyOne: boolean) {
69
72
  // Back from Stripe: say how it went, on the block that was paid (its anchor), then tidy the address.
@@ -75,7 +78,8 @@ const headAlign = headingAlign ?? blockAlign ?? 'center';
75
78
  url.searchParams.delete('payment');
76
79
  history.replaceState(history.state, '', url);
77
80
  }
78
- // The price now (a new price or a sale since the build); gone from offer, the form is closed.
81
+ // The name and price now (renamed, repriced, or a sale begun or ended since the build); gone
82
+ // from offer, the form is closed.
79
83
  const src = block.dataset.pay;
80
84
  if (!src) return;
81
85
  fetch(src, { headers: { Accept: 'application/json' } })
@@ -87,13 +91,22 @@ const headAlign = headingAlign ?? blockAlign ?? 'center';
87
91
  return;
88
92
  }
89
93
  if (!res.ok) return;
90
- const d = (await res.json()) as { amount: number; currency?: string };
91
- if (String(d.amount) === block.dataset.amount) return;
92
- const text = price(d.amount, d.currency ?? 'gbp');
93
- const shown = block.querySelector<HTMLElement>('[data-pay-price]');
94
- if (shown) shown.textContent = text;
94
+ const d = (await res.json()) as PayItemAnswer;
95
+ const currency = d.currency ?? 'gbp';
96
+ const text = formatPrice(d.amount, currency);
97
+ const sale = readSale(d);
98
+ const set = (sel: string, value: string) => { const el = block.querySelector<HTMLElement>(sel); if (el && el.textContent !== value) el.textContent = value; };
99
+ const show = (sel: string, on: boolean) => { const el = block.querySelector<HTMLElement>(sel); if (el) el.hidden = !on; };
100
+ set('[data-pay-now]', text);
101
+ if (sale) {
102
+ set('[data-pay-was-amount]', formatPrice(sale.usualAmount, currency));
103
+ set('[data-pay-sale-text]', saleLine(sale.lastDay));
104
+ }
105
+ ['[data-pay-was]', '[data-pay-now-label]', '[data-pay-sale]'].forEach((sel) => show(sel, !!sale));
95
106
  const button = form?.querySelector<HTMLButtonElement>('button[type="submit"]');
96
- if (button) button.textContent = `${block.dataset.label ?? 'Pay'} ${text}`;
107
+ if (button) { const label = `${block.dataset.label ?? 'Pay'} ${text}`; if (button.textContent !== label) button.textContent = label; }
108
+ // The item's own name, unless the block gives its own title.
109
+ if (block.dataset.itemTitle !== undefined && d.title) set('.b-pay__head .b-heading', d.title);
97
110
  })
98
111
  .catch(() => { /* the price from the build stands; the platform reads the real one when they pay */ });
99
112
  }
@@ -127,6 +140,46 @@ const headAlign = headingAlign ?? blockAlign ?? 'center';
127
140
  color: var(--fg);
128
141
  font-variant-numeric: tabular-nums;
129
142
  }
143
+ .b-pay__was {
144
+ margin-inline-end: 0.5rem;
145
+ font-size: 0.6em;
146
+ font-weight: 500;
147
+ color: var(--muted);
148
+ text-decoration-thickness: 0.08em;
149
+ }
150
+ .b-pay__sale {
151
+ margin: 0;
152
+ color: var(--muted);
153
+ font-size: 0.95rem;
154
+ }
155
+ .b-pay__tag {
156
+ display: inline-block;
157
+ margin-inline-end: 0.35rem;
158
+ padding: 0.1rem 0.55rem;
159
+ border: 1px solid var(--gold);
160
+ border-radius: 999px;
161
+ background: var(--gold-light);
162
+ color: var(--fg);
163
+ font-size: 0.75rem;
164
+ font-weight: 700;
165
+ letter-spacing: 0.06em;
166
+ text-transform: uppercase;
167
+ vertical-align: 0.1em;
168
+ }
169
+ /* Read out ("Usual price: £40", "Sale price: £30"), not shown. */
170
+ .b-pay__sr {
171
+ position: absolute;
172
+ width: 1px;
173
+ height: 1px;
174
+ overflow: hidden;
175
+ clip-path: inset(50%);
176
+ white-space: nowrap;
177
+ }
178
+ .b-pay__was[hidden],
179
+ .b-pay__sale[hidden],
180
+ .b-pay__sr[hidden] {
181
+ display: none;
182
+ }
130
183
  .b-pay__intro {
131
184
  margin: 0;
132
185
  color: var(--muted);
package/lib/pay.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * A "Pay" block takes a payment for one of the organisation's items (the Ferst Payments module).
3
3
  * The block names the item; its name and price are the organisation's, read from the platform at
4
- * build time (and again in the browser, so a new price or a sale shows without a rebuild). The page
4
+ * build time (and again in the browser, so a new name, a new price or a sale shows, and a sale
5
+ * ends, without a rebuild). A sale shows as one: the usual price struck through and its last day. The page
5
6
  * never says what to charge: the platform reads the price itself when the visitor pays.
6
7
  *
7
8
  * A payment is always the site's own, so the site must say who it is (`siteId` in
@@ -14,6 +15,8 @@ export interface PayItem {
14
15
  /** In the smallest unit (pence). */
15
16
  amount: number;
16
17
  currency: string;
18
+ /** A sale running now: the usual price (struck through beside `amount`) and its last day (YYYY-MM-DD, UTC). */
19
+ sale: { usualAmount: number; lastDay: string } | null;
17
20
  /** Where the browser posts the payment (the platform sends the visitor on to Stripe). */
18
21
  action: string;
19
22
  /** Where the page re-reads the current price. */
@@ -29,6 +32,28 @@ export function formatPrice(amount: number, currency: string): string {
29
32
  return currency.toLowerCase() === 'gbp' ? `£${text}` : `${text} ${currency.toUpperCase()}`;
30
33
  }
31
34
 
35
+ /**
36
+ * When a sale ends, in words: "until 30 November" (the year only when it isn't this one), or
37
+ * "ends today" on its last day. Days are UTC, as the platform sets them.
38
+ */
39
+ export function saleLine(lastDay: string, now = new Date()): string {
40
+ if (lastDay === now.toISOString().slice(0, 10)) return 'ends today';
41
+ const d = new Date(`${lastDay}T12:00:00Z`);
42
+ const sameYear = d.getUTCFullYear() === now.getUTCFullYear();
43
+ return `until ${d.toLocaleDateString('en-GB', { day: 'numeric', month: 'long', ...(sameYear ? {} : { year: 'numeric' }), timeZone: 'UTC' })}`;
44
+ }
45
+
46
+ /** The platform's answer, as the block reads it (at build time and in the browser). */
47
+ export interface PayItemAnswer {
48
+ id: string; title: string; amount: number; currency?: string; action: string;
49
+ sale?: { usual_amount: number; last_day: string } | null;
50
+ }
51
+
52
+ export function readSale(d: Pick<PayItemAnswer, 'sale'>): PayItem['sale'] {
53
+ const s = d.sale;
54
+ return s && typeof s.usual_amount === 'number' && typeof s.last_day === 'string' ? { usualAmount: s.usual_amount, lastDay: s.last_day } : null;
55
+ }
56
+
32
57
  const cache = new Map<string, Promise<PayItem>>();
33
58
 
34
59
  export function loadPayItem(item: string, opts: { base: string; siteId?: string | null; fetch?: FetchFn }): Promise<PayItem> {
@@ -57,6 +82,6 @@ async function fetchItem(item: string, base: string, siteId: string | null, fetc
57
82
  throw new Error(`Pay block: "${item}" isn’t on offer on this site. Check it in the portal (the Payments module: what your sites take payment for, and Payments switched on for the site), or run \`npx ferst-modules\` to list what this site can take payment for.`);
58
83
  }
59
84
  if (!res.ok) throw new Error(`Pay block: the Ferst platform answered ${res.status} for item "${item}" (${url}).`);
60
- const d = (await res.json()) as { id: string; title: string; amount: number; currency?: string; action: string };
61
- return { id: d.id, title: d.title, amount: d.amount, currency: d.currency ?? 'gbp', action: d.action, definition: url };
85
+ const d = (await res.json()) as PayItemAnswer;
86
+ return { id: d.id, title: d.title, amount: d.amount, currency: d.currency ?? 'gbp', sale: readSale(d), action: d.action, definition: url };
62
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ferst-core",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
6
6
  "license": "BUSL-1.1",