@zappie/foundations 0.10.0 → 0.11.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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @zappie/foundations
2
2
 
3
+ ## 0.11.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1444c8d: `Schedule` and `scheduleSlots` follow each menu's own ordering window, not only the branch's. A bag gets only the times every menu in it allows: the longest notice a pre-order menu asks for, the shortest days-ahead limit, and the hours all its menus are served. A pre-order menu's days-ahead limit no longer reaches past the branch's `orderAheadDays`. At a branch that takes orders only while open, a pre-order menu still uses its own limit.
8
+
9
+ `Schedule` reads the bag's menus from the source menus of the cart's lines, or from `menuIds` when you pass it. `canAsap` is true only while every menu in the bag is served and none needs notice. When nothing can be ordered, `blocker` says why and names the menu that blocks the bag.
10
+
11
+ `scheduleSlots` moves to `@zappie/foundations/schedule` with the same signature, so the scheduling rules stay out of the initial entry. Import it from there instead of `@zappie/foundations`. `Schedule` loads the rules when it first mounts, and `status` stays `loading` until they arrive. A test with fake timers can wait for them with `vi.dynamicImportSettled()`.
12
+
13
+ `scheduleSlots` takes the bag's `menus`. `scheduleDays(branch, timezone, now, menus, { days })` answers, for each day from today, whether the menus can be ordered for that day, with its slots or the reason: the branch is closed, a menu isn't served that day, the day is too far ahead, or it is too soon for the notice the menu needs. `scheduleBlocker` gives the same reason for the whole bag. Both come from `@zappie/foundations/schedule`. The types `ScheduleBlock`, `ScheduleDay`, `ScheduleMenu`, `ScheduleOptions` and `ScheduleSlot` come from either entry. The initial entry grows by about 30 B gzip.
14
+
15
+ ## 0.10.1
16
+
17
+ ### Patch Changes
18
+
19
+ - ab4162d: `@zappie/sdk` is now a peer dependency, so foundations and the base theme use your site's own SDK. Before, a package manager could install a second, older SDK inside foundations, and TypeScript then refused a `Branch` or `Item` from foundations where your site expected its own. Keep `@zappie/sdk` in your site's dependencies, version 0.10.3 or later.
20
+
3
21
  ## 0.10.0
4
22
 
5
23
  ### Minor Changes
@@ -1,76 +1,12 @@
1
1
  "use client";
2
2
 
3
3
  // src/fulfilment/schedule.tsx
4
- import { useMenu, useStore, useZappie } from "@zappie/sdk/react";
4
+ import { useCart, useMenu, useStore, useZappie } from "@zappie/sdk/react";
5
5
  import { forwardRef, useEffect, useMemo, useState } from "react";
6
6
  import { part } from "../internal/slot.js";
7
7
  import { useControllableState } from "../internal/use-controllable-state.js";
