@wix/himalaya-cli 0.1642.0 → 0.1644.0
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.
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/schema.d.ts +6 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/validator.d.ts +3 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/core.mjs +3 -1
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/sdui.mjs +3 -1
- package/dist/browser-plane/assets.json +5 -5
- package/dist/browser-plane/preview-deferred/{3361f61ce6b490c8b7b252658ea67f733d88c6e9f95906d0e31eea4827141df3.bin → 155f61519885c85d61a39fa595197071a3ca07680da56055ccb1dcff2e709cd1.bin} +1 -1
- package/dist/browser-plane/sw.js +6 -6
- package/dist/cli.mjs +358 -144
- package/dist/preview-assets.generated.bin +0 -0
- package/guides/component-reference.json +1 -1
- package/guides/component-reference.md +6 -0
- package/guides/designing-himi-screens.md +18 -0
- package/guides/offline-reading.md +1 -1
- package/guides/recipe-four-state.md +17 -0
- package/guides/release-notes.md +21 -0
- package/guides/sdk-reference.json +1 -1
- package/package.json +2 -2
- package/template-icons/conference.png +0 -0
- package/templates/beauty-salon/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/beauty-salon/tier5-src/himi-store-product/index.ts +6 -3
- package/templates/branded-lite/dev/index.ts +152 -88
- package/templates/branded-lite/l10n/en.json +1 -1
- package/templates/branded-lite/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/branded-lite/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/branded-lite/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/branded-lite/tier5-src/himi-store-product/index.ts +7 -3
- package/templates/community-club/dev/index.ts +152 -88
- package/templates/community-club/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/community-club/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/community-club/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/conference/README.md +53 -0
- package/templates/conference/config.ts +321 -0
- package/templates/conference/dev/index.ts +17252 -0
- package/templates/conference/himalaya.content.json +46 -0
- package/templates/conference/himalaya.template.json +15 -0
- package/templates/conference/icon.png +0 -0
- package/templates/conference/platforms.json +1 -0
- package/templates/conference/tier5-src/_shared/agenda-stars.ts +64 -0
- package/templates/conference/tier5-src/_shared/app-locale.ts +61 -0
- package/templates/conference/tier5-src/_shared/branded-lite-hosted.ts +279 -0
- package/templates/conference/tier5-src/_shared/branded-load-state.ts +109 -0
- package/templates/conference/tier5-src/_shared/business-chat-copy.ts +40 -0
- package/templates/conference/tier5-src/_shared/business-chat-history.ts +51 -0
- package/templates/conference/tier5-src/_shared/business-chat-message-model.ts +177 -0
- package/templates/conference/tier5-src/_shared/business-chat-model.ts +56 -0
- package/templates/conference/tier5-src/_shared/business-chat-presenter.ts +576 -0
- package/templates/conference/tier5-src/_shared/business-chat-transport.ts +148 -0
- package/templates/conference/tier5-src/_shared/collection-contract.ts +229 -0
- package/templates/conference/tier5-src/_shared/collection-query.ts +193 -0
- package/templates/conference/tier5-src/_shared/collection-service.ts +400 -0
- package/templates/conference/tier5-src/_shared/engine-intl.ts +508 -0
- package/templates/conference/tier5-src/_shared/event-agenda.ts +122 -0
- package/templates/conference/tier5-src/_shared/event-registration.ts +86 -0
- package/templates/conference/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/conference/tier5-src/_shared/himi-account.ts +164 -0
- package/templates/conference/tier5-src/_shared/himi-copy.ts +89 -0
- package/templates/conference/tier5-src/_shared/himi-events.ts +65 -0
- package/templates/conference/tier5-src/_shared/himi-store.ts +7 -0
- package/templates/conference/tier5-src/_shared/hosted-checkout.ts +70 -0
- package/templates/conference/tier5-src/_shared/iap-attendance-form.ts +106 -0
- package/templates/conference/tier5-src/_shared/iap-attendance-purchase.ts +211 -0
- package/templates/conference/tier5-src/_shared/iap-attendance.ts +38 -0
- package/templates/conference/tier5-src/_shared/iap-worker.ts +125 -0
- package/templates/conference/tier5-src/_shared/inbox-rooms.ts +68 -0
- package/templates/conference/tier5-src/_shared/local-preferences.ts +38 -0
- package/templates/conference/tier5-src/_shared/member-access.ts +52 -0
- package/templates/conference/tier5-src/_shared/member-account-routes.ts +52 -0
- package/templates/conference/tier5-src/_shared/member-read.ts +52 -0
- package/templates/conference/tier5-src/_shared/member-screen.ts +99 -0
- package/templates/conference/tier5-src/_shared/offset-page.ts +10 -0
- package/templates/conference/tier5-src/_shared/online-actions.ts +76 -0
- package/templates/conference/tier5-src/_shared/owner-load-error.ts +348 -0
- package/templates/conference/tier5-src/_shared/payment-setup.ts +6 -0
- package/templates/conference/tier5-src/_shared/rich-body.ts +78 -0
- package/templates/conference/tier5-src/_shared/rich-content-link.ts +34 -0
- package/templates/conference/tier5-src/_shared/sign-in-sheet.ts +18 -0
- package/templates/conference/tier5-src/business-chat/index.ts +724 -0
- package/templates/conference/tier5-src/himi-conference-agenda/index.ts +183 -0
- package/templates/conference/tier5-src/himi-conference-home/index.ts +389 -0
- package/templates/conference/tier5-src/himi-events-account/index.ts +219 -0
- package/templates/conference/tier5-src/himi-events-detail/index.ts +1304 -0
- package/templates/conference/tier5-src/himi-events-list/index.ts +124 -0
- package/templates/conference/tier5-src/himi-events-my-events/index.ts +225 -0
- package/templates/conference/tier5-src/himi-events-rsvp/index.ts +230 -0
- package/templates/conference/tier5-src/himi-store-account/index.ts +998 -0
- package/templates/conference/tokens.json +365 -0
- package/templates/events-meetups/dev/index.ts +152 -88
- package/templates/events-meetups/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/events-meetups/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/events-meetups/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/gym-fitness/dev/index.ts +152 -88
- package/templates/gym-fitness/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/gym-fitness/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/gym-fitness/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/gym-fitness/tier5-src/himi-store-product/index.ts +6 -3
- package/templates/nonprofit-cause/dev/index.ts +152 -88
- package/templates/nonprofit-cause/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/nonprofit-cause/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/nonprofit-cause/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/podcast-creator/dev/index.ts +152 -88
- package/templates/podcast-creator/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/podcast-creator/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/podcast-creator/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/portfolio-creative/dev/index.ts +152 -88
- package/templates/portfolio-creative/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/portfolio-creative/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/portfolio-creative/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/portfolio-creative/tier5-src/himi-store-product/index.ts +6 -3
- package/templates/restaurant/dev/index.ts +152 -88
- package/templates/restaurant/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/restaurant/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/restaurant/tier5-src/himi-events-detail/index.ts +93 -11
- package/templates/retail-store/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/retail-store/tier5-src/himi-store-product/index.ts +7 -3
- package/templates/yoga-studio/dev/index.ts +152 -88
- package/templates/yoga-studio/tier5-src/_shared/featured-event.ts +63 -0
- package/templates/yoga-studio/tier5-src/_shared/iap-presentation.ts +5 -2
- package/templates/yoga-studio/tier5-src/himi-events-detail/index.ts +93 -11
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Pure Events registration helpers shared by the event detail and the event list: which action an
|
|
2
|
+
// event offers, its label, and the URLs behind it. One copy, so a card and the detail screen never
|
|
3
|
+
// disagree about whether an event can be registered for.
|
|
4
|
+
import type { WixEvent } from "@himalaya/wix-events";
|
|
5
|
+
import { formatWeekdayDateTime } from "./engine-intl.js";
|
|
6
|
+
import { copyParam } from "./himi-copy.js";
|
|
7
|
+
|
|
8
|
+
function detailCopy(key: string, fallback: string): string { return copyParam(key, fallback); }
|
|
9
|
+
|
|
10
|
+
/** Registration CTA kind derived from the event's registration/eventType. */
|
|
11
|
+
export type RegistrationKind = "rsvp" | "tickets" | "external" | "none";
|
|
12
|
+
|
|
13
|
+
/** Map registration status and decoded eventType onto the CTA variant. The decoded
|
|
14
|
+
* `eventType` already folds registration.type / registration.initialType /
|
|
15
|
+
* the legacy flat `type` (decodeEvent, #478). Without a recognized type or
|
|
16
|
+
* status there is no registration action to offer. */
|
|
17
|
+
export function registrationKindOf(e: WixEvent): RegistrationKind {
|
|
18
|
+
const raw = e as unknown as { registration?: { type?: string; initialType?: string; status?: string } };
|
|
19
|
+
const value = (raw.registration?.type ?? e.eventType ?? raw.registration?.initialType ?? "").toUpperCase();
|
|
20
|
+
if (value === "NONE" || value.includes("NO_REGISTRATION")) return "none";
|
|
21
|
+
// Open statuses select the footer action, so they must also select the
|
|
22
|
+
// inventory read and action guards when type metadata is absent or stale.
|
|
23
|
+
switch (raw.registration?.status?.toUpperCase()) {
|
|
24
|
+
case "OPEN_TICKETS": return "tickets";
|
|
25
|
+
case "OPEN_EXTERNAL": return "external";
|
|
26
|
+
case "OPEN_RSVP":
|
|
27
|
+
case "OPEN_RSVP_WAITLIST_ONLY":
|
|
28
|
+
case "SCHEDULED_RSVP": return "rsvp";
|
|
29
|
+
}
|
|
30
|
+
if (value.includes("EXTERNAL")) return "external";
|
|
31
|
+
if (value.includes("TICKET")) return "tickets";
|
|
32
|
+
if (value.includes("RSVP")) return "rsvp";
|
|
33
|
+
return "none";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function eventStatus(e: WixEvent): string {
|
|
37
|
+
const raw = e as unknown as { status?: string };
|
|
38
|
+
return (raw.status ?? e.eventStatus ?? "").toUpperCase();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function registrationUi(e: WixEvent, now: number): { state: string; label: string; action: string; caption: string; notice: string } {
|
|
42
|
+
const raw = e as unknown as { registration?: { status?: string; registrationPaused?: boolean; registrationDisabled?: boolean; allowedGuestTypes?: string; rsvp?: { startDate?: string }; tickets?: { soldOut?: boolean }; external?: { url?: string } }; form?: { messages?: { registrationClosed?: { message?: string; exploreEventsActionLabel?: string }; ticketsUnavailable?: { message?: string; exploreEventsActionLabel?: string } } } };
|
|
43
|
+
const r = raw.registration ?? {};
|
|
44
|
+
const status = eventStatus(e);
|
|
45
|
+
const type = registrationKindOf(e);
|
|
46
|
+
if (status === "CANCELED") return { state: "notice", label: "", action: "", caption: "", notice: detailCopy("event.canceledNotice", "This event was canceled.") };
|
|
47
|
+
if (status === "ENDED") return { state: "notice", label: "", action: "", caption: "", notice: detailCopy("event.endedNotice", "This event has ended.") };
|
|
48
|
+
if (type === "none" || r.registrationDisabled) return { state: "none", label: "", action: "", caption: "", notice: "" };
|
|
49
|
+
const ownerClosed = raw.form?.messages?.registrationClosed?.message;
|
|
50
|
+
const ownerSold = raw.form?.messages?.ticketsUnavailable?.message;
|
|
51
|
+
if (["CLOSED_AUTOMATICALLY", "CLOSED_MANUALLY"].includes(r.status || "") || r.registrationPaused) return { state: "notice", label: "", action: "", caption: "", notice: ownerClosed || detailCopy("event.closedNotice", "Registration is closed.") };
|
|
52
|
+
if (type === "tickets" && r.tickets?.soldOut) return { state: "notice", label: "", action: "", caption: "", notice: ownerSold || detailCopy("event.soldOutNotice", "Tickets are sold out.") };
|
|
53
|
+
if (r.status === "SCHEDULED_RSVP") return { state: "scheduled", label: detailCopy("event.rsvpCta", "RSVP"), action: "", caption: detailCopy("event.opensCaption", "RSVP opens {date}.").replace("{date}", r.rsvp?.startDate ? formatWeekdayDateTime(new Date(r.rsvp.startDate)) : ""), notice: "" };
|
|
54
|
+
if (r.status === "OPEN_RSVP_WAITLIST_ONLY") return { state: "waitlist", label: detailCopy("event.waitlistCta", "Join Waitlist"), action: "himi.open", caption: detailCopy("event.fullCaption", "This event is full."), notice: "" };
|
|
55
|
+
if (r.status === "OPEN_TICKETS" || type === "tickets") return { state: "tickets", label: detailCopy("event.ticketsCta", "Buy Tickets"), action: "openTickets", caption: "", notice: "" };
|
|
56
|
+
if (r.status === "OPEN_EXTERNAL" || type === "external") return registrationUrlOf(e)
|
|
57
|
+
? { state: "external", label: detailCopy("event.externalCta", "Register"), action: "openRegistration", caption: "", notice: "" }
|
|
58
|
+
: { state: "none", label: "", action: "", caption: "", notice: "" };
|
|
59
|
+
if (type === "rsvp" && r.status === "OPEN_RSVP") return { state: "rsvp", label: detailCopy("event.rsvpCta", "RSVP"), action: "himi.open", caption: "", notice: "" };
|
|
60
|
+
if (r.status === "OPEN_RSVP" || (type === "rsvp" && !r.status)) return { state: "rsvp", label: detailCopy("event.rsvpCta", "RSVP"), action: "himi.open", caption: "", notice: "" };
|
|
61
|
+
void now;
|
|
62
|
+
return { state: "none", label: "", action: "", caption: "", notice: "" };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** External registration URL / event page URL from the raw payload ("" when absent). */
|
|
66
|
+
export function registrationUrlOf(e: WixEvent): string {
|
|
67
|
+
const raw = e as unknown as {
|
|
68
|
+
registration?: { external?: { registration?: string; url?: string } };
|
|
69
|
+
eventPageUrl?: { base?: string; path?: string } | string;
|
|
70
|
+
};
|
|
71
|
+
const external = raw.registration?.external?.registration ?? raw.registration?.external?.url;
|
|
72
|
+
if (typeof external === "string" && /^https?:\/\//i.test(external)) return external;
|
|
73
|
+
return eventPageUrlOf(e);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The event's public page URL when the payload carries one ("" otherwise). */
|
|
77
|
+
export function eventPageUrlOf(e: WixEvent): string {
|
|
78
|
+
const raw = e as unknown as { eventPageUrl?: { base?: string; path?: string } | string };
|
|
79
|
+
const page = raw.eventPageUrl;
|
|
80
|
+
if (typeof page === "string" && /^https?:\/\//i.test(page)) return page;
|
|
81
|
+
if (page && typeof page === "object" && typeof page.base === "string" && page.base) {
|
|
82
|
+
const path = typeof page.path === "string" ? page.path : "";
|
|
83
|
+
return `${page.base.replace(/\/$/, "")}${path && !path.startsWith("/") ? "/" : ""}${path}`;
|
|
84
|
+
}
|
|
85
|
+
return "";
|
|
86
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The site's featured event: the one on now, else the next one to start. A conference app is
|
|
2
|
+
// about one event at a time — its Home hero, its Tickets tab and its Agenda all show this one —
|
|
3
|
+
// and a site with several events reaches the rest from the events list.
|
|
4
|
+
//
|
|
5
|
+
// Read from the visitor-readable Events v3 query. "None" (no live or upcoming event) is a real
|
|
6
|
+
// answer, distinct from a failed read: the caller shows an empty state for the first and an
|
|
7
|
+
// error with Retry for the second, never a sample event.
|
|
8
|
+
import { queryEvents, startDateOf, endDateOf, type WixEvent, type WixEventFieldset } from "@himalaya/wix-events";
|
|
9
|
+
|
|
10
|
+
export type FeaturedEventRead = { ok: true; event: WixEvent | null } | { ok: false; event: null };
|
|
11
|
+
|
|
12
|
+
// Only live and upcoming events, earliest first, filtered and sorted by Wix (verified on a visitor
|
|
13
|
+
// token, 2026-11-10): the featured one is on the first page however many events the site has. The
|
|
14
|
+
// pick below still checks each row, and pages on (to Wix's own total) only while no page has
|
|
15
|
+
// produced an event to feature — never a fixed page cap that could miss one.
|
|
16
|
+
const FEATURED_QUERY = {
|
|
17
|
+
filter: { status: { $in: ["UPCOMING", "STARTED"] } },
|
|
18
|
+
sort: [{ fieldName: "dateAndTimeSettings.startDate", order: "ASC" }],
|
|
19
|
+
};
|
|
20
|
+
const PAGE = 50;
|
|
21
|
+
|
|
22
|
+
const millis = (iso: string | undefined): number => {
|
|
23
|
+
const ms = iso ? Date.parse(iso) : NaN;
|
|
24
|
+
return Number.isNaN(ms) ? NaN : ms;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** The event to feature among `events` at `now`: a published event that has not ended — the one
|
|
28
|
+
* that started first when several run now, else the next to start. A draft, a canceled event
|
|
29
|
+
* and an ended one are never featured; an event with no date is featured only when no dated one
|
|
30
|
+
* qualifies (its date is "to be announced"). */
|
|
31
|
+
export function pickFeaturedEvent(events: WixEvent[], now: number): WixEvent | null {
|
|
32
|
+
const eligible = events.filter((e) => {
|
|
33
|
+
const status = String(e.eventStatus ?? (e as { status?: string }).status ?? "").toUpperCase();
|
|
34
|
+
if (status === "DRAFT" || status === "CANCELED" || status === "ENDED") return false;
|
|
35
|
+
const end = millis(endDateOf(e)), start = millis(startDateOf(e));
|
|
36
|
+
const last = Number.isNaN(end) ? start : end;
|
|
37
|
+
return Number.isNaN(last) || last >= now;
|
|
38
|
+
});
|
|
39
|
+
const dated = eligible.filter((e) => !Number.isNaN(millis(startDateOf(e))));
|
|
40
|
+
if (dated.length) return dated.slice().sort((a, b) => millis(startDateOf(a)) - millis(startDateOf(b)))[0];
|
|
41
|
+
return eligible[0] ?? null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Read the site's live and upcoming events and pick the featured one. */
|
|
45
|
+
export async function readFeaturedEvent(fields: WixEventFieldset[] = ["DETAILS", "REGISTRATION"], now: number = Date.now()): Promise<FeaturedEventRead> {
|
|
46
|
+
const events: WixEvent[] = [];
|
|
47
|
+
try {
|
|
48
|
+
for (let offset = 0; ; offset += PAGE) {
|
|
49
|
+
const res = await queryEvents({ limit: PAGE, offset, fields, query: FEATURED_QUERY as never });
|
|
50
|
+
const page = (res.events ?? []).filter((e) => !!e.id);
|
|
51
|
+
events.push(...page);
|
|
52
|
+
const total = Number((res as { pagingMetadata?: { total?: unknown } }).pagingMetadata?.total);
|
|
53
|
+
const done = page.length < PAGE || (Number.isFinite(total) && offset + PAGE >= total);
|
|
54
|
+
// A dated pick is final (a later page starts later); an undated one is not — a dated event
|
|
55
|
+
// may still follow, and a dated event is always featured before a "to be announced" one.
|
|
56
|
+
const pick = pickFeaturedEvent(events, now);
|
|
57
|
+
if (done || (pick && !Number.isNaN(millis(startDateOf(pick))))) break;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
return { ok: false, event: null };
|
|
61
|
+
}
|
|
62
|
+
return { ok: true, event: pickFeaturedEvent(events, now) };
|
|
63
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { dispatch } from "@himalaya/dispatch";
|
|
2
|
+
// Relative for node resolution (see himi-store.ts note); rewritten off-repo.
|
|
3
|
+
import { displayName, getMyData, initials, type WixMyData } from "@himalaya/wix-users";
|
|
4
|
+
import { getMyMember } from "@himalaya/wix-members";
|
|
5
|
+
import { wixMediaUrl } from "@himalaya/wix-media";
|
|
6
|
+
import { formatDateMedium, formatWeekdayDateTime } from "../_shared/engine-intl.js";
|
|
7
|
+
|
|
8
|
+
export interface AccountProfile {
|
|
9
|
+
isMember: boolean;
|
|
10
|
+
identityLine: string;
|
|
11
|
+
detailLine: string;
|
|
12
|
+
memberName: string;
|
|
13
|
+
memberEmail: string;
|
|
14
|
+
memberImage: string;
|
|
15
|
+
memberInitials: string;
|
|
16
|
+
hasMemberImage: boolean;
|
|
17
|
+
noMemberImage: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AccountActivityRow {
|
|
21
|
+
id: string;
|
|
22
|
+
title: string;
|
|
23
|
+
subtitle: string;
|
|
24
|
+
meta: string;
|
|
25
|
+
status: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function strOf(arg: unknown): string {
|
|
29
|
+
if (typeof arg === "string") return arg;
|
|
30
|
+
const a = arg as { value?: string; text?: string } | undefined;
|
|
31
|
+
return a?.value ?? a?.text ?? "";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function titleCase(value?: string): string {
|
|
35
|
+
return (value || "")
|
|
36
|
+
.toLowerCase()
|
|
37
|
+
.split(/[_\s-]+/)
|
|
38
|
+
.filter(Boolean)
|
|
39
|
+
.map((part) => part[0].toUpperCase() + part.slice(1))
|
|
40
|
+
.join(" ");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function fmtDate(value?: string): string {
|
|
44
|
+
if (!value) return "";
|
|
45
|
+
const date = new Date(value);
|
|
46
|
+
if (Number.isNaN(date.getTime())) return "";
|
|
47
|
+
return formatDateMedium(date);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function fmtDateTime(value?: string): string {
|
|
51
|
+
if (!value) return "";
|
|
52
|
+
const date = new Date(value);
|
|
53
|
+
if (Number.isNaN(date.getTime())) return "";
|
|
54
|
+
return formatWeekdayDateTime(date);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function initialsFromName(value: string): string {
|
|
58
|
+
const parts = value.trim().split(/\s+/).filter(Boolean);
|
|
59
|
+
if (!parts.length) return "";
|
|
60
|
+
const first = parts[0]?.[0] ?? "";
|
|
61
|
+
const last = parts.length > 1 ? parts[parts.length - 1]?.[0] ?? "" : "";
|
|
62
|
+
return (first + last).toUpperCase();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function profileImage(data: WixMyData): string {
|
|
66
|
+
const profile = data.profile as { profileImage?: string; photo?: { url?: string }; imageUrl?: string } | undefined;
|
|
67
|
+
return profile?.profileImage || profile?.photo?.url || profile?.imageUrl || "";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function profileFromData(data: WixMyData, fallbackEmail = ""): AccountProfile {
|
|
71
|
+
const email = fallbackEmail || data.email || data.contactEmail || "";
|
|
72
|
+
const name = displayName(data) || email || "Member";
|
|
73
|
+
const image = profileImage(data);
|
|
74
|
+
return {
|
|
75
|
+
isMember: true,
|
|
76
|
+
identityLine: "Signed in",
|
|
77
|
+
detailLine: email || name,
|
|
78
|
+
memberName: name,
|
|
79
|
+
memberEmail: email,
|
|
80
|
+
memberImage: image,
|
|
81
|
+
memberInitials: initials(data) || initialsFromName(name) || "M",
|
|
82
|
+
hasMemberImage: !!image,
|
|
83
|
+
noMemberImage: !image,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The signed-in site member's name and photo from their member record, or undefined when it
|
|
88
|
+
* cannot be read or names nobody (the caller then shows the email). */
|
|
89
|
+
async function siteMemberProfile(email: string): Promise<AccountProfile | undefined> {
|
|
90
|
+
try {
|
|
91
|
+
const member = (await getMyMember({ fieldsets: ["FULL"] }))?.member;
|
|
92
|
+
const name = [member?.contact?.firstName, member?.contact?.lastName].map((part) => (part ?? "").trim()).filter(Boolean).join(" ")
|
|
93
|
+
|| (member?.profile?.nickname ?? "").trim();
|
|
94
|
+
if (!name) return undefined;
|
|
95
|
+
const image = wixMediaUrl(member?.profile?.photo?.url) || wixMediaUrl(member?.profile?.photo?.id) || "";
|
|
96
|
+
return {
|
|
97
|
+
isMember: true,
|
|
98
|
+
identityLine: "Signed in",
|
|
99
|
+
detailLine: email || name,
|
|
100
|
+
memberName: name,
|
|
101
|
+
memberEmail: email || member?.loginEmail || "",
|
|
102
|
+
memberImage: image,
|
|
103
|
+
memberInitials: initialsFromName(name) || "M",
|
|
104
|
+
hasMemberImage: !!image,
|
|
105
|
+
noMemberImage: !image,
|
|
106
|
+
};
|
|
107
|
+
} catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function guestProfile(detailLine: string): AccountProfile {
|
|
113
|
+
return {
|
|
114
|
+
isMember: false,
|
|
115
|
+
identityLine: "Browsing as a guest",
|
|
116
|
+
detailLine,
|
|
117
|
+
memberName: "",
|
|
118
|
+
memberEmail: "",
|
|
119
|
+
memberImage: "",
|
|
120
|
+
memberInitials: "M",
|
|
121
|
+
hasMemberImage: false,
|
|
122
|
+
noMemberImage: true,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function signedInEmail(): Promise<string> {
|
|
127
|
+
try {
|
|
128
|
+
const identity = await dispatch("himi.wix.memberIdentity") as unknown;
|
|
129
|
+
const direct = identity as { email?: unknown } | null;
|
|
130
|
+
const wrapped = identity as { payload?: { email?: unknown } } | null;
|
|
131
|
+
const email = direct?.email ?? wrapped?.payload?.email;
|
|
132
|
+
return typeof email === "string" ? email : "";
|
|
133
|
+
} catch {
|
|
134
|
+
return "";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function loadMemberProfile(guestDetailLine: string): Promise<AccountProfile> {
|
|
139
|
+
// `himi.wix.memberIdentity` is the session source of truth. Visitor-scoped
|
|
140
|
+
// getMyData can still return a site/account nickname (e.g. apptestercoowner)
|
|
141
|
+
// after sign-out or for anonymous — never treat that as a signed-in member.
|
|
142
|
+
const storedEmail = await signedInEmail();
|
|
143
|
+
if (!storedEmail) return guestProfile(guestDetailLine);
|
|
144
|
+
try {
|
|
145
|
+
const data = await getMyData();
|
|
146
|
+
return profileFromData(data, storedEmail);
|
|
147
|
+
} catch {
|
|
148
|
+
// The account profile is an owner endpoint: the web host refuses it, and a site member need
|
|
149
|
+
// not have one. The member's own record (the Members API) still names them.
|
|
150
|
+
const member = await siteMemberProfile(storedEmail);
|
|
151
|
+
if (member) return member;
|
|
152
|
+
return {
|
|
153
|
+
isMember: true,
|
|
154
|
+
identityLine: "Signed in",
|
|
155
|
+
detailLine: storedEmail,
|
|
156
|
+
memberName: storedEmail,
|
|
157
|
+
memberEmail: storedEmail,
|
|
158
|
+
memberImage: "",
|
|
159
|
+
memberInitials: initialsFromName(storedEmail) || "M",
|
|
160
|
+
hasMemberImage: false,
|
|
161
|
+
noMemberImage: true,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Copy-as-configuration for WORKER-emitted strings (`himi docs designing-himi-screens`,
|
|
2
|
+
// §"Copy is configuration"): a worker never hardcodes user-facing copy — the
|
|
3
|
+
// descriptor bakes the provisioned copy-pack overrides relevant to this worker
|
|
4
|
+
// into its onAppear action params as a `copy` JSON object (customer strings or
|
|
5
|
+
// source-matched stock translations, computed at descriptor-build time), and the
|
|
6
|
+
// worker reads every user-facing string through `copyParam(key, fallback)`.
|
|
7
|
+
//
|
|
8
|
+
// STASHED (the stashed appear-params idiom, himi-branded-lite-home): the
|
|
9
|
+
// param-carrying onAppear dispatch updates the stash; later arg-less calls
|
|
10
|
+
// (self-kick refreshes, retry buttons, pull-to-refresh) fall back to it, so
|
|
11
|
+
// resolution order can never drop the pack. With no pack the param is absent
|
|
12
|
+
// and every read returns `fallback` verbatim — routing a worker string through
|
|
13
|
+
// copyParam is byte-for-byte behavior-preserving.
|
|
14
|
+
//
|
|
15
|
+
// Pure + Hermes-safe (no host globals, no Node APIs; its only import is the pure `app-locale` sibling): bundled into
|
|
16
|
+
// every worker that emits user-facing copy, and copied verbatim into the
|
|
17
|
+
// generated templates' tier5-src/_shared/.
|
|
18
|
+
|
|
19
|
+
import { isSingular, setAppLocale, setBusinessTimeZone } from "./app-locale.js";
|
|
20
|
+
|
|
21
|
+
/** Customer strings override every read; translated stock is valid only for its
|
|
22
|
+
* original English fallback. Some existing keys serve more than one message. */
|
|
23
|
+
export type CopyPackValue = string | { source: string; translation: string };
|
|
24
|
+
|
|
25
|
+
let stashedCopy: Record<string, CopyPackValue> = Object.create(null);
|
|
26
|
+
let copyReceived = false;
|
|
27
|
+
|
|
28
|
+
/** Absorb the `copy` onAppear param (customer strings or stock translation records)
|
|
29
|
+
* from an action arg. Call first in every action that can receive onAppear
|
|
30
|
+
* params (load/refresh). Non-copy args and malformed packs are ignored — the
|
|
31
|
+
* stock fallbacks keep rendering. */
|
|
32
|
+
export function stashCopy(arg: unknown): void {
|
|
33
|
+
if (!arg || typeof arg !== "object") return;
|
|
34
|
+
// A param-carrying dispatch names the app language and business zone too (absent: the English house
|
|
35
|
+
// pattern, and no zone).
|
|
36
|
+
if ("copy" in arg || "locale" in arg || "businessTimeZone" in arg) {
|
|
37
|
+
setAppLocale((arg as Record<string, unknown>).locale);
|
|
38
|
+
setBusinessTimeZone((arg as Record<string, unknown>).businessTimeZone);
|
|
39
|
+
}
|
|
40
|
+
const raw = (arg as Record<string, unknown>).copy;
|
|
41
|
+
if (typeof raw !== "string" || !raw) return;
|
|
42
|
+
try {
|
|
43
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
44
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return;
|
|
45
|
+
const next: Record<string, CopyPackValue> = Object.create(null);
|
|
46
|
+
for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
|
|
47
|
+
if (typeof value === "string") next[key] = value;
|
|
48
|
+
else if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
49
|
+
const row = value as Record<string, unknown>;
|
|
50
|
+
if (typeof row.source === "string" && typeof row.translation === "string") {
|
|
51
|
+
next[key] = { source: row.source, translation: row.translation };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
stashedCopy = next;
|
|
56
|
+
copyReceived = true;
|
|
57
|
+
} catch {
|
|
58
|
+
// Malformed pack param — keep the stock copy rather than dying mid-action.
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The provisioned override for `key`, or `fallback` (the stock copy). An optional
|
|
63
|
+
* restored label survives only until a current copy pack arrives. */
|
|
64
|
+
export function copyParam(key: string, fallback: string, restoredFallback = fallback): string {
|
|
65
|
+
const value = stashedCopy[key];
|
|
66
|
+
// A disk-restored row may paint before onAppear supplies its copy pack. Keep its
|
|
67
|
+
// last label only before a pack arrives; even an empty current pack selects stock copy.
|
|
68
|
+
if (value === undefined) return copyReceived ? fallback : restoredFallback;
|
|
69
|
+
if (typeof value === "string") return value !== "" ? value : fallback;
|
|
70
|
+
return value?.source === fallback && value.translation !== "" ? value.translation : fallback;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Interpolate `{name}` placeholders in a (possibly overridden) template:
|
|
74
|
+
* `copyTemplate("plans.joinOnSite", "…join {name}.", { name })`. Unknown
|
|
75
|
+
* placeholders resolve to "" so a typo'd override degrades, never crashes. */
|
|
76
|
+
export function copyTemplate(key: string, fallback: string, values: Record<string, string | number>): string {
|
|
77
|
+
return copyParam(key, fallback).replace(/\{([A-Za-z0-9_]+)\}/g, (_all, name: string) =>
|
|
78
|
+
name in values ? String(values[name]) : "",
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The small ICU plural form used by Bookings spot counts. The provisioned copy may
|
|
83
|
+
* translate both branches without adding a second key for the singular. */
|
|
84
|
+
export function copyPlural(key: string, fallback: string, count: number): string {
|
|
85
|
+
const pattern = copyParam(key, fallback);
|
|
86
|
+
const match = /^\{n, plural, one \{([^{}]*)\} other \{([^{}]*)\}\}$/.exec(pattern);
|
|
87
|
+
if (!match) return copyTemplate(key, fallback, { n: count });
|
|
88
|
+
return (isSingular(count) ? match[1] : match[2])!.replace(/#/g, String(count));
|
|
89
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Shared himi-events helpers for the app worker bundles.
|
|
2
|
+
//
|
|
3
|
+
// The PURE event display normalizers (normalizeEvent, eventLocation, eventWhen, …)
|
|
4
|
+
// graduated to the SDK (SP-3) — re-exported below so existing bundle imports keep
|
|
5
|
+
// working; new code should import them from "@himalaya/wix-events" directly.
|
|
6
|
+
// Relative for node resolution (see himi-store.ts note); rewritten off-repo.
|
|
7
|
+
export * from "@himalaya/wix-events";
|
|
8
|
+
|
|
9
|
+
// Host-coupled RSVP-persistence + screen-input helpers stay HERE (not in the published
|
|
10
|
+
// @himalaya/wix-events SDK): they read the worker-runtime host globals
|
|
11
|
+
// (`globalThis.himalaya.storage` / `.input`), which a published, brand-scrubbed SDK
|
|
12
|
+
// package must not reference. The himi-events-* bundles import these from this module.
|
|
13
|
+
|
|
14
|
+
interface StorageLike {
|
|
15
|
+
getItem(key: string): string | null;
|
|
16
|
+
setItem(key: string, value: string): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const RSVP_KEY = "himi-events.confirmed-rsvps";
|
|
20
|
+
|
|
21
|
+
function storage(): StorageLike | undefined {
|
|
22
|
+
const host = globalThis as unknown as {
|
|
23
|
+
himalaya?: { storage?: StorageLike };
|
|
24
|
+
localStorage?: StorageLike;
|
|
25
|
+
};
|
|
26
|
+
return host.himalaya?.storage ?? host.localStorage;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function currentInputId(prefixes: string[]): string {
|
|
30
|
+
// WS-C — `himalaya.input.id` is the PRIMARY contract: hosts thread the row's
|
|
31
|
+
// `id` nav param (himi.open { screenId: <canonical>, id }) into worker input.
|
|
32
|
+
const input = (globalThis as { himalaya?: { input?: { id?: unknown; screenId?: unknown } } }).himalaya?.input;
|
|
33
|
+
if (typeof input?.id === "string" && input.id) return input.id;
|
|
34
|
+
// #570 back-compat for pre-params releases with baked `<prefix><id>`
|
|
35
|
+
// screenIds — do not author against this.
|
|
36
|
+
const screenId = typeof input?.screenId === "string" ? input.screenId : "";
|
|
37
|
+
for (const prefix of prefixes) {
|
|
38
|
+
if (screenId.startsWith(prefix)) return screenId.slice(prefix.length);
|
|
39
|
+
}
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function confirmedRsvpIds(): string[] {
|
|
44
|
+
try {
|
|
45
|
+
const raw = storage()?.getItem(RSVP_KEY);
|
|
46
|
+
const parsed = raw ? JSON.parse(raw) : [];
|
|
47
|
+
return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === "string") : [];
|
|
48
|
+
} catch {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function rememberRsvp(eventId: string): void {
|
|
54
|
+
if (!eventId) return;
|
|
55
|
+
const ids = confirmedRsvpIds();
|
|
56
|
+
const next = [eventId, ...ids.filter((id) => id !== eventId)].slice(0, 50);
|
|
57
|
+
try { storage()?.setItem(RSVP_KEY, JSON.stringify(next)); } catch { /* best-effort */ }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Remove a locally confirmed RSVP after Wix confirms its cancellation. */
|
|
61
|
+
export function forgetRsvp(eventId: string): void {
|
|
62
|
+
if (!eventId) return;
|
|
63
|
+
const next = confirmedRsvpIds().filter((id) => id !== eventId);
|
|
64
|
+
try { storage()?.setItem(RSVP_KEY, JSON.stringify(next)); } catch { /* best-effort */ }
|
|
65
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Graduated to the SDK (SP-3): the store display helpers + v3 product query now live in
|
|
2
|
+
// @himalaya/wix-stores. This thin re-export keeps existing bundle imports working; new
|
|
3
|
+
// code should import from "@himalaya/wix-stores" directly.
|
|
4
|
+
// Relative so node (tests importing worker sources) resolves it too — the bare
|
|
5
|
+
// "@himalaya/wix-stores" name exists only in the esbuild alias map / the off-repo
|
|
6
|
+
// umbrella. gen-templates.mts rewrites this back to @himalaya/wix-stores off-repo.
|
|
7
|
+
export * from "@himalaya/wix-stores";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Hosted checkout. A Branded Lite hosted in a host shell (Spaces) pays as the member the host
|
|
2
|
+
// lent it: Headless `createRedirectSession` does not take a Member instance, so the HOST opens the site's
|
|
3
|
+
// checkout signed in as that member. The worker names only its ecom checkout; the host reads Wix's
|
|
4
|
+
// checkout URL, mints a one-time member session and opens the site's `createSessionCookie` → checkout.
|
|
5
|
+
// The session token never reaches this worker.
|
|
6
|
+
//
|
|
7
|
+
// Classic Wix Events tickets and classic Pricing Plans have no ecom checkout: for those the
|
|
8
|
+
// worker names the page by id (`himi.host.memberSitePage`) and the host opens the site's own event or
|
|
9
|
+
// plans page the same way, signed in — where the member picks and pays on the site. A member's orders
|
|
10
|
+
// are the same (Wix lists them to no member identity), so My orders opens the site's own page too.
|
|
11
|
+
|
|
12
|
+
import { dispatch } from "@himalaya/dispatch";
|
|
13
|
+
import { hostSiteFromInput } from "./branded-lite-hosted.js";
|
|
14
|
+
|
|
15
|
+
/** The host verbs (registered by the host shell, never by the app itself). The first prepares the
|
|
16
|
+
* checkout and answers a one-time ticket; the second opens it. Two steps so the caller's charge guard
|
|
17
|
+
* is re-read after the host's network round trips and right before the checkout opens — the prepared
|
|
18
|
+
* URL (which carries the member's session token) stays in the host; only the ticket crosses here. */
|
|
19
|
+
export const HOSTED_CHECKOUT_ACTION = "himi.host.memberCheckout";
|
|
20
|
+
export const HOSTED_CHECKOUT_OPEN_ACTION = "himi.host.memberCheckout.open";
|
|
21
|
+
/** Prepares a site PAGE (not an ecom checkout) the same way; opened with [HOSTED_CHECKOUT_OPEN_ACTION]. */
|
|
22
|
+
export const HOSTED_SITE_PAGE_ACTION = "himi.host.memberSitePage";
|
|
23
|
+
|
|
24
|
+
/** A site page a hosted worker may ask its host for — ids only, never a URL: the host reads the URL
|
|
25
|
+
* from Wix itself and decides whether the member may be signed in there. */
|
|
26
|
+
export type HostedSitePage = { page: "event"; eventId: string } | { page: "plans"; planId?: string } | { page: "orders" };
|
|
27
|
+
|
|
28
|
+
/** Whether this worker runs inside a host that opened a site for it. */
|
|
29
|
+
export function isHostedWorker(): boolean {
|
|
30
|
+
return hostSiteFromInput((globalThis as { himalaya?: { input?: unknown } }).himalaya?.input) !== null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type HostAnswer = { opened?: unknown; ticket?: unknown; payload?: { opened?: unknown; ticket?: unknown } } | null;
|
|
34
|
+
type HostOpener = (stillMayCharge: () => Promise<boolean>) => Promise<boolean | "withdrawn">;
|
|
35
|
+
|
|
36
|
+
/** Prepare with [action] [params] → re-read the guard → open by ticket. A host without the verbs, or one
|
|
37
|
+
* that refused (Wix named no page, an unsafe URL, no account session), → false, never a throw. */
|
|
38
|
+
function hostOpener(action: string, params: Record<string, string>): HostOpener {
|
|
39
|
+
return async (stillMayCharge) => {
|
|
40
|
+
try {
|
|
41
|
+
const prepared = await dispatch(action, params) as HostAnswer;
|
|
42
|
+
const ticket = prepared?.ticket ?? prepared?.payload?.ticket;
|
|
43
|
+
if (typeof ticket !== "string" || !ticket) return false;
|
|
44
|
+
// An unopened ticket simply expires in the host: nothing to cancel.
|
|
45
|
+
if (!(await stillMayCharge())) return "withdrawn";
|
|
46
|
+
const result = await dispatch(HOSTED_CHECKOUT_OPEN_ACTION, { ticket }) as HostAnswer;
|
|
47
|
+
const opened = result?.opened ?? result?.payload?.opened;
|
|
48
|
+
return opened === true || opened === "true";
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** `payCashierOrder`'s `openCashier` for a hosted app: the host prepares [checkoutId]'s checkout as the
|
|
56
|
+
* lent member, the caller's guard is re-read, and only then does the host open it. Resolves whether it
|
|
57
|
+
* opened ("withdrawn" when the guard said no); a host without the verbs (or that refused) → false. */
|
|
58
|
+
export function hostedCheckoutOpener(checkoutId: string): HostOpener {
|
|
59
|
+
return hostOpener(HOSTED_CHECKOUT_ACTION, { checkoutId });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The site's own [page] opened by the host, signed in as the lent member where the host deems it safe
|
|
63
|
+
* (a visitor otherwise) — the same two steps and answers as [hostedCheckoutOpener]. */
|
|
64
|
+
export function hostedSitePageOpener(page: HostedSitePage): HostOpener {
|
|
65
|
+
const params: Record<string, string> = page.page === "event"
|
|
66
|
+
? { page: "event", eventId: page.eventId }
|
|
67
|
+
: page.page === "orders" ? { page: "orders" }
|
|
68
|
+
: { page: "plans", ...(page.planId ? { planId: page.planId } : {}) };
|
|
69
|
+
return hostOpener(HOSTED_SITE_PAGE_ACTION, params);
|
|
70
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { copyParam } from "./himi-copy.js";
|
|
2
|
+
|
|
3
|
+
export interface AttendanceField {
|
|
4
|
+
id: string;
|
|
5
|
+
label: string;
|
|
6
|
+
kind: string;
|
|
7
|
+
required: boolean;
|
|
8
|
+
value: string;
|
|
9
|
+
selectedIds: string[];
|
|
10
|
+
options: { id: string; label: string }[];
|
|
11
|
+
error: string;
|
|
12
|
+
maxLength?: number;
|
|
13
|
+
maxSize?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const object = (v: unknown): Record<string, unknown> => v !== null && typeof v === "object" && !Array.isArray(v) ? v as Record<string, unknown> : {};
|
|
17
|
+
const text = (v: unknown): string => typeof v === "string" ? v : "";
|
|
18
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
19
|
+
|
|
20
|
+
export function createAttendanceForm(prefix: string) {
|
|
21
|
+
let fields: AttendanceField[] = [];
|
|
22
|
+
let supported = false;
|
|
23
|
+
const label = (name: string) => copyParam(`${prefix}.${name}Label`, name === "firstName" ? "First name" : name === "lastName" ? "Last name" : "Email");
|
|
24
|
+
const field = (scope: string, name: string, input: Record<string, unknown> = {}): AttendanceField => ({
|
|
25
|
+
id: `${scope}:${name}`, label: text(input.label) || label(name), required: input.mandatory === true,
|
|
26
|
+
kind: name === "email" ? "email" : input.type === "NUMBER" ? "number" : input.type === "TEXT_ARRAY" ? "multiple" : Array.isArray(input.options) && input.options.length ? "choice" : "text",
|
|
27
|
+
value: "", selectedIds: [], error: "",
|
|
28
|
+
options: Array.isArray(input.options) ? input.options.filter((o): o is string => typeof o === "string").map((o) => ({ id: o, label: o })) : [],
|
|
29
|
+
...(typeof input.maxLength === "number" ? { maxLength: input.maxLength } : {}),
|
|
30
|
+
...(typeof input.maxSize === "number" ? { maxSize: input.maxSize } : {}),
|
|
31
|
+
});
|
|
32
|
+
const contact = (scope: string) => ["firstName", "lastName", "email"].map((name) => field(scope, name, { mandatory: true }));
|
|
33
|
+
|
|
34
|
+
function event(raw: unknown): void {
|
|
35
|
+
fields = contact("buyer");
|
|
36
|
+
const controls = object(raw).controls;
|
|
37
|
+
supported = Array.isArray(controls) && controls.length > 0;
|
|
38
|
+
const names = new Set<string>();
|
|
39
|
+
for (const control of Array.isArray(controls) ? controls : []) {
|
|
40
|
+
const c = object(control);
|
|
41
|
+
if (c.deleted === true) continue;
|
|
42
|
+
if (!Array.isArray(c.inputs) || !c.inputs.length) { supported = false; continue; }
|
|
43
|
+
for (const rawInput of c.inputs) {
|
|
44
|
+
const input = object(rawInput);
|
|
45
|
+
const name = text(input.name);
|
|
46
|
+
if (!name || names.has(name) || !["TEXT", "NUMBER", "TEXT_ARRAY"].includes(text(input.type)) || (input.type === "TEXT_ARRAY" && (!Array.isArray(input.options) || !input.options.length))) { supported = false; continue; }
|
|
47
|
+
names.add(name);
|
|
48
|
+
fields.push(field("guest", name, { ...input, mandatory: input.mandatory === true || ["firstName", "lastName", "email"].includes(name) }));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!["firstName", "lastName", "email"].every((name) => names.has(name))) supported = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function booking(service: unknown): void {
|
|
55
|
+
fields = contact("buyer");
|
|
56
|
+
const id = text(object(object(service).form).id);
|
|
57
|
+
// Custom intake schemas need their own reader; never silently omit their required fields.
|
|
58
|
+
supported = id === "00000000-0000-0000-0000-000000000000";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function change(arg: unknown): void {
|
|
62
|
+
const a = object(arg);
|
|
63
|
+
const f = fields.find((candidate) => candidate.id === a.id);
|
|
64
|
+
if (!f) return;
|
|
65
|
+
if (typeof a.option === "string") {
|
|
66
|
+
if (f.kind === "multiple") f.selectedIds = f.selectedIds.includes(a.option) ? f.selectedIds.filter((v) => v !== a.option) : [...f.selectedIds, a.option];
|
|
67
|
+
else f.value = a.option;
|
|
68
|
+
f.error = "";
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
f.value = text(a.value ?? a.text ?? a.selectedId);
|
|
72
|
+
f.selectedIds = Array.isArray(a.selectedIds) ? a.selectedIds.filter((v): v is string => typeof v === "string") : [];
|
|
73
|
+
f.error = "";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function valid(f: AttendanceField): boolean {
|
|
77
|
+
const value = f.value.trim();
|
|
78
|
+
const values = f.kind === "multiple" ? f.selectedIds : value ? [value] : [];
|
|
79
|
+
if (!values.length) return !f.required;
|
|
80
|
+
if (f.options.length && values.some((v) => !f.options.some((o) => o.id === v))) return false;
|
|
81
|
+
if (f.maxSize !== undefined && values.length > f.maxSize) return false;
|
|
82
|
+
if (f.maxLength !== undefined && values.some((v) => v.length > f.maxLength!)) return false;
|
|
83
|
+
return f.kind === "email" ? EMAIL.test(value) : f.kind !== "number" || /^-?\d+(?:\.\d+)?$/.test(value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function validate(): boolean {
|
|
87
|
+
for (const f of fields) f.error = valid(f) ? "" : copyParam(`${prefix}.formInvalidText`, "Check this field.");
|
|
88
|
+
return supported && fields.every((f) => !f.error);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function buyer(): Record<string, string> {
|
|
92
|
+
return Object.fromEntries(fields.filter((f) => f.id.startsWith("buyer:")).map((f) => [f.id.slice(6), f.value.trim()]));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function ticket(definitionId: string) {
|
|
96
|
+
const inputValues = fields.filter((f) => f.id.startsWith("guest:") && (f.value.trim() || f.selectedIds.length)).map((f) => ({ inputName: f.id.slice(6), ...(f.kind === "multiple" ? { values: f.selectedIds.slice() } : { value: f.value.trim() }) }));
|
|
97
|
+
return { buyer: buyer(), guests: [{ ticketDefinitionId: definitionId, form: { inputValues } }] };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
event, booking, change, validate, buyer, ticket,
|
|
102
|
+
supported: () => supported,
|
|
103
|
+
rows: () => fields.map((f) => ({ ...f, selectedIds: f.selectedIds.slice(), kind: f.kind === "text" && f.required ? "requiredText" : f.kind })),
|
|
104
|
+
clear: () => { for (const f of fields) { f.value = ""; f.selectedIds = []; f.error = ""; } },
|
|
105
|
+
};
|
|
106
|
+
}
|