@wix/agent-skills 1.20.0 → 1.20.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.
- package/package.json +1 -1
- package/skills/wix-headless-fast/SKILL.md +200 -34
- package/skills/wix-headless-fast/entry/skill.md +10 -2
- package/skills/wix-headless-fast/install/attach.mjs +253 -0
- package/skills/wix-headless-fast/install/deploy.mjs +109 -9
- package/skills/wix-headless-fast/install/fast-path.mjs +29 -34
- package/skills/wix-headless-fast/references/blog/INSTRUCTIONS.md +224 -60
- package/skills/wix-headless-fast/references/blog/app/components/blog/BlogFeedView.tsx +2 -3
- package/skills/wix-headless-fast/references/blog/app/components/blog/PostView.tsx +2 -3
- package/skills/wix-headless-fast/references/blog/app/hooks/blog/useBlogFeed.ts +24 -116
- package/skills/wix-headless-fast/references/blog/app/hooks/blog/usePost.ts +16 -64
- package/skills/wix-headless-fast/references/blog/app/wix/blog/blog-feed-store.ts +137 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/post-store.ts +78 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/posts-core.ts +117 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/posts.ts +13 -60
- package/skills/wix-headless-fast/references/blog/app/wix/blog/taxonomy-core.ts +47 -0
- package/skills/wix-headless-fast/references/blog/app/wix/blog/taxonomy.ts +10 -28
- package/skills/wix-headless-fast/references/blog/app-astro/pages/blog/[...slug].astro +2 -4
- package/skills/wix-headless-fast/references/blog/rest/posts.ts +42 -0
- package/skills/wix-headless-fast/references/blog/rest/taxonomy.ts +62 -0
- package/skills/wix-headless-fast/references/blog/seed/SEED.md +9 -0
- package/skills/wix-headless-fast/references/bookings/INSTRUCTIONS.md +179 -33
- package/skills/wix-headless-fast/references/bookings/app/components/bookings/ServiceBookingView.tsx +2 -1
- package/skills/wix-headless-fast/references/bookings/app/hooks/bookings/useBookingFlow.ts +26 -132
- package/skills/wix-headless-fast/references/bookings/app/hooks/bookings/useServices.ts +18 -49
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/booking-core.ts +218 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/booking-flow-store.ts +169 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/booking.ts +57 -142
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/services-core.ts +92 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/services-store.ts +90 -0
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/services.ts +22 -61
- package/skills/wix-headless-fast/references/bookings/app/wix/bookings/types.ts +3 -1
- package/skills/wix-headless-fast/references/bookings/rest/booking.ts +113 -0
- package/skills/wix-headless-fast/references/bookings/rest/services.ts +55 -0
- package/skills/wix-headless-fast/references/bookings/seed/SEED.md +10 -0
- package/skills/wix-headless-fast/references/cms/INSTRUCTIONS.md +236 -62
- package/skills/wix-headless-fast/references/cms/app/hooks/cms/useCollection.ts +19 -52
- package/skills/wix-headless-fast/references/cms/app/hooks/cms/useItem.ts +16 -36
- package/skills/wix-headless-fast/references/cms/app/wix/cms/collection-store.ts +158 -0
- package/skills/wix-headless-fast/references/cms/app/wix/cms/item-store.ts +93 -0
- package/skills/wix-headless-fast/references/cms/app/wix/cms/items-core.ts +177 -0
- package/skills/wix-headless-fast/references/cms/app/wix/cms/items.ts +13 -41
- package/skills/wix-headless-fast/references/cms/rest/items.ts +125 -0
- package/skills/wix-headless-fast/references/cms/seed/SEED.md +9 -0
- package/skills/wix-headless-fast/references/events/INSTRUCTIONS.md +239 -83
- package/skills/wix-headless-fast/references/events/app/components/events/EventConfirmationView.tsx +1 -1
- package/skills/wix-headless-fast/references/events/app/components/events/EventRegistrationView.tsx +7 -5
- package/skills/wix-headless-fast/references/events/app/components/events/EventsView.tsx +2 -2
- package/skills/wix-headless-fast/references/events/app/hooks/events/useEventRegistration.ts +28 -110
- package/skills/wix-headless-fast/references/events/app/hooks/events/useEvents.ts +19 -54
- package/skills/wix-headless-fast/references/events/app/wix/events/events-core.ts +130 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/events-store.ts +97 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/events.ts +13 -94
- package/skills/wix-headless-fast/references/events/app/wix/events/registration-core.ts +113 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/registration-store.ts +156 -0
- package/skills/wix-headless-fast/references/events/app/wix/events/registration.ts +38 -69
- package/skills/wix-headless-fast/references/events/rest/events.ts +57 -0
- package/skills/wix-headless-fast/references/events/rest/registration.ts +83 -0
- package/skills/wix-headless-fast/references/events/seed/SEED.md +9 -0
- package/skills/wix-headless-fast/references/forms/INSTRUCTIONS.md +214 -26
- package/skills/wix-headless-fast/references/forms/app/hooks/forms/useWixForm.ts +35 -267
- package/skills/wix-headless-fast/references/forms/app/wix/forms/form-store.ts +332 -0
- package/skills/wix-headless-fast/references/forms/app/wix/forms/forms-core.ts +238 -0
- package/skills/wix-headless-fast/references/forms/app/wix/forms/forms.ts +28 -243
- package/skills/wix-headless-fast/references/forms/app/wix/forms/submissions-core.ts +216 -0
- package/skills/wix-headless-fast/references/forms/app/wix/forms/submissions.ts +32 -204
- package/skills/wix-headless-fast/references/forms/rest/forms.ts +34 -0
- package/skills/wix-headless-fast/references/forms/rest/submissions.ts +56 -0
- package/skills/wix-headless-fast/references/forms/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/forms/seed/seed-forms.mjs +15 -8
- package/skills/wix-headless-fast/references/members/INSTRUCTIONS.md +314 -63
- package/skills/wix-headless-fast/references/members/app/components/members/LoginForm.tsx +1 -1
- package/skills/wix-headless-fast/references/members/app/wix/members/auth-core.ts +105 -0
- package/skills/wix-headless-fast/references/members/app/wix/members/auth.ts +36 -65
- package/skills/wix-headless-fast/references/members/app/wix/members/client.ts +1 -1
- package/skills/wix-headless-fast/references/members/app/wix/members/members-core.ts +43 -0
- package/skills/wix-headless-fast/references/members/app/wix/members/members.ts +11 -32
- package/skills/wix-headless-fast/references/members/rest/auth.ts +215 -0
- package/skills/wix-headless-fast/references/members/rest/members.ts +26 -0
- package/skills/wix-headless-fast/references/portfolio/INSTRUCTIONS.md +238 -59
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/CollectionProjectsView.tsx +2 -2
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/CollectionsView.tsx +2 -2
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/GalleryMedia.tsx +35 -0
- package/skills/wix-headless-fast/references/portfolio/app/components/portfolio/ProjectDetailView.tsx +8 -38
- package/skills/wix-headless-fast/references/portfolio/app/hooks/portfolio/useCollectionProjects.ts +23 -50
- package/skills/wix-headless-fast/references/portfolio/app/hooks/portfolio/useCollections.ts +16 -34
- package/skills/wix-headless-fast/references/portfolio/app/hooks/portfolio/useProjectDetail.ts +22 -49
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/collection-projects-store.ts +82 -0
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/collections-store.ts +58 -0
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/portfolio-core.ts +177 -0
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/portfolio.ts +31 -118
- package/skills/wix-headless-fast/references/portfolio/app/wix/portfolio/project-detail-store.ts +81 -0
- package/skills/wix-headless-fast/references/portfolio/rest/portfolio.ts +75 -0
- package/skills/wix-headless-fast/references/portfolio/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/pricing-plans/INSTRUCTIONS.md +212 -51
- package/skills/wix-headless-fast/references/pricing-plans/app/components/pricing-plans/PlanDetailView.tsx +9 -20
- package/skills/wix-headless-fast/references/pricing-plans/app/components/pricing-plans/PlansView.tsx +7 -32
- package/skills/wix-headless-fast/references/pricing-plans/app/components/pricing-plans/SubscribeButton.tsx +41 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/hooks/pricing-plans/usePlanPurchase.ts +12 -33
- package/skills/wix-headless-fast/references/pricing-plans/app/hooks/pricing-plans/usePlans.ts +17 -34
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plan-purchase-store.ts +54 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plans-core.ts +106 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plans-store.ts +64 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/plans.ts +7 -55
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/purchase-core.ts +43 -0
- package/skills/wix-headless-fast/references/pricing-plans/app/wix/pricing-plans/purchase.ts +11 -34
- package/skills/wix-headless-fast/references/pricing-plans/rest/plans.ts +31 -0
- package/skills/wix-headless-fast/references/pricing-plans/rest/purchase.ts +20 -0
- package/skills/wix-headless-fast/references/pricing-plans/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/restaurants/INSTRUCTIONS.md +240 -47
- package/skills/wix-headless-fast/references/restaurants/app/components/restaurants/MenuView.tsx +2 -2
- package/skills/wix-headless-fast/references/restaurants/app/components/restaurants/OrderCartDrawer.tsx +2 -2
- package/skills/wix-headless-fast/references/restaurants/app/hooks/restaurants/useMenus.ts +14 -35
- package/skills/wix-headless-fast/references/restaurants/app/hooks/restaurants/useReservation.ts +28 -171
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/menu-core.ts +155 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/menu-store.ts +89 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/menu.ts +23 -117
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/ordering-core.ts +124 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/ordering.ts +45 -114
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/reservation-store.ts +193 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/reservations-core.ts +94 -0
- package/skills/wix-headless-fast/references/restaurants/app/wix/restaurants/reservations.ts +15 -87
- package/skills/wix-headless-fast/references/restaurants/rest/menu.ts +60 -0
- package/skills/wix-headless-fast/references/restaurants/rest/ordering.ts +169 -0
- package/skills/wix-headless-fast/references/restaurants/rest/reservations.ts +68 -0
- package/skills/wix-headless-fast/references/restaurants/seed/SEED.md +8 -0
- package/skills/wix-headless-fast/references/shared/SUPPLIED-CONTENT.md +23 -0
- package/skills/wix-headless-fast/references/shared/app/wix/media.ts +45 -4
- package/skills/wix-headless-fast/references/shared/app/wix/sdk.ts +13 -0
- package/skills/wix-headless-fast/references/shared/rest/client.ts +155 -0
- package/skills/wix-headless-fast/references/shared/rest/config.ts +3 -0
- package/skills/wix-headless-fast/references/shared/rest/media.ts +65 -0
- package/skills/wix-headless-fast/references/storefront/INSTRUCTIONS.md +182 -63
- package/skills/wix-headless-fast/references/storefront/app/components/storefront/OptionPicker.tsx +95 -0
- package/skills/wix-headless-fast/references/storefront/app/components/storefront/QuickAdd.tsx +29 -58
- package/skills/wix-headless-fast/references/storefront/app/hooks/storefront/useProductDetail.ts +29 -163
- package/skills/wix-headless-fast/references/storefront/app/hooks/storefront/useShop.ts +26 -300
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/cart-core.ts +121 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/cart.ts +49 -162
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/catalog-core.ts +309 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/catalog.ts +47 -370
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/product-detail-store.ts +147 -0
- package/skills/wix-headless-fast/references/storefront/app/wix/storefront/shop-store.ts +289 -0
- package/skills/wix-headless-fast/references/storefront/rest/cart.ts +132 -0
- package/skills/wix-headless-fast/references/storefront/rest/catalog.ts +123 -0
- package/skills/wix-headless-fast/references/storefront/seed/SEED.md +25 -9
- package/skills/wix-headless-fast/references/storefront/seed/seed-store.mjs +6 -35
- package/skills/wix-manage/references/ecommerce/ecom-shipping.md +1 -1
- package/skills/wix-vibe-headless/references/storefront/INSTRUCTIONS.md +0 -40
- package/skills/wix-vibe-headless/references/storefront/seed/SEED.md +3 -0
- package/skills/wix-vibe-headless/references/storefront/seed/seed-store.cjs +6 -0
- package/skills/wix-headless-fast/install/pin-agents-md.mjs +0 -57
|
@@ -1,54 +1,23 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import
|
|
1
|
+
// React binding of the services store (wix/bookings/services-store.ts) — the listing state lives
|
|
2
|
+
// there, framework-free; this hook subscribes to one instance per mounted listing and exposes its
|
|
3
|
+
// state and actions under one name. SSR-friendly: pass server-fetched data as `initial*` (Astro
|
|
4
|
+
// frontmatter / server component) and no client fetch happens; a SPA passes nothing. Astro islands
|
|
5
|
+
// and React SPAs use this; a static page, Vue, or Svelte uses the store directly.
|
|
6
|
+
import { useEffect, useRef, useSyncExternalStore } from "react";
|
|
7
|
+
import { createServicesStore, type ServicesState, type ServicesStore, type ServicesStoreOptions } from "../../wix/bookings/services-store";
|
|
7
8
|
|
|
8
|
-
export
|
|
9
|
-
initialServices?: ServiceSummary[];
|
|
10
|
-
initialCategories?: BookingCategory[];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface UseServices {
|
|
14
|
-
/** null while the first load is in flight — render skeletons, not an empty state. */
|
|
15
|
-
services: ServiceSummary[] | null;
|
|
16
|
-
categories: BookingCategory[];
|
|
17
|
-
activeCategoryId: string | null;
|
|
18
|
-
setActiveCategoryId: (id: string | null) => void;
|
|
19
|
-
error: string | null;
|
|
20
|
-
}
|
|
9
|
+
export type UseServicesOptions = ServicesStoreOptions;
|
|
21
10
|
|
|
22
|
-
export
|
|
23
|
-
const [all, setAll] = useState<ServiceSummary[] | null>(initialServices ?? null);
|
|
24
|
-
const [categories, setCategories] = useState<BookingCategory[]>(initialCategories ?? []);
|
|
25
|
-
const [activeCategoryId, setActiveCategoryId] = useState<string | null>(null);
|
|
26
|
-
const [error, setError] = useState<string | null>(null);
|
|
11
|
+
export type UseServices = ServicesState & Pick<ServicesStore, "setActiveCategoryId">;
|
|
27
12
|
|
|
13
|
+
export function useServices(options: UseServicesOptions = {}): UseServices {
|
|
14
|
+
const ref = useRef<ServicesStore | null>(null);
|
|
15
|
+
if (!ref.current) ref.current = createServicesStore(options);
|
|
16
|
+
const store = ref.current;
|
|
28
17
|
useEffect(() => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!alive) return;
|
|
35
|
-
setAll([]);
|
|
36
|
-
setError(e instanceof Error ? e.message : String(e));
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
if (!initialCategories) {
|
|
40
|
-
fetchBookingCategories().then((c) => alive && setCategories(c));
|
|
41
|
-
}
|
|
42
|
-
return () => {
|
|
43
|
-
alive = false;
|
|
44
|
-
};
|
|
45
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
-
}, []);
|
|
47
|
-
|
|
48
|
-
const services = useMemo(() => {
|
|
49
|
-
if (all === null) return null;
|
|
50
|
-
return activeCategoryId ? all.filter((s) => s.categoryId === activeCategoryId) : all;
|
|
51
|
-
}, [all, activeCategoryId]);
|
|
52
|
-
|
|
53
|
-
return { services, categories, activeCategoryId, setActiveCategoryId, error };
|
|
18
|
+
store.start();
|
|
19
|
+
return () => store.stop();
|
|
20
|
+
}, [store]);
|
|
21
|
+
const state = useSyncExternalStore(store.subscribe, store.getState, store.getState);
|
|
22
|
+
return { ...state, setActiveCategoryId: store.setActiveCategoryId };
|
|
54
23
|
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Booking rules, request builders, and DTO mapping — transport-agnostic, imported by BOTH transports:
|
|
2
|
+
// ./booking.ts (the SDK) and the REST twin in references/bookings/rest/booking.ts (fetch). The
|
|
3
|
+
// availability request bodies, the slot mapper, the form-field rules, the createBooking body, the
|
|
4
|
+
// cart body, and the checkout-or-place decision live HERE, once — these are the payloads that are
|
|
5
|
+
// exact and easy to get subtly wrong. Raw entities may carry `_id` (SDK) or `id` (REST). Imports are
|
|
6
|
+
// type-only so a strip to JS emits no imports.
|
|
7
|
+
import type { BookingFormField, BookingResult, ServiceDetail, Slot } from "./types";
|
|
8
|
+
|
|
9
|
+
export type Raw = Record<string, any>;
|
|
10
|
+
|
|
11
|
+
export const rawId = (raw: Raw | undefined | null): string => raw?._id ?? raw?.id ?? "";
|
|
12
|
+
|
|
13
|
+
// Copies of services-core's ids (same values) so this file stands alone when stripped.
|
|
14
|
+
/** The Wix Bookings app id — the cart line's catalogReference.appId. */
|
|
15
|
+
export const BOOKINGS_APP_ID = "13d21c63-b5ec-5912-8397-c3a5ddb27a97";
|
|
16
|
+
/** Staff-member resource type id (ANY_RESOURCE fallback + staff filtering). */
|
|
17
|
+
export const STAFF_RESOURCE_TYPE_ID = "1cd44cf8-756f-41c3-bd90-3e2ffcaf1155";
|
|
18
|
+
|
|
19
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
20
|
+
/** Local wall-clock "YYYY-MM-DDThh:mm:ss" (NO Z) — the format the availability APIs require. */
|
|
21
|
+
export function toLocalDateString(d: Date): string {
|
|
22
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The visitor's IANA time zone — availability and the booking are computed in it. */
|
|
26
|
+
export const defaultTimeZone = (): string => Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
27
|
+
|
|
28
|
+
// ---- availability ----------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
export interface SlotsWindow {
|
|
31
|
+
from?: Date;
|
|
32
|
+
days?: number;
|
|
33
|
+
timeZone?: string;
|
|
34
|
+
staffId?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** [from, from + days) as the local strings both time-slot APIs take. */
|
|
38
|
+
export function slotsRange({ from = new Date(), days = 7 }: SlotsWindow): { fromLocalDate: string; toLocalDate: string } {
|
|
39
|
+
const to = new Date(from.getTime() + days * 24 * 3600 * 1000);
|
|
40
|
+
return { fromLocalDate: toLocalDateString(from), toLocalDate: toLocalDateString(to) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The List Availability Time Slots body for an APPOINTMENT service: bookable slots only, the staff
|
|
45
|
+
* resources included on each slot, optionally narrowed to one staff member.
|
|
46
|
+
*/
|
|
47
|
+
export function appointmentSlotsRequest(serviceId: string, { timeZone = defaultTimeZone(), staffId, ...window }: SlotsWindow): Raw {
|
|
48
|
+
return {
|
|
49
|
+
serviceId,
|
|
50
|
+
...slotsRange(window),
|
|
51
|
+
timeZone,
|
|
52
|
+
bookable: true,
|
|
53
|
+
cursorPaging: { limit: 100 },
|
|
54
|
+
includeResourceTypeIds: [STAFF_RESOURCE_TYPE_ID],
|
|
55
|
+
...(staffId ? { resourceTypes: [{ resourceTypeId: STAFF_RESOURCE_TYPE_ID, resourceIds: [staffId] }] } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The List Event Time Slots body for a CLASS service: its sessions, bookable only, optionally one staff member's. */
|
|
60
|
+
export function classSlotsRequest(serviceId: string, { timeZone = defaultTimeZone(), staffId, ...window }: SlotsWindow): Raw {
|
|
61
|
+
return {
|
|
62
|
+
serviceIds: [serviceId],
|
|
63
|
+
...slotsRange(window),
|
|
64
|
+
timeZone,
|
|
65
|
+
includeNonBookable: false,
|
|
66
|
+
...(staffId ? { eventFilter: { "resources.id": { $hasSome: [staffId] } } } : {}),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function toSlot(raw: Raw): Slot {
|
|
71
|
+
const start: string = raw.localStartDate ?? "";
|
|
72
|
+
const staff = ((raw.availableResources ?? []) as Raw[])
|
|
73
|
+
.flatMap((ar) => (ar.resources ?? []) as Raw[])
|
|
74
|
+
.map((r) => ({ id: rawId(r), name: r.name ?? "" }))
|
|
75
|
+
.filter((r) => r.id);
|
|
76
|
+
let label = start.slice(11, 16);
|
|
77
|
+
try {
|
|
78
|
+
label = new Date(start).toLocaleTimeString(undefined, { hour: "numeric", minute: "2-digit" });
|
|
79
|
+
} catch {
|
|
80
|
+
/* keep the hh:mm fallback */
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
startLocal: start,
|
|
84
|
+
endLocal: raw.localEndDate ?? "",
|
|
85
|
+
dayKey: start.slice(0, 10),
|
|
86
|
+
label,
|
|
87
|
+
scheduleId: raw.scheduleId ?? null,
|
|
88
|
+
eventId: raw.eventInfo?.eventId ?? null,
|
|
89
|
+
staff,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface BookingDay {
|
|
94
|
+
dayKey: string;
|
|
95
|
+
dayLabel: string;
|
|
96
|
+
slots: Slot[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const dayLabel = (dayKey: string): string => {
|
|
100
|
+
try {
|
|
101
|
+
return new Date(`${dayKey}T12:00:00`).toLocaleDateString(undefined, { weekday: "short", month: "short", day: "numeric" });
|
|
102
|
+
} catch {
|
|
103
|
+
return dayKey;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Slots grouped by day, days in order — what a slot picker renders. */
|
|
108
|
+
export function groupSlotsByDay(slots: Slot[]): BookingDay[] {
|
|
109
|
+
const byDay = new Map<string, Slot[]>();
|
|
110
|
+
for (const s of slots) byDay.set(s.dayKey, [...(byDay.get(s.dayKey) ?? []), s]);
|
|
111
|
+
return [...byDay.entries()]
|
|
112
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
113
|
+
.map(([dayKey, daySlots]) => ({ dayKey, dayLabel: dayLabel(dayKey), slots: daySlots }));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---- the booking form -------------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
export const FALLBACK_FIELDS: BookingFormField[] = [
|
|
119
|
+
{ target: "first_name", label: "First Name", type: "STRING", required: true },
|
|
120
|
+
{ target: "last_name", label: "Last Name", type: "STRING", required: true },
|
|
121
|
+
{ target: "email", label: "Email", type: "EMAIL", required: true },
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
const FIELD_TYPES = ["STRING", "EMAIL", "PHONE", "NUMBER", "URL"];
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Flat, render-ready fields from a form summary — deleted fields and non-text types dropped, values
|
|
128
|
+
* keyed by `target`. `required` comes from the full form schema (`fields[].validation.required`,
|
|
129
|
+
* keyed by target); the summary doesn't carry it. Without the schema every field counts as required.
|
|
130
|
+
* ALWAYS non-empty: contact basics when the schema is missing or unusable.
|
|
131
|
+
*/
|
|
132
|
+
export function toFormFields(summary: Raw | null | undefined, form?: Raw | null): BookingFormField[] {
|
|
133
|
+
const requiredByTarget = new Map<string, boolean>(
|
|
134
|
+
((form?.fields ?? []) as Raw[]).filter((f) => f.target).map((f) => [f.target as string, f.validation?.required === true]),
|
|
135
|
+
);
|
|
136
|
+
const fields = ((summary?.fields ?? []) as Raw[])
|
|
137
|
+
.filter((f) => !f.deleted)
|
|
138
|
+
.filter((f) => f.type && FIELD_TYPES.includes(f.type))
|
|
139
|
+
.map((f) => ({
|
|
140
|
+
target: f.target ?? "",
|
|
141
|
+
label: f.label ?? f.target ?? "",
|
|
142
|
+
type: f.type as BookingFormField["type"],
|
|
143
|
+
...(Array.isArray(f.options) && f.options.length ? { options: f.options as string[] } : {}),
|
|
144
|
+
required: requiredByTarget.get(f.target ?? "") ?? true,
|
|
145
|
+
}))
|
|
146
|
+
.filter((f) => f.target);
|
|
147
|
+
return fields.length ? fields : FALLBACK_FIELDS;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---- createBooking → cart → checkout-or-place --------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The Create Booking `booking` object. APPOINTMENT: the SLOT's scheduleId (never service.schedule.id)
|
|
154
|
+
* plus start/end as local wall-clock with the time zone; CLASS: the eventId. A chosen staff member
|
|
155
|
+
* is the slot's `resource`; otherwise ANY_RESOURCE of the staff type. `idKey` spells the resource's
|
|
156
|
+
* id the way the transport wants it: `_id` on the SDK, `id` on REST.
|
|
157
|
+
*/
|
|
158
|
+
export function bookingRequest(
|
|
159
|
+
service: Pick<ServiceDetail, "id" | "paymentOption">,
|
|
160
|
+
slot: Slot,
|
|
161
|
+
{ staffId, timeZone = defaultTimeZone(), idKey = "id" }: { staffId?: string; timeZone?: string; idKey?: "_id" | "id" } = {},
|
|
162
|
+
): Raw {
|
|
163
|
+
const staff = staffId ? slot.staff.find((s) => s.id === staffId) : undefined;
|
|
164
|
+
return {
|
|
165
|
+
selectedPaymentOption: service.paymentOption,
|
|
166
|
+
totalParticipants: 1,
|
|
167
|
+
bookedEntity: {
|
|
168
|
+
slot: {
|
|
169
|
+
serviceId: service.id,
|
|
170
|
+
scheduleId: slot.scheduleId ?? undefined,
|
|
171
|
+
eventId: slot.eventId ?? undefined,
|
|
172
|
+
startDate: slot.startLocal,
|
|
173
|
+
endDate: slot.endLocal,
|
|
174
|
+
timezone: timeZone,
|
|
175
|
+
...(staff
|
|
176
|
+
? { resource: { [idKey]: staff.id, name: staff.name } }
|
|
177
|
+
: { resourceSelections: [{ resourceTypeId: STAFF_RESOURCE_TYPE_ID, selectionMethod: "ANY_RESOURCE" }] }),
|
|
178
|
+
location: { locationType: "OWNER_BUSINESS" },
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The Create Cart body that holds the booked seat: one line whose catalogItemId is the BOOKING id. */
|
|
185
|
+
export function bookingCartRequest(bookingId: string): Raw {
|
|
186
|
+
return {
|
|
187
|
+
catalogItems: [{ quantity: 1, catalogReference: { catalogItemId: bookingId, appId: BOOKINGS_APP_ID } }],
|
|
188
|
+
cart: { source: { channelType: "WEB" } },
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The booking id out of a Create Booking response (wrapped { booking } on both transports). */
|
|
193
|
+
export const bookingIdOf = (res: Raw | null | undefined): string => rawId(res?.booking);
|
|
194
|
+
|
|
195
|
+
/** The cart id out of a Create Cart response — the SDK may unwrap the cart, REST wraps it. */
|
|
196
|
+
export const cartIdOf = (res: Raw | null | undefined): string => rawId(res) || rawId(res?.cart);
|
|
197
|
+
|
|
198
|
+
/** The cart's total from a Calculate Cart response (`summary.priceSummary.total`); 0 when absent. */
|
|
199
|
+
export const cartTotal = (calc: Raw | null | undefined): number => Number(calc?.summary?.priceSummary?.total?.amount ?? 0);
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Hosted checkout is required for anything that costs money paid online, and always when the
|
|
203
|
+
* service carries a cancellation fee (a card must be on file). Otherwise the order is placed
|
|
204
|
+
* directly — free and pay-in-person bookings confirm without a checkout.
|
|
205
|
+
*/
|
|
206
|
+
export function checkoutRequired(service: Pick<ServiceDetail, "paymentOption" | "cancellationFeeEnabled">, total: number): boolean {
|
|
207
|
+
return service.cancellationFeeEnabled || (total > 0 && service.paymentOption !== "OFFLINE");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The Create Redirect Session body for a cart's checkout; the cart id IS the checkout id. */
|
|
211
|
+
export function checkoutRedirectRequest(cartId: string, origin: string): Raw {
|
|
212
|
+
return { ecomCheckout: { checkoutId: cartId }, callbacks: origin ? { postFlowUrl: `${origin}/` } : {} };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A placed order → the confirmed result; the order id is wherever the transport put it. */
|
|
216
|
+
export function confirmedResult(bookingId: string, order: Raw | null | undefined): BookingResult {
|
|
217
|
+
return { kind: "confirmed", bookingId, orderId: order?.orderId ?? (rawId(order?.order) || null) };
|
|
218
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// The whole booking state machine for one service as a framework-free store — the logic behind
|
|
2
|
+
// useBookingFlow, usable from React (useBookingFlow wraps it), from a static page's booking surface,
|
|
3
|
+
// from Vue/Svelte, or as the specification for a port: the availability window (day-grouped slots,
|
|
4
|
+
// week paging, optional staff filter), the schema-driven form, and book(). All correctness (slot
|
|
5
|
+
// scheduleId vs eventId, ANY_RESOURCE, formSubmission, payment option, checkout-or-place) lives in
|
|
6
|
+
// the data layer; this store orchestrates. A late response from a superseded window is dropped.
|
|
7
|
+
// One store per booking surface: createBookingFlowStore(service), not a singleton.
|
|
8
|
+
import { bookService, fetchBookingForm, fetchSlots } from "./booking";
|
|
9
|
+
import { groupSlotsByDay, type BookingDay } from "./booking-core";
|
|
10
|
+
import type { BookingFormField, BookingResult, ServiceDetail, Slot } from "./types";
|
|
11
|
+
|
|
12
|
+
export type { BookingDay };
|
|
13
|
+
|
|
14
|
+
const WEEK = 7 * 24 * 3600 * 1000;
|
|
15
|
+
|
|
16
|
+
/** Everything a booking surface renders from. Read it with getState() or through a subscription. */
|
|
17
|
+
export interface BookingFlowState {
|
|
18
|
+
/** Slots grouped by day, in order — [{ dayKey, dayLabel, slots }]. null while loading. */
|
|
19
|
+
days: BookingDay[] | null;
|
|
20
|
+
/** The 7-day window start; page with nextWeek/prevWeek (prev clamps to today). */
|
|
21
|
+
windowStart: Date;
|
|
22
|
+
/** Staff filter — render a picker only when service.staff.length > 1. */
|
|
23
|
+
staffId: string | undefined;
|
|
24
|
+
selectedSlot: Slot | null;
|
|
25
|
+
/** Schema-driven form fields (never empty — contact basics fallback). */
|
|
26
|
+
formFields: BookingFormField[];
|
|
27
|
+
values: Record<string, string>;
|
|
28
|
+
/** True when a slot is selected and every field has a value. */
|
|
29
|
+
canBook: boolean;
|
|
30
|
+
booking: boolean;
|
|
31
|
+
/** Set after a free/offline booking completes — the only REAL success signal. */
|
|
32
|
+
confirmed: BookingResult | null;
|
|
33
|
+
error: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface BookingFlowStore {
|
|
37
|
+
getState(): BookingFlowState;
|
|
38
|
+
subscribe(listener: () => void): () => void;
|
|
39
|
+
/** Load the first window and the form. Call once when mounted (a browser — availability is timezone-specific). */
|
|
40
|
+
start(): void;
|
|
41
|
+
/** Stop reacting; drop late responses. */
|
|
42
|
+
stop(): void;
|
|
43
|
+
nextWeek(): void;
|
|
44
|
+
prevWeek(): void;
|
|
45
|
+
setStaffId(id: string | undefined): void;
|
|
46
|
+
setSelectedSlot(slot: Slot | null): void;
|
|
47
|
+
setValue(target: string, value: string): void;
|
|
48
|
+
/** Books the selected slot. On "redirect" the browser is already navigating; otherwise `confirmed` is set. Rejects with the refusal (also in .error). */
|
|
49
|
+
book(): Promise<BookingResult>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createBookingFlowStore(service: ServiceDetail): BookingFlowStore {
|
|
53
|
+
let windowStart = new Date();
|
|
54
|
+
let staffId: string | undefined;
|
|
55
|
+
let slots: Slot[] | null = null;
|
|
56
|
+
let selectedSlot: Slot | null = null;
|
|
57
|
+
let formFields: BookingFormField[] = [];
|
|
58
|
+
let values: Record<string, string> = {};
|
|
59
|
+
let booking = false;
|
|
60
|
+
let confirmed: BookingResult | null = null;
|
|
61
|
+
let error: string | null = null;
|
|
62
|
+
let started = false;
|
|
63
|
+
let generation = 0;
|
|
64
|
+
const listeners = new Set<() => void>();
|
|
65
|
+
let snapshot: BookingFlowState | null = null;
|
|
66
|
+
const emit = () => {
|
|
67
|
+
snapshot = null;
|
|
68
|
+
for (const fn of listeners) fn();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function getState(): BookingFlowState {
|
|
72
|
+
if (snapshot) return snapshot;
|
|
73
|
+
snapshot = {
|
|
74
|
+
days: slots === null ? null : groupSlotsByDay(slots),
|
|
75
|
+
windowStart,
|
|
76
|
+
staffId,
|
|
77
|
+
selectedSlot,
|
|
78
|
+
formFields,
|
|
79
|
+
values,
|
|
80
|
+
canBook: !!selectedSlot && formFields.every((f) => !f.required || (values[f.target] ?? "").trim().length > 0),
|
|
81
|
+
booking,
|
|
82
|
+
confirmed,
|
|
83
|
+
error,
|
|
84
|
+
};
|
|
85
|
+
return snapshot;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// The window or the staff filter changed: clear the picker and load the slots for the new selection.
|
|
89
|
+
function loadSlots(): void {
|
|
90
|
+
if (!started) return;
|
|
91
|
+
const id = ++generation;
|
|
92
|
+
slots = null;
|
|
93
|
+
selectedSlot = null;
|
|
94
|
+
emit();
|
|
95
|
+
fetchSlots(service, { from: windowStart, days: 7, staffId })
|
|
96
|
+
.then((s) => {
|
|
97
|
+
if (!started || generation !== id) return; // superseded — drop it
|
|
98
|
+
slots = s;
|
|
99
|
+
emit();
|
|
100
|
+
})
|
|
101
|
+
.catch((e) => {
|
|
102
|
+
if (!started || generation !== id) return;
|
|
103
|
+
slots = [];
|
|
104
|
+
error = e instanceof Error ? e.message : String(e);
|
|
105
|
+
emit();
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
getState,
|
|
111
|
+
subscribe(fn) {
|
|
112
|
+
listeners.add(fn);
|
|
113
|
+
return () => listeners.delete(fn);
|
|
114
|
+
},
|
|
115
|
+
start() {
|
|
116
|
+
if (started) return;
|
|
117
|
+
started = true;
|
|
118
|
+
loadSlots();
|
|
119
|
+
fetchBookingForm(service.formId).then((f) => { if (started) { formFields = f; emit(); } });
|
|
120
|
+
},
|
|
121
|
+
stop() {
|
|
122
|
+
started = false;
|
|
123
|
+
generation++;
|
|
124
|
+
},
|
|
125
|
+
nextWeek() {
|
|
126
|
+
windowStart = new Date(windowStart.getTime() + WEEK);
|
|
127
|
+
loadSlots();
|
|
128
|
+
},
|
|
129
|
+
prevWeek() {
|
|
130
|
+
const prev = new Date(windowStart.getTime() - WEEK);
|
|
131
|
+
windowStart = prev < new Date() ? new Date() : prev;
|
|
132
|
+
loadSlots();
|
|
133
|
+
},
|
|
134
|
+
setStaffId(id) {
|
|
135
|
+
if (id === staffId) return;
|
|
136
|
+
staffId = id;
|
|
137
|
+
loadSlots();
|
|
138
|
+
},
|
|
139
|
+
setSelectedSlot(slot) {
|
|
140
|
+
selectedSlot = slot;
|
|
141
|
+
emit();
|
|
142
|
+
},
|
|
143
|
+
setValue(target, value) {
|
|
144
|
+
values = { ...values, [target]: value };
|
|
145
|
+
emit();
|
|
146
|
+
},
|
|
147
|
+
async book() {
|
|
148
|
+
if (!selectedSlot) throw new Error("Pick a time first.");
|
|
149
|
+
booking = true;
|
|
150
|
+
error = null;
|
|
151
|
+
emit();
|
|
152
|
+
try {
|
|
153
|
+
const result = await bookService(service, selectedSlot, values, { staffId });
|
|
154
|
+
if (result.kind === "redirect") {
|
|
155
|
+
window.location.href = result.url;
|
|
156
|
+
} else {
|
|
157
|
+
confirmed = result;
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
error = e instanceof Error ? e.message : String(e);
|
|
162
|
+
throw e;
|
|
163
|
+
} finally {
|
|
164
|
+
booking = false;
|
|
165
|
+
emit();
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|