8
- import {
9
- canOrderAhead,
10
- getOpenStatus,
11
- localParts,
12
- openingIntervals
13
- } from "../status/hours.js";
14
- function scheduleSlots(branch, timezone, now, {
15
- intervalMinutes = 30,
16
- maxDays = 90,
17
- noticeMinutes = 0,
18
- menuSchedule
19
- } = {}) {
20
- if (!Number.isInteger(intervalMinutes) || intervalMinutes < 1 || !Number.isInteger(maxDays) || maxDays < 1 || maxDays > 90 || !Number.isFinite(noticeMinutes) || noticeMinutes < 0) {
21
- throw new RangeError(
22
- "Schedule requires a positive interval, 1\u201390 days and a non-negative notice"
23
- );
24
- }
25
- const preorder = menuSchedule?.preorder ? menuSchedule : void 0;
26
- if (!branch.fulfilment.scheduling || !preorder && !canOrderAhead(branch)) {
27
- return [];
28
- }
29
- const days = Math.min(
30
- maxDays,
31
- preorder?.preorderMaxDays && preorder.preorderMaxDays > 0 ? preorder.preorderMaxDays : branch.orderAheadDays ?? 14
32
- );
33
- const earliest = now.getTime() + Math.max(noticeMinutes, (preorder?.preorderNoticeHours ?? 0) * 60) * 6e4;
34
- const latest = now.getTime() + days * 864e5;
35
- const menuIntervals = menuSchedule?.openingTimes.length ? openingIntervals(
36
- {
37
- ...branch,
38
- openingTimes: menuSchedule.openingTimes,
39
- specialHours: []
40
- },
41
- timezone,
42
- now,
43
- days + 1
44
- ) : void 0;
45
- const slots = [];
46
- for (const { start, end } of openingIntervals(
47
- branch,
48
- timezone,
49
- now,
50
- days + 1
51
- )) {
52
- const first = start + Math.max(0, Math.ceil((earliest - start) / (intervalMinutes * 6e4))) * intervalMinutes * 6e4;
53
- for (let instant = first; instant < end && instant <= latest; instant += intervalMinutes * 6e4) {
54
- if (menuIntervals && !menuIntervals.some(
55
- (interval) => interval.start <= instant && interval.end > instant
56
- )) {
57
- continue;
58
- }
59
- const date = new Date(instant);
60
- slots.push({
61
- value: date.toISOString(),
62
- date: localParts(date, timezone).date,
63
- label: new Intl.DateTimeFormat("en", {
64
- timeZone: timezone,
65
- hour: "numeric",
66
- minute: "2-digit",
67
- timeZoneName: "shortOffset"
68
- }).format(date)
69
- });
70
- }
71
- }
72
- return slots;
73
- }
8
+ var windowModule;
9
+ var windowLoad;
74
10
  var Schedule = /* @__PURE__ */ forwardRef(function Schedule2({
75
11
  branch,
76
12
  timezone: suppliedTimezone,
@@ -80,13 +16,35 @@ var Schedule = /* @__PURE__ */ forwardRef(function Schedule2({
80
16
  intervalMinutes,
81
17
  maxDays,
82
18
  noticeMinutes,
19
+ menuIds,
83
20
  ...props
84
21
  }, ref) {
85
22
  const client = useZappie();
86
23
  const store = useStore();
87
24
  const menu = useMenu(branch.id, { metadataOnly: true });
25
+ const cart = useCart();
26
+ const ids = (menuIds ?? cart.lines.map((line) => line.menuId)).join(" ");
88
27
  const [retrying, setRetrying] = useState(false);
89
- const status = menu.isLoading || retrying ? "loading" : menu.error ? "error" : "ready";
28
+ const [rules, setRules] = useState(windowModule);
29
+ const [failed, setFailed] = useState(false);
30
+ const load = () => {
31
+ (windowLoad ?? (windowLoad = import("./window.js"))).then(
32
+ (module) => {
33
+ windowModule = module;
34
+ setRules(module);
35
+ },
36
+ () => {
37
+ windowLoad = void 0;
38
+ setFailed(true);
39
+ }
40
+ );
41
+ };
42
+ useEffect(() => {
43
+ if (!rules) {
44
+ load();
45
+ }
46
+ }, []);
47
+ const status = menu.isLoading || retrying || !rules && !failed ? "loading" : menu.error || failed ? "error" : "ready";
90
48
  const ready = status === "ready";
91
49
  const timezone = suppliedTimezone ?? branch.timezone ?? store.data?.timezone ?? "UTC";
92
50
  const [now, setNow] = useState(() => /* @__PURE__ */ new Date());
@@ -102,30 +60,36 @@ var Schedule = /* @__PURE__ */ forwardRef(function Schedule2({
102
60
  defaultValue,
103
61
  onChange: onValueChange
104
62
  });
105
- const slots = useMemo(
106
- () => !ready ? [] : scheduleSlots(branch, timezone, now, {
63
+ const result = useMemo(
64
+ () => rules?.bagWindow(branch, timezone, now, menu.data, ids, {
107
65
  intervalMinutes,
108
66
  maxDays,
109
- noticeMinutes,
110
- menuSchedule: menu.data?.schedule
67
+ noticeMinutes
111
68
  }),
112
69
  [
70
+ rules,
113
71
  branch,
114
72
  timezone,
115
73
  now,
74
+ menu.data,
75
+ ids,
116
76
  intervalMinutes,
117
77
  maxDays,
118
- noticeMinutes,
119
- menu.data?.schedule,
120
- ready
78
+ noticeMinutes
121
79
  ]
122
80
  );
81
+ const slots = ready && result?.slots || [];
123
82
  const days = Array.from(new Set(slots.map((slot) => slot.date))).map(
124
83
  (date) => ({ date, slots: slots.filter((slot) => slot.date === date) })
125
84
  );
126
- const canAsap = ready && !menu.data?.schedule?.preorder && getOpenStatus(branch, timezone, now).isOpen;
85
+ const canAsap = ready && result.canAsap;
86
+ const isClosed = ready && !canAsap && slots.length === 0;
127
87
  const isValid = selection === null ? canAsap : slots.some((slot) => slot.value === selection);
128
88
  const retry = () => {
89
+ if (failed) {
90
+ setFailed(false);
91
+ load();
92
+ }
129
93
  if (retrying || !menu.error) {
130
94
  return;
131
95
  }
@@ -144,7 +108,8 @@ var Schedule = /* @__PURE__ */ forwardRef(function Schedule2({
144
108
  {
145
109
  status,
146
110
  retry,
147
- isClosed: ready && !canAsap && slots.length === 0,
111
+ isClosed,
112
+ blocker: isClosed ? result.why() : void 0,
148
113
  days,
149
114
  slots,
150
115
  value: selection,
@@ -164,7 +129,6 @@ var Schedule = /* @__PURE__ */ forwardRef(function Schedule2({
164
129
  );
165
130
  });
166
131
  export {
167
- Schedule,
168
- scheduleSlots
132
+ Schedule
169
133
  };
170
134
  //# sourceMappingURL=schedule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
3
+ canOrderAhead,
4
+ getOpenStatus,
5
+ localParts,
6
+ openingIntervals
7
+ } from "../status/hours.js";
8
+ var labels = /* @__PURE__ */ new Map();
9
+ function grid(branch, timezone, now, menus, step, days, earliest) {
10
+ const latest = now.getTime() + days * 864e5;
11
+ const menuIntervals = menus.flatMap(
12
+ ({ schedule }) => schedule?.openingTimes.length ? [
13
+ openingIntervals(
14
+ {
15
+ ...branch,
16
+ openingTimes: schedule.openingTimes,
17
+ specialHours: []
18
+ },
19
+ timezone,
20
+ now,
21
+ days + 1
22
+ )
23
+ ] : []
24
+ );
25
+ let label = labels.get(timezone);
26
+ if (!label) {
27
+ label = new Intl.DateTimeFormat("en", {
28
+ timeZone: timezone,
29
+ hour: "numeric",
30
+ minute: "2-digit",
31
+ timeZoneName: "shortOffset"
32
+ });
33
+ labels.set(timezone, label);
34
+ }
35
+ const slots = [];
36
+ for (const { start, end } of openingIntervals(
37
+ branch,
38
+ timezone,
39
+ now,
40
+ days + 1
41
+ )) {
42
+ for (let instant = start + Math.max(0, Math.ceil((earliest - start) / step)) * step; instant < end && instant <= latest; instant += step) {
43
+ if (menuIntervals.every(
44
+ (intervals) => intervals.some(
45
+ (interval) => interval.start <= instant && interval.end > instant
46
+ )
47
+ )) {
48
+ const date = new Date(instant);
49
+ slots.push({
50
+ value: date.toISOString(),
51
+ date: localParts(date, timezone).date,
52
+ label: label.format(date)
53
+ });
54
+ }
55
+ }
56
+ }
57
+ return slots;
58
+ }
59
+ function explain(branch, timezone, now, menus, {
60
+ intervalMinutes = 30,
61
+ maxDays = 90,
62
+ noticeMinutes = 0
63
+ } = {}, span = 0) {
64
+ if (!Number.isInteger(intervalMinutes) || intervalMinutes < 1 || !Number.isInteger(maxDays) || maxDays < 1 || maxDays > 90 || !(noticeMinutes >= 0 && noticeMinutes < Infinity) || !Number.isInteger(span) || span < 0 || span > 90) {
65
+ throw new RangeError(
66
+ "Schedule requires a positive interval, 1\u201390 days and a non-negative notice"
67
+ );
68
+ }
69
+ const preorders = menus.filter((menu) => menu.schedule?.preorder);
70
+ const ahead = branch.orderAheadDays;
71
+ let days = Math.min(
72
+ maxDays,
73
+ ahead > 0 || !preorders.some((menu) => menu.schedule.preorderMaxDays > 0) ? ahead ?? 14 : 90
74
+ );
75
+ let notice = noticeMinutes;
76
+ let daysMenu;
77
+ let noticeMenu;
78
+ for (const { id, schedule } of preorders) {
79
+ const limit = schedule.preorderMaxDays;
80
+ if (limit > 0 && limit < days) {
81
+ days = limit;
82
+ daysMenu = id;
83
+ }
84
+ if (schedule.preorderNoticeHours * 60 > notice) {
85
+ notice = schedule.preorderNoticeHours * 60;
86
+ noticeMenu = id;
87
+ }
88
+ }
89
+ const step = intervalMinutes * 6e4;
90
+ const scheduling = branch.fulfilment.scheduling && (preorders.length > 0 || canOrderAhead(branch));
91
+ const slots = scheduling ? grid(
92
+ branch,
93
+ timezone,
94
+ now,
95
+ menus,
96
+ step,
97
+ days,
98
+ now.getTime() + notice * 6e4
99
+ ) : [];
100
+ let why;
101
+ return {
102
+ slots,
103
+ why(date) {
104
+ if (!scheduling) {
105
+ return { reason: "no-scheduling" };
106
+ }
107
+ if (!why) {
108
+ const open = (list) => grid(
109
+ branch,
110
+ timezone,
111
+ now,
112
+ list,
113
+ step,
114
+ Math.max(span, days, 7),
115
+ -Infinity
116
+ );
117
+ const branchOpen = open([]);
118
+ const menusOpen = menus.map(
119
+ (_, index) => open(menus.slice(0, index + 1))
120
+ );
121
+ why = (on) => {
122
+ const at = (list) => list.filter(
123
+ (slot) => on ? slot.date === on : Date.parse(slot.value) >= now.getTime()
124
+ );
125
+ const index = menusOpen.findIndex((list) => !at(list).length);
126
+ return at(slots).length ? void 0 : !at(branchOpen).length ? { reason: "closed" } : index >= 0 ? { reason: "menu-closed", menuId: menus[index].id } : at(menusOpen.at(-1) ?? branchOpen).every(
127
+ (slot) => Date.parse(slot.value) > now.getTime() + days * 864e5
128
+ ) ? { reason: "too-far", maxDays: days, menuId: daysMenu } : {
129
+ reason: "too-soon",
130
+ noticeHours: notice / 60,
131
+ menuId: noticeMenu
132
+ };
133
+ };
134
+ }
135
+ return why(date);
136
+ }
137
+ };
138
+ }
139
+ function bagWindow(branch, timezone, now, menu, ids, options) {
140
+ const sections = menu?.menus?.filter(
141
+ (section) => ids.split(" ").includes(section.id)
142
+ );
143
+ const menus = sections?.length ? sections : menu?.schedule ? [menu] : [];
144
+ return {
145
+ ...explain(branch, timezone, now, menus, options),
146
+ canAsap: getOpenStatus(branch, timezone, now).isOpen && menus.every(
147
+ ({ schedule }) => !schedule?.preorder && (!schedule?.openingTimes.length || getOpenStatus({ ...branch, ...schedule }, timezone, now).isOpen)
148
+ )
149
+ };
150
+ }
151
+ export {
152
+ bagWindow,
153
+ explain
154
+ };
155
+ //# sourceMappingURL=window.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/fulfilment/window.ts"],
4
+ "mappings": ";AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAqCP,IAAM,SAAS,oBAAI,IAAiC;AACpD,SAAS,KACP,QACA,UACA,KACA,OACA,MACA,MACA,UACA;AACA,QAAM,SAAS,IAAI,QAAQ,IAAI,OAAO;AACtC,QAAM,gBAAgB,MAAM;AAAA,IAAQ,CAAC,EAAE,SAAS,MAC9C,UAAU,aAAa,SACnB;AAAA,MACE;AAAA,QACE;AAAA,UACE,GAAG;AAAA,UACH,cAAc,SAAS;AAAA,UACvB,cAAc,CAAC;AAAA,QACjB;AAAA,QACA;AAAA,QACA;AAAA,QACA,OAAO;AAAA,MACT;AAAA,IACF,IACA,CAAC;AAAA,EACP;AACA,MAAI,QAAQ,OAAO,IAAI,QAAQ;AAC/B,MAAI,CAAC,OAAO;AACV,YAAQ,IAAI,KAAK,eAAe,MAAM;AAAA,MACpC,UAAU;AAAA,MACV,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,cAAc;AAAA,IAChB,CAAC;AACD,WAAO,IAAI,UAA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K,EAAE,SACb,SACA,CAAC,GAAG,UAAU,EAAE,SACd,EAAE,QAAQ,SAAS,IACnB,SAAS,IACP,EAAE,QAAQ,eAAe,QAAQ,MAAM,KAAK,EAAG,GAAG,IAClD,GAAG,UAAU,GAAG,EAAE,KAAK,UAAU,EAAE;AAAA,YAC/B,CAAC,SACC,KAAK,MAAM,KAAK,KAAK,IACrB,IAAI,QAAQ,IAAI,OAAO;AAAA,UAC3B,IACA,EAAE,QAAQ,WAAW,SAAS,MAAM,QAAQ,SAAS,IACrD;AAAA,YACE,QAAQ;AAAA,YACR,aAAa,SAAS;AAAA,YACtB,QAAQ;AAAA,UACV;AAAA,QACZ;AAAA,MACF;AACA,aAAO,IAAI,IAAI;AAAA,IACjB;AAAA,EACF;AACF;AAMO,SAAS,UACd,QACA,UACA,KACA,MACA,KACA,SACA;AACA,QAAM,WAAW,MAAM,OAAO;AAAA,IAAO,CAAC,YACpC,IAAI,MAAM,GAAG,EAAE,SAAS,QAAQ,EAAE;AAAA,EACpC;AACA,QAAM,QAAwB,UAAU,SACpC,WACA,MAAM,WACJ,CAAC,IAAI,IACL,CAAC;AACP,SAAO;AAAA,IACL,GAAG,QAAQ,QAAQ,UAAU,KAAK,OAAO,OAAO;AAAA,IAChD,SACE,cAAc,QAAQ,UAAU,GAAG,EAAE,UACrC,MAAM;AAAA,MACJ,CAAC,EAAE,SAAS,MACV,CAAC,UAAU,aACV,CAAC,UAAU,aAAa,UACvB,cAAc,EAAE,GAAG,QAAQ,GAAG,SAAS,GAAG,UAAU,GAAG,EAAE;AAAA,IAC/D;AAAA,EACJ;AACF;",
5
+ "names": []
6
+ }
package/dist/index.d.ts CHANGED
@@ -4,10 +4,12 @@ import * as react from 'react';
4
4
  import { HTMLAttributes, ReactNode, KeyboardEvent as KeyboardEvent$1, ImgHTMLAttributes, ChangeEvent, ClipboardEvent } from 'react';
5
5
  import { ZappieEnvironment } from '@zappie/sdk/services';
6
6
  import * as _zappie_sdk from '@zappie/sdk';
7
- import { Branch as Branch$1, CartLine, Money as Money$1, CartLineInput, CartIssue, CartFix, Quote, DeliveryAddress, MenuSchedule, CartFulfilment, OptionGroup as OptionGroup$1, Item, Choice as Choice$1, MoneyFormatOptions, PaymentMethodKind, Category, Menu, Store, OpeningTime } from '@zappie/sdk';
7
+ import { Branch as Branch$1, CartLine, Money as Money$1, CartLineInput, CartIssue, CartFix, Quote, DeliveryAddress, CartFulfilment, OptionGroup as OptionGroup$1, Item, Choice as Choice$1, MoneyFormatOptions, PaymentMethodKind, Category, Menu, Store, OpeningTime } from '@zappie/sdk';
8
8
  import { Coordinates } from '@zappie/sdk/branches';
9
9
  import { useCart } from '@zappie/sdk/react';
10
10
  export { D as Deferred, a as DeferredIdleOptions, b as DeferredProps, I as IntentPreloadHandlers, d as deferred, u as useIntentPreload } from './index-B1NMxq0i.js';
11
+ import { S as ScheduleBlock, a as ScheduleSlot } from './window-BAFAfQnw.js';
12
+ export { b as ScheduleDay, c as ScheduleMenu, d as ScheduleOptions } from './window-BAFAfQnw.js';
11
13
  import { C as CloseOptions, O as OverlayQuery } from './history-BlytQinP.js';
12
14
  import { SelectionControl } from '@zappie/sdk/options';
13
15
  import '@base-ui/react/dialog';
@@ -424,14 +426,6 @@ declare const Branch: {
424
426
  } & react.RefAttributes<HTMLElement>>;
425
427
  };
426
428
 
427
- /**
428
- * A selectable time with an ISO instant, local date and display label including the UTC offset.
429
- */
430
- type ScheduleSlot = {
431
- value: string;
432
- label: string;
433
- date: string;
434
- };
435
429
  /**
436
430
  * Whether the branch's menu hours have loaded. `loading` and `error` say nothing about whether the
437
431
  * branch is open.
@@ -445,12 +439,14 @@ type ScheduleStatus = "loading" | "error" | "ready";
445
439
  * the branch is closed is only right when `isClosed` is true.
446
440
  */
447
441
  type ScheduleState = {
448
- /** `loading` while the menu hours load, `error` when they failed, and `ready` once they're known. */
442
+ /** `loading` while the menu hours and scheduling rules load, `error` when either failed, and `ready` once both are known. */
449
443
  status: ScheduleStatus;
450
444
  /** Load the menu hours again after an error. */
451
445
  retry: () => void;
452
- /** True when the hours are known and the branch takes neither ASAP nor scheduled orders. */
446
+ /** True when the hours are known and the bag can be ordered neither ASAP nor for a later slot. */
453
447
  isClosed: boolean;
448
+ /** Why nothing can be ordered when `isClosed` is true, naming the menu that blocks the bag when one does. */
449
+ blocker?: ScheduleBlock | undefined;
454
450
  days: {
455
451
  date: string;
456
452
  slots: ScheduleSlot[];
@@ -459,30 +455,14 @@ type ScheduleState = {
459
455
  value: string | null;
460
456
  setValue: (value: string | null) => void;
461
457
  isAsap: boolean;
462
- /** True when the hours are known and the branch takes an order now. */
458
+ /** True when the hours are known, the branch takes an order now and every menu in the bag is served now without notice. */
463
459
  canAsap: boolean;
464
460
  isValid: boolean;
465
461
  };
466
- /**
467
- * Generate scheduling slots within branch hours and any supplied menu hours and preorder limits.
468
- * Slots reach as far as the branch's `orderAheadDays`, or a pre-order menu's own limit, and never
469
- * past `maxDays` when you pass it. A closed branch still gets its later slots. Returns no slots
470
- * when the branch doesn't take orders ahead (`canOrderAhead`), unless the menu is a pre-order menu.
471
- *
472
- * @param branch - The branch whose hours and scheduling capability apply.
473
- * @param timezone - The time zone used to interpret opening hours and label slots.
474
- * @param now - The instant from which notice and scheduling limits are measured.
475
- * @returns Slots with ISO values, local dates and display labels.
476
- */
477
- declare function scheduleSlots(branch: Branch$1, timezone: string, now: Date, { intervalMinutes, maxDays, noticeMinutes, menuSchedule, }?: {
478
- intervalMinutes?: number | undefined;
479
- maxDays?: number | undefined;
480
- noticeMinutes?: number | undefined;
481
- menuSchedule?: MenuSchedule | undefined;
482
- }): ScheduleSlot[];
483
462
  /**
484
463
  * Expose valid fulfilment slots and ASAP eligibility using branch hours and loaded menu metadata.
485
- * Use null for ASAP. Requires a ZappieProvider and refreshes the available times each minute.
464
+ * The slots fit every menu in the bag: `menuIds`, or else the source menus of the cart's lines.
465
+ * See `scheduleSlots` for how the menus combine. Use null for ASAP. Requires a ZappieProvider and refreshes the available times each minute.
486
466
  * While the menu hours load or after they fail, `status` says so. Don't show those states as closed.
487
467
  */
488
468
  declare const Schedule: react.ForwardRefExoticComponent<Omit<react.AllHTMLAttributes<HTMLElement>, PartOwnedAttributes> & {
@@ -497,6 +477,8 @@ declare const Schedule: react.ForwardRefExoticComponent<Omit<react.AllHTMLAttrib
497
477
  intervalMinutes?: number | undefined;
498
478
  maxDays?: number | undefined;
499
479
  noticeMinutes?: number;
480
+ /** The bag's source menus. Defaults to the menus of the cart's lines. */
481
+ menuIds?: readonly string[] | undefined;
500
482
  } & react.RefAttributes<HTMLElement>>;
501
483
 
502
484
  type FulfilmentState = {
@@ -1163,5 +1145,5 @@ declare const OpenStatus: react.ForwardRefExoticComponent<Omit<react.AllHTMLAttr
1163
1145
  /** The installed @zappie/foundations version. */
1164
1146
  declare const FOUNDATIONS_VERSION: string;
1165
1147
 
1166
- export { Account, AccountNotConfiguredError, Address, Branch, BranchMap, Cart, Choice, DeliveryMap, FOUNDATIONS_VERSION, Fulfilment, Image, ItemForm, ItemNote, Money, OpenStatus, OptionGroup, Otp, Overlay, PartProps, PaymentMethods, Quantity, Schedule, Search, Totals, ZappieAccountLink, canOrderAhead, contactLinks, createSearchIndex, formatOpeningHours, formatWallClock, getOpenStatus, parseWallClock, preloadOverlay, scheduleSlots, useControllableState, useDragToClose, useOverlay, useZappieAccountUrl, zappieAccountUrl, zappieEnvironment, zappieSupportUrl };
1167
- export type { ActiveGroup, AddressState, AddressSuggestion, BranchMapLoadErrorState, BranchMapProps, ContactLink, Customer, DeliveryMapProps, DragDirection, DragToCloseOptions, ImageProps, ItemFormContext, ItemFormInvalidGroup, ItemFormProps, MapLoadErrorState, NoteState, PaymentMethod, QuantityProps, QuantityState, ScheduleSlot, ScheduleState, ScheduleStatus, SearchResult, SearchSource, SearchState, SignInOptions, TotalsState, ZappieSupportContext };
1148
+ export { Account, AccountNotConfiguredError, Address, Branch, BranchMap, Cart, Choice, DeliveryMap, FOUNDATIONS_VERSION, Fulfilment, Image, ItemForm, ItemNote, Money, OpenStatus, OptionGroup, Otp, Overlay, PartProps, PaymentMethods, Quantity, Schedule, ScheduleBlock, ScheduleSlot, Search, Totals, ZappieAccountLink, canOrderAhead, contactLinks, createSearchIndex, formatOpeningHours, formatWallClock, getOpenStatus, parseWallClock, preloadOverlay, useControllableState, useDragToClose, useOverlay, useZappieAccountUrl, zappieAccountUrl, zappieEnvironment, zappieSupportUrl };
1149
+ export type { ActiveGroup, AddressState, AddressSuggestion, BranchMapLoadErrorState, BranchMapProps, ContactLink, Customer, DeliveryMapProps, DragDirection, DragToCloseOptions, ImageProps, ItemFormContext, ItemFormInvalidGroup, ItemFormProps, MapLoadErrorState, NoteState, PaymentMethod, QuantityProps, QuantityState, ScheduleState, ScheduleStatus, SearchResult, SearchSource, SearchState, SignInOptions, TotalsState, ZappieSupportContext };
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ import { Cart, Totals } from "./cart/index.js";
14
14
  import { deferred, useIntentPreload } from "./deferred/index.js";
15
15
  import { Address } from "./fulfilment/address-picker.js";
16
16
  import { Branch } from "./fulfilment/branch-picker.js";
17
- import { Schedule, scheduleSlots } from "./fulfilment/schedule.js";
17
+ import { Schedule } from "./fulfilment/schedule.js";
18
18
  import { Fulfilment } from "./fulfilment/switch.js";
19
19
  import { useControllableState } from "./internal/use-controllable-state.js";
20
20
  export * from "./item-form/index.js";
@@ -65,7 +65,6 @@ export {
65
65
  getOpenStatus,
66
66
  parseWallClock,
67
67
  preloadOverlay,
68
- scheduleSlots,
69
68
  useControllableState,
70
69
  useDragToClose,
71
70
  useIntentPreload,
package/dist/index.js.map CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "mappings": ";;;AACA,SAAS,uBAAuB;AAIhC,SAAS,SAAS,iCAAiC;AAEnD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,WAAW,mBAAmB;AAOvC,SAAS,MAAM,cAAc;AAE7B,SAAS,UAAU,wBAAwB;AAO3C,SAAS,eAAe;AAKxB,SAAS,cAAc;AACvB,SAAS,UAAU,qBAAqB;AAMxC,SAAS,kBAAkB;AAE3B,SAAS,4BAA4B;AACrC,cAAc;AACd,SAAS,aAAa;AAEtB,SAAS,aAAa;AACtB,SAAS,WAAW;AACpB,SAAS,sBAAsB;AAK/B,SAAS,sBAAsB;AAC/B,SAAS,SAAS,kBAAkB;AACpC,SAAS,sBAAsB;AAE/B,SAAS,mBAAmB,cAAc;AAE1C,SAAS,cAAc,wBAAwB;AAE/C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,kBAAkB;AAjEpB,IAAM,sBAA8B;",
4
+ "mappings": ";;;AACA,SAAS,uBAAuB;AAIhC,SAAS,SAAS,iCAAiC;AAEnD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,WAAW,mBAAmB;AAOvC,SAAS,MAAM,cAAc;AAE7B,SAAS,UAAU,wBAAwB;AAO3C,SAAS,eAAe;AAKxB,SAAS,cAAc;AAEvB,SAAS,gBAAgB;AAEzB,SAAS,kBAAkB;AAS3B,SAAS,4BAA4B;AACrC,cAAc;AACd,SAAS,aAAa;AAEtB,SAAS,aAAa;AACtB,SAAS,WAAW;AACpB,SAAS,sBAAsB;AAK/B,SAAS,sBAAsB;AAC/B,SAAS,SAAS,kBAAkB;AACpC,SAAS,sBAAsB;AAE/B,SAAS,mBAAmB,cAAc;AAE1C,SAAS,cAAc,wBAAwB;AAE/C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,kBAAkB;AArEpB,IAAM,sBAA8B;",
5
5
  "names": []
6
6
  }
@@ -0,0 +1,51 @@
1
+ import { Branch, MenuSchedule } from '@zappie/sdk';
2
+ import { c as ScheduleMenu, d as ScheduleOptions, S as ScheduleBlock, b as ScheduleDay, a as ScheduleSlot } from '../window-BAFAfQnw.js';
3
+
4
+ /**
5
+ * Generate scheduling slots within branch hours and the hours and pre-order terms of each menu.
6
+ * A bag holding items from several menus gets only the times every menu allows: the longest
7
+ * notice, the shortest days-ahead limit and the hours all the menus share.
8
+ *
9
+ * Slots reach as far as the branch's `orderAheadDays`, or a pre-order menu's shorter limit, and
10
+ * never past `maxDays` when you pass it. A pre-order menu at a branch that takes orders only while
11
+ * open uses its own limit. A closed branch still gets its later slots. Returns no slots when the
12
+ * branch doesn't take orders ahead (`canOrderAhead`), unless a menu is a pre-order menu.
13
+ *
14
+ * @param branch - The branch whose hours and scheduling capability apply.
15
+ * @param timezone - The time zone used to interpret opening hours and label slots.
16
+ * @param now - The instant from which notice and scheduling limits are measured.
17
+ * @param options - Site limits, the bag's `menus`, or one `menuSchedule` without a menu ID.
18
+ * @returns Slots with ISO values, local dates and display labels.
19
+ */
20
+ declare function scheduleSlots(branch: Branch, timezone: string, now: Date, { menuSchedule, menus, ...options }?: ScheduleOptions & {
21
+ menuSchedule?: MenuSchedule | undefined;
22
+ menus?: readonly ScheduleMenu[] | undefined;
23
+ }): ScheduleSlot[];
24
+ /**
25
+ * Say why a bag holding items from `menus` has no slot, naming the menu that blocks it when one
26
+ * does. Returns undefined when the bag has a slot.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * scheduleBlocker(branch, "Africa/Lagos", new Date(), [brunch, surfAndTurf]);
31
+ * // { reason: "menu-closed", menuId: surfAndTurf.id }
32
+ * ```
33
+ */
34
+ declare function scheduleBlocker(branch: Branch, timezone: string, now: Date, menus: readonly ScheduleMenu[], options?: ScheduleOptions): ScheduleBlock | undefined;
35
+ /**
36
+ * Answer, for each local day from today, whether a bag holding items from `menus` can be
37
+ * fulfilled that day, with the slots it can or the reason it can't. Pass one menu to ask whether
38
+ * that menu can make each day.
39
+ *
40
+ * @param days - How many days to answer, from today in the branch's time zone. Defaults to 14.
41
+ * @example
42
+ * ```ts
43
+ * scheduleDays(branch, "Africa/Lagos", new Date(), [catering]);
44
+ * // [{ date: "2026-11-12", slots: [], blocker: { reason: "too-soon", noticeHours: 18, menuId: catering.id } }, ...]
45
+ * ```
46
+ */
47
+ declare function scheduleDays(branch: Branch, timezone: string, now: Date, menus: readonly ScheduleMenu[], { days, ...options }?: ScheduleOptions & {
48
+ days?: number;
49
+ }): ScheduleDay[];
50
+
51
+ export { ScheduleBlock, ScheduleDay, ScheduleMenu, ScheduleOptions, ScheduleSlot, scheduleBlocker, scheduleDays, scheduleSlots };
@@ -0,0 +1,37 @@
1
+ // src/schedule/index.ts
2
+ import {
3
+ explain
4
+ } from "../fulfilment/window.js";
5
+ import { dateOffset, localParts } from "../status/hours.js";
6
+ function scheduleSlots(branch, timezone, now, {
7
+ menuSchedule,
8
+ menus = [],
9
+ ...options
10
+ } = {}) {
11
+ return explain(
12
+ branch,
13
+ timezone,
14
+ now,
15
+ menuSchedule ? [...menus, { id: "", schedule: menuSchedule }] : [...menus],
16
+ options
17
+ ).slots;
18
+ }
19
+ function scheduleBlocker(branch, timezone, now, menus, options) {
20
+ return explain(branch, timezone, now, [...menus], options).why();
21
+ }
22
+ function scheduleDays(branch, timezone, now, menus, { days = 14, ...options } = {}) {
23
+ const result = explain(branch, timezone, now, [...menus], options, days);
24
+ const today = localParts(now, timezone).date;
25
+ return Array.from({ length: days }, (_, offset) => {
26
+ const date = dateOffset(today, offset);
27
+ const slots = result.slots.filter((slot) => slot.date === date);
28
+ const blocker = slots.length ? void 0 : result.why(date);
29
+ return blocker ? { date, slots, blocker } : { date, slots };
30
+ });
31
+ }
32
+ export {
33
+ scheduleBlocker,
34
+ scheduleDays,
35
+ scheduleSlots
36
+ };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/schedule/index.ts"],
4
+ "mappings": ";AACA;AAAA,EACE;AAAA,OAMK;AACP,SAAS,YAAY,kBAAkB;AAwBhC,SAAS,cACd,QACA,UACA,KACA;AAAA,EACE;AAAA,EACA,QAAQ,CAAC;AAAA,EACT,GAAG;AACL,IAGI,CAAC,GACW;AAChB,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,eAAe,CAAC,GAAG,OAAO,EAAE,IAAI,IAAI,UAAU,aAAa,CAAC,IAAI,CAAC,GAAG,KAAK;AAAA,IACzE;AAAA,EACF,EAAE;AACJ;AAWO,SAAS,gBACd,QACA,UACA,KACA,OACA,SAC2B;AAC3B,SAAO,QAAQ,QAAQ,UAAU,KAAK,CAAC,GAAG,KAAK,GAAG,OAAO,EAAE,IAAI;AACjE;AAaO,SAAS,aACd,QACA,UACA,KACA,OACA,EAAE,OAAO,IAAI,GAAG,QAAQ,IAAyC,CAAC,GACnD;AACf,QAAM,SAAS,QAAQ,QAAQ,UAAU,KAAK,CAAC,GAAG,KAAK,GAAG,SAAS,IAAI;AACvE,QAAM,QAAQ,WAAW,KAAK,QAAQ,EAAE;AACxC,SAAO,MAAM,KAAK,EAAE,QAAQ,KAAK,GAAG,CAAC,GAAG,WAAW;AACjD,UAAM,OAAO,WAAW,OAAO,MAAM;AACrC,UAAM,QAAQ,OAAO,MAAM,OAAO,CAAC,SAAS,KAAK,SAAS,IAAI;AAC9D,UAAM,UAAU,MAAM,SAAS,SAAY,OAAO,IAAI,IAAI;AAC1D,WAAO,UAAU,EAAE,MAAM,OAAO,QAAQ,IAAI,EAAE,MAAM,MAAM;AAAA,EAC5D,CAAC;AACH;",
5
+ "names": []
6
+ }
package/dist/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var PACKAGE_VERSION = true ? "0.10.0" : "0.0.0-development";
2
+ var PACKAGE_VERSION = true ? "0.11.0" : "0.0.0-development";
3
3
  export {
4
4
  PACKAGE_VERSION
5
5
  };
@@ -0,0 +1,53 @@
1
+ import { MenuSection } from '@zappie/sdk';
2
+
3
+ /**
4
+ * A selectable time with an ISO instant, local date and display label including the UTC offset.
5
+ */
6
+ type ScheduleSlot = {
7
+ value: string;
8
+ label: string;
9
+ date: string;
10
+ };
11
+ /** A source menu whose hours and pre-order terms limit when a bag can be fulfilled. */
12
+ type ScheduleMenu = Pick<MenuSection, "id" | "schedule">;
13
+ /** Limits that a site adds to the branch and menu rules. */
14
+ type ScheduleOptions = {
15
+ /** Minutes between slots. Defaults to 30. */
16
+ intervalMinutes?: number | undefined;
17
+ /** The furthest day ahead to offer, 1 to 90. Defaults to 90. */
18
+ maxDays?: number | undefined;
19
+ /** Minutes of lead time every order needs. Defaults to 0. */
20
+ noticeMinutes?: number | undefined;
21
+ };
22
+ /**
23
+ * Why a day, or a whole bag, has no slot.
24
+ *
25
+ * - `no-scheduling`: the branch takes no scheduled orders for these menus.
26
+ * - `closed`: the branch is closed.
27
+ * - `menu-closed`: the menu named by `menuId` isn't served then, or shares no time with the menus before it.
28
+ * - `too-far`: the day is past the `maxDays` limit. `menuId` names the menu that sets it, when one does.
29
+ * - `too-soon`: the remaining times are within the `noticeHours` the order needs. `menuId` names the
30
+ * menu that asks for that notice, when one does. Zero `noticeHours` means the day's times have passed.
31
+ */
32
+ type ScheduleBlock = {
33
+ reason: "no-scheduling" | "closed";
34
+ } | {
35
+ reason: "menu-closed";
36
+ menuId: string;
37
+ } | {
38
+ reason: "too-far";
39
+ maxDays: number;
40
+ menuId?: string | undefined;
41
+ } | {
42
+ reason: "too-soon";
43
+ noticeHours: number;
44
+ menuId?: string | undefined;
45
+ };
46
+ /** One local day with its slots, and why it has none when `slots` is empty. */
47
+ type ScheduleDay = {
48
+ date: string;
49
+ slots: ScheduleSlot[];
50
+ blocker?: ScheduleBlock;
51
+ };
52
+
53
+ export type { ScheduleBlock as S, ScheduleSlot as a, ScheduleDay as b, ScheduleMenu as c, ScheduleOptions as d };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zappie/foundations",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "UI-less React primitives for Zappie storefronts: behaviour, state and accessibility without styles.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -17,6 +17,10 @@
17
17
  "types": "./dist/branch-switch/index.d.ts",
18
18
  "import": "./dist/branch-switch/index.js"
19
19
  },
20
+ "./schedule": {
21
+ "types": "./dist/schedule/index.d.ts",
22
+ "import": "./dist/schedule/index.js"
23
+ },
20
24
  "./rich-text": {
21
25
  "types": "./dist/rich-text/index.d.ts",
22
26
  "import": "./dist/rich-text/index.js"
@@ -36,12 +40,12 @@
36
40
  ],
37
41
  "dependencies": {
38
42
  "@base-ui/react": "^1.9.0",
39
- "@googlemaps/markerclusterer": "^2.6.2",
40
- "@zappie/sdk": "^0.10.3"
43
+ "@googlemaps/markerclusterer": "^2.6.2"
41
44
  },
42
45
  "peerDependencies": {
43
46
  "react": "^18.2.0 || ^19.0.0",
44
- "react-dom": "^18.2.0 || ^19.0.0"
47
+ "react-dom": "^18.2.0 || ^19.0.0",
48
+ "@zappie/sdk": "^0.10.3"
45
49
  },
46
50
  "repository": {
47
51
  "type": "git",
@@ -50,7 +54,8 @@
50
54
  },
51
55
  "devDependencies": {
52
56
  "@types/google.maps": "^3.66.7",
53
- "esbuild": "^0.28.2"
57
+ "esbuild": "^0.28.2",
58
+ "@zappie/sdk": "0.10.5"
54
59
  },
55
60
  "license": "MIT",
56
61
  "homepage": "https://github.com/zappie/storefront-packages#readme",