@wix/himalaya-cli 0.2014.0 → 0.2016.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/cli.mjs
CHANGED
|
@@ -215042,22 +215042,24 @@ var init_generate = __esm({
|
|
|
215042
215042
|
const left = arr4(placement.gaps).map(str16).filter(Boolean);
|
|
215043
215043
|
if (left.length) ctx.gap(elementId, "pages_section", `the Pages document has content the section cannot show (${left.join(", ")}); its text and images are shown without it`);
|
|
215044
215044
|
const title = str16(s5.title).trim();
|
|
215045
|
-
const heading = s5.showTitle === true && title ? [text10(`${id}.title`, title, "title3", {},
|
|
215045
|
+
const heading = s5.showTitle === true && title ? [text10(`${id}.title`, title, "title3", {}, inset(0, 0, 0))] : [];
|
|
215046
215046
|
return vstack(`${id}.box`, [...heading, placement.component], heading.length ? 8 : 0, inset(8, 8));
|
|
215047
215047
|
}
|
|
215048
215048
|
return ctx.unsupported(elementId, "pages_section", placement ? `its Pages document is not ready in pages-content.json (state "${str16(placement.state)}")` : "its Pages document is not in pages-content.json: re-run `himi one-app migrate` to collect it");
|
|
215049
215049
|
},
|
|
215050
|
-
// The site's services as
|
|
215051
|
-
//
|
|
215050
|
+
// The site's services as the source draws them: a search field, then rows that open the service, each with where it is offered, its
|
|
215051
|
+
// price and a Book button, and a See All button to the services screen. The source section has no settings of its own (title only),
|
|
215052
|
+
// so the section shows a short list; its search finds any service by name, and the screen keeps category chips and the location pin.
|
|
215052
215053
|
bookings_services_list_section: (id, s5, _st, index, ctx, sectionId) => {
|
|
215053
215054
|
if (!ctx.hasScreen("himi_service")) return ctx.unsupported(sectionId, "bookings_services_list_section", "this scaffold has no himi_service screen for a row to open");
|
|
215054
215055
|
const hasScreen2 = ctx.hasScreen("himi_services");
|
|
215055
|
-
ctx.
|
|
215056
|
+
const timeScreen = ctx.hasScreen("himi_time") ? "himi_time" : "himi_service";
|
|
215057
|
+
ctx.gap(sectionId, "bookings_services_list_section", `the source lists every service; the section shows the first ${SERVICES_INLINE} (its search finds any service by name)${hasScreen2 ? ", and a See All button opens the services screen (category chips, location filter)" : ""}`);
|
|
215056
215058
|
const svc = ctx.uses("services", `services${index}`, { limit: SERVICES_INLINE });
|
|
215057
215059
|
const children2 = [];
|
|
215058
215060
|
const title = str16(s5.title).trim();
|
|
215059
215061
|
if (s5.showTitle === true && title) children2.push(text10(`${id}.title`, title, "title3", {}, inset(0, 0, 0)));
|
|
215060
|
-
const list5 = ctx.recipe("services-list", { svc, id, detailScreen: "himi_service", seeAllLabel: "See All", seeAllScreen: "himi_services" });
|
|
215062
|
+
const list5 = ctx.recipe("services-list", { svc, id, detailScreen: "himi_service", timeScreen, searchPlaceholder: "Search services", seeAllLabel: "See All", seeAllScreen: "himi_services" });
|
|
215061
215063
|
children2.push(hasScreen2 ? list5 : withoutNode(list5, `${id}.more`), ...ctx.optionalRecipe("section-error", { svc, id, action: "load" }));
|
|
215062
215064
|
return vstack(`${id}.box`, children2, 12, { ...inset(16, 16), onAppear: { actionId: `tier5.${svc}.load`, params: {} } });
|
|
215063
215065
|
},
|
|
@@ -175,7 +175,7 @@ The CLI's versioned native-widget inventory supplies these rows automatically.
|
|
|
175
175
|
|
|
176
176
|
| Source section type | Effective behavior to verify |
|
|
177
177
|
| --- | --- |
|
|
178
|
-
| `bookings_services_list_section` | Search, category and location filters; each card's dates (a course), price and an Ended badge for a past course. `himi_services` has all of them, so never hand-write a list: a page made only of this section opens that screen, and beside other sections the generator shows a short inline list (the first 6 services, each opening its service
|
|
178
|
+
| `bookings_services_list_section` | Search, category and location filters; each card's dates (a course), price and an Ended badge for a past course. `himi_services` has all of them, so never hand-write a list: a page made only of this section opens that screen, and beside other sections the generator shows a short inline list (the first 6 services, each opening its service, with its place, price and, where it is booked by time, a Book button that opens Choose Time) under a search field that finds any service by name, and a See All to that screen when the scaffold ships the recipe, else the title and a See All link to it. |
|
|
179
179
|
| `single_group_feed_section`, `groups_feed_section` | Post actions, attachments, comments/post destination, topic filter and the group's Members / Library / Rules / Media destinations. |
|
|
180
180
|
| `platform_general_contact_section`, `platform_general_contact_section_bp` | Business address and map, respecting authored visibility and available coordinates. |
|
|
181
181
|
| `achievements_list_section` | Category filter, including All Categories with no categories; actual visitor/member login, access/join/payment/Continue states; participants line; default limit 2 and See All when applicable. The section's chosen category (`categorySettings`) scopes the list and a MANUAL order leads it, as in the source; a program's cover falls back to its video poster. |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/himalaya-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2016.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "himi — the Himalaya authoring CLI. Build a Tier-5 content package on your own machine, push it to the production serve as a draft, preview it on web + Himi Player, and read worker logs back — the remote dev loop. Wraps @himalaya/serve-cli for transport.",
|
|
6
6
|
"keywords": [
|
|
@@ -82,5 +82,5 @@
|
|
|
82
82
|
"artifactId": "himalaya-cli"
|
|
83
83
|
}
|
|
84
84
|
},
|
|
85
|
-
"falconPackageHash": "
|
|
85
|
+
"falconPackageHash": "03a4a7ce1f5a84d91b153f3a24d6c74c0cebf58484ed00e82d6601c7"
|
|
86
86
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
-
"_recipe": "A page's services: rows (photo, name, tagline, duration and price, a
|
|
2
|
+
"_recipe": "A page's services: a search field that finds a service by name among every service of the site, rows (photo, name, tagline, duration and price, where it is offered, Ended for a course that is over; a tap opens the service) each with its Book button where online booking by time is possible (Book opens Choose Time on the service; a course has none, its row opens it), the section's See All button when the site has more services than it shows, and the empty text. ONE node, always mounted. Service: registerServicesSection(svc, { limit }).",
|
|
3
3
|
"_placeholders": {
|
|
4
4
|
"svc": "service name the page worker registered with registerServicesSection",
|
|
5
5
|
"id": "unique node id prefix for this section",
|
|
6
6
|
"detailScreen": "service screen id, himi_service",
|
|
7
|
+
"timeScreen": "the screen Book opens on the service: himi_time, or himi_service in a scaffold without it",
|
|
8
|
+
"searchPlaceholder": "the search field's placeholder",
|
|
7
9
|
"seeAllLabel": "the See All button's label",
|
|
8
10
|
"seeAllScreen": "the screen See All opens, himi_services"
|
|
9
11
|
},
|
|
@@ -16,6 +18,24 @@
|
|
|
16
18
|
"spacing": 12,
|
|
17
19
|
"alignment": "leading",
|
|
18
20
|
"children": [
|
|
21
|
+
{
|
|
22
|
+
"id": "{{id}}.search",
|
|
23
|
+
"body": {
|
|
24
|
+
"package": "himalaya",
|
|
25
|
+
"type": "SearchField",
|
|
26
|
+
"props": {
|
|
27
|
+
"placeholder": "{{searchPlaceholder}}",
|
|
28
|
+
"debounceMs": 200,
|
|
29
|
+
"value": "$service:{{svc}}.query",
|
|
30
|
+
"onChange": {
|
|
31
|
+
"actionId": "tier5.{{svc}}.search"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"modifiers": {
|
|
36
|
+
"hiddenPath": "$service:{{svc}}.noSearch"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
19
39
|
{
|
|
20
40
|
"id": "{{id}}.loading",
|
|
21
41
|
"body": {
|
|
@@ -44,7 +64,7 @@
|
|
|
44
64
|
"type": "LazyList",
|
|
45
65
|
"props": {
|
|
46
66
|
"spacing": 12,
|
|
47
|
-
"itemsPath": "$service:{{svc}}.
|
|
67
|
+
"itemsPath": "$service:{{svc}}.serviceRows",
|
|
48
68
|
"items": [
|
|
49
69
|
{
|
|
50
70
|
"id": "{{id}}.row",
|
|
@@ -126,6 +146,23 @@
|
|
|
126
146
|
"hiddenPath": "$item.noMeta"
|
|
127
147
|
}
|
|
128
148
|
},
|
|
149
|
+
{
|
|
150
|
+
"id": "{{id}}.location",
|
|
151
|
+
"body": {
|
|
152
|
+
"package": "himalaya",
|
|
153
|
+
"type": "Text",
|
|
154
|
+
"props": {
|
|
155
|
+
"text": "$item.location",
|
|
156
|
+
"style": "caption",
|
|
157
|
+
"maxLines": 1,
|
|
158
|
+
"color": "textSecondary",
|
|
159
|
+
"collapseWhenEmpty": true
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
"modifiers": {
|
|
163
|
+
"hiddenPath": "$item.noLocation"
|
|
164
|
+
}
|
|
165
|
+
},
|
|
129
166
|
{
|
|
130
167
|
"id": "{{id}}.tag",
|
|
131
168
|
"body": {
|
|
@@ -149,6 +186,58 @@
|
|
|
149
186
|
"modifiers": {
|
|
150
187
|
"weight": 1
|
|
151
188
|
}
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
"id": "{{id}}.book",
|
|
192
|
+
"body": {
|
|
193
|
+
"package": "himalaya",
|
|
194
|
+
"type": "HStack",
|
|
195
|
+
"props": {
|
|
196
|
+
"spacing": 0,
|
|
197
|
+
"alignment": "center",
|
|
198
|
+
"children": [
|
|
199
|
+
{
|
|
200
|
+
"id": "{{id}}.book.label",
|
|
201
|
+
"body": {
|
|
202
|
+
"package": "himalaya",
|
|
203
|
+
"type": "Text",
|
|
204
|
+
"props": {
|
|
205
|
+
"text": "$item.bookCta",
|
|
206
|
+
"style": "footnote",
|
|
207
|
+
"color": "textPrimary",
|
|
208
|
+
"maxLines": 1
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
"modifiers": {
|
|
216
|
+
"hiddenPath": "$item.noBook",
|
|
217
|
+
"height": 36,
|
|
218
|
+
"padding": {
|
|
219
|
+
"leading": 16,
|
|
220
|
+
"trailing": 16
|
|
221
|
+
},
|
|
222
|
+
"cornerRadius": 18,
|
|
223
|
+
"border": {
|
|
224
|
+
"width": 1,
|
|
225
|
+
"color": {
|
|
226
|
+
"token": "textPrimary"
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
"pressFeedback": "scale",
|
|
230
|
+
"haptic": "light",
|
|
231
|
+
"onTap": {
|
|
232
|
+
"actionId": "himi.open",
|
|
233
|
+
"params": {
|
|
234
|
+
"screenId": "{{timeScreen}}",
|
|
235
|
+
"id": "$item.id"
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
"accessibilityRole": "button",
|
|
239
|
+
"accessibilityLabel": "$item.bookA11y"
|
|
240
|
+
}
|
|
152
241
|
}
|
|
153
242
|
]
|
|
154
243
|
}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
// A page's services section as a registrable service: the site's Wix Bookings services in the business's own order (the order of its
|
|
2
|
-
// Book Online page), as rows that open the service
|
|
3
|
-
// this is its short form for a page
|
|
2
|
+
// Book Online page), as rows that open the service, each with where it is offered and a Book button (the recipe names the screen it
|
|
3
|
+
// opens). The services screen (himi_services) keeps category chips and the location pin; this is its short form for a page:
|
|
4
4
|
// limit -> at most N services, and `noMore` is false when the site has more (the section's See All button shows then).
|
|
5
|
+
// The section's search field finds a service by name among every service of the site (up to 500), not only the first `limit`.
|
|
5
6
|
// A page worker registers it under a service name; the descriptor binds `$service:<serviceName>.*` (recipe services-list.json).
|
|
6
|
-
import { defineService } from "@himalaya/state";
|
|
7
|
+
import { defineService, type ServiceHandle } from "@himalaya/state";
|
|
7
8
|
import { queryServices, type WixService } from "@himalaya/wix-bookings";
|
|
8
9
|
import { wixMediaUrl } from "@himalaya/wix-media";
|
|
9
10
|
import { businessTimeZone } from "./app-locale.js";
|
|
11
|
+
import { serviceLocationChoices } from "./booking-location.js";
|
|
10
12
|
import { courseDatesText, courseHasEnded, courseNeedsZone, serviceDurationText, servicePriceLine, type BookingLabel } from "./himi-bookings.js";
|
|
11
13
|
import { copyParam, copyTemplate, stashCopy } from "./himi-copy.js";
|
|
12
14
|
import { initials } from "./himi-text.js";
|
|
15
|
+
import { offsetPage } from "./offset-page.js";
|
|
13
16
|
|
|
14
17
|
export interface ServicesSectionOptions {
|
|
15
18
|
/** Show at most this many services. */
|
|
@@ -21,24 +24,41 @@ export interface ServiceRow {
|
|
|
21
24
|
tagline: string; noTagline: boolean;
|
|
22
25
|
/** Duration and price, or a course's dates and price: "1 hr · $70". */
|
|
23
26
|
meta: string; noMeta: boolean;
|
|
24
|
-
/**
|
|
27
|
+
/** Where the service is offered: its locations' names (else addresses), or "Client's place"; "" when the record names none. */
|
|
28
|
+
location: string; noLocation: boolean;
|
|
29
|
+
/** "Ended" for a course that is over; "" otherwise. */
|
|
25
30
|
tag: string; noTag: boolean;
|
|
26
31
|
image: string; initial: string; hasImage: boolean; noImage: boolean;
|
|
27
32
|
/** The service screen a row opens; the row binds `id` as its nav param. */
|
|
28
33
|
detailScreenId: string;
|
|
34
|
+
/** Online booking is on and the service is booked by time (not a course): the Book button shows. */
|
|
35
|
+
canBook: boolean; noBook: boolean;
|
|
36
|
+
/** "Book", or "Request" when the business approves each booking by hand (the services screen's words). */
|
|
37
|
+
bookCta: string; bookA11y: string;
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
export interface ServicesSectionState {
|
|
32
41
|
status: "loading" | "ready" | "error";
|
|
33
|
-
rows
|
|
42
|
+
/** The rows shown (not `rows`: a snapshot an earlier worker wrote under that key lacks this worker's row fields). */
|
|
43
|
+
serviceRows: ServiceRow[];
|
|
34
44
|
emptyText: string;
|
|
35
45
|
notLoading: boolean; notErrored: boolean; noRows: boolean; noEmpty: boolean;
|
|
36
46
|
/** false when the site has more services than the section shows. */
|
|
37
47
|
noMore: boolean;
|
|
48
|
+
/** What is typed in the search field, echoed back as typed. */
|
|
49
|
+
query: string;
|
|
50
|
+
/** The search field hides while the section loads or fails, and when the site has no service to find. */
|
|
51
|
+
noSearch: boolean;
|
|
38
52
|
}
|
|
39
53
|
|
|
40
54
|
/** The row's 72pt thumbnail at 3x: the shared list size (1000px) would fetch and decode far more pixels than it shows. */
|
|
41
55
|
const THUMB = { width: 216, height: 216, quality: 85 };
|
|
56
|
+
/** One Bookings query page, and the most pages a search reads (500 services). */
|
|
57
|
+
const PAGE = 100;
|
|
58
|
+
const MAX_PAGES = 5;
|
|
59
|
+
/** The business's own order, as on the services screen. */
|
|
60
|
+
const SORT = [{ fieldName: "category.sortOrder", order: "ASC" as const }, { fieldName: "sortOrder", order: "ASC" as const }, { fieldName: "id", order: "ASC" as const }];
|
|
61
|
+
const FILTER = { hidden: { $ne: true } };
|
|
42
62
|
|
|
43
63
|
/** The services screen's own lines for a duration, a price and a course's dates, so a row reads the same in both places. */
|
|
44
64
|
const label: BookingLabel = (key, fallback, values) =>
|
|
@@ -66,10 +86,43 @@ function mediaUrl(value: unknown): string {
|
|
|
66
86
|
|
|
67
87
|
const serviceImage = (service: WixService): string => mediaUrl(service.media);
|
|
68
88
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
89
|
+
type AddressLike = { formattedAddress?: unknown; addressLine?: unknown; city?: unknown; subdivision?: unknown; postalCode?: unknown; country?: unknown;
|
|
90
|
+
streetAddress?: { number?: unknown; name?: unknown; apt?: unknown } };
|
|
91
|
+
const strOf = (value: unknown): string => (typeof value === "string" ? value.trim() : "");
|
|
92
|
+
|
|
93
|
+
/** A Wix address as one line, the service screen's rule: its formatted text, else its street and city parts. */
|
|
94
|
+
function addressLine(value: unknown): string {
|
|
95
|
+
if (!value || typeof value !== "object") return "";
|
|
96
|
+
const address = value as AddressLike;
|
|
97
|
+
const formatted = strOf(address.formattedAddress);
|
|
98
|
+
if (formatted) return formatted;
|
|
99
|
+
const street = [strOf(address.streetAddress?.number), strOf(address.streetAddress?.name)].filter(Boolean).join(" ");
|
|
100
|
+
const line = [street, strOf(address.streetAddress?.apt)].filter(Boolean).join(" ") || strOf(address.addressLine);
|
|
101
|
+
const city = strOf(address.city);
|
|
102
|
+
if (!line && !city) return "";
|
|
103
|
+
return [line, city, strOf(address.subdivision), strOf(address.postalCode), strOf(address.country)].filter(Boolean).join(", ");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The service's places in its own order: a business location by name (else address), a custom one by address, the client's place. */
|
|
107
|
+
function locationText(service: WixService): string {
|
|
108
|
+
const places: string[] = [];
|
|
109
|
+
for (const location of service.locations ?? []) {
|
|
110
|
+
const part = (location?.type === "BUSINESS" || location?.business ? location?.business : location?.custom) as { address?: unknown } | undefined;
|
|
111
|
+
const place = location?.type === "CUSTOMER" ? copyParam("service.clientPlace", "Client's place")
|
|
112
|
+
: serviceLocationChoices([location])[0]?.label || addressLine(part?.address) || addressLine((location as { calculatedAddress?: unknown } | undefined)?.calculatedAddress);
|
|
113
|
+
if (place && !places.includes(place)) places.push(place);
|
|
114
|
+
}
|
|
115
|
+
return places.join(" · ");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function bookCtaFor(request: boolean): string {
|
|
119
|
+
return request ? copyParam("services.requestCta", "Request") : copyParam("services.bookCta", "Book");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function bookLabel(name: string, request: boolean): string {
|
|
123
|
+
return request
|
|
124
|
+
? copyTemplate("services.requestLabel", "Request to book {name}", { name })
|
|
125
|
+
: copyTemplate("services.bookLabel", "Book {name}", { name });
|
|
73
126
|
}
|
|
74
127
|
|
|
75
128
|
function serviceRow(service: WixService): ServiceRow {
|
|
@@ -82,41 +135,159 @@ function serviceRow(service: WixService): ServiceRow {
|
|
|
82
135
|
lastSessionEnd: service.schedule?.lastSessionEnd,
|
|
83
136
|
...(businessTimeZone() && courseNeedsZone(service.schedule) ? { timeZone: businessTimeZone() } : {}),
|
|
84
137
|
} : undefined;
|
|
85
|
-
const ended = schedule && courseHasEnded({ schedule }, Date.now())
|
|
138
|
+
const ended = !!schedule && courseHasEnded({ schedule }, Date.now());
|
|
86
139
|
const meta = [isCourse ? courseDatesText(service) : serviceDurationText(service, label), servicePriceLine(service, label).text].filter(Boolean).join(" · ");
|
|
87
|
-
const tag =
|
|
140
|
+
const tag = ended ? copyParam("services.ended", "Ended") : "";
|
|
88
141
|
const tagline = typeof service.tagLine === "string" ? service.tagLine.trim() : "";
|
|
142
|
+
const location = locationText(service);
|
|
89
143
|
const image = serviceImage(service);
|
|
144
|
+
// The services screen's rule: Book (a time to pick) shows while online booking is on, never for a course: a course is booked whole,
|
|
145
|
+
// or not at all once it started, on its service screen, which the row opens.
|
|
146
|
+
const canBook = service.onlineBooking?.enabled !== false && !isCourse;
|
|
147
|
+
const request = service.onlineBooking?.requireManualApproval === true;
|
|
90
148
|
return {
|
|
91
|
-
id: service.id, title, tagline, noTagline: !tagline, meta, noMeta: !meta, tag, noTag: !tag,
|
|
149
|
+
id: service.id, title, tagline, noTagline: !tagline, meta, noMeta: !meta, location, noLocation: !location, tag, noTag: !tag,
|
|
92
150
|
image, initial: initials(title), hasImage: !!image, noImage: !image, detailScreenId: "himi_service",
|
|
151
|
+
canBook, noBook: !canBook, bookCta: bookCtaFor(request), bookA11y: bookLabel(title, request),
|
|
93
152
|
};
|
|
94
153
|
}
|
|
95
154
|
|
|
155
|
+
/** What the search field sent: a bare string, or `{ value }` / `{ text }`. */
|
|
156
|
+
const queryOf = (arg: unknown): string => {
|
|
157
|
+
if (typeof arg === "string") return arg;
|
|
158
|
+
const value = (arg as { value?: unknown; text?: unknown } | undefined)?.value ?? (arg as { text?: unknown } | undefined)?.text;
|
|
159
|
+
return typeof value === "string" ? value : "";
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/** A name match, ignoring case, anywhere in the name: "stretch" finds "Studio Stretch and Reset". */
|
|
163
|
+
const matches = (service: WixService, query: string): boolean => (service.name ?? "").toLowerCase().includes(query.trim().toLowerCase());
|
|
164
|
+
|
|
165
|
+
type Actions = {
|
|
166
|
+
load: (state: ServicesSectionState, arg?: unknown) => Promise<ServicesSectionState | undefined>;
|
|
167
|
+
search: (state: ServicesSectionState, arg?: unknown) => Promise<ServicesSectionState | undefined>;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/** What a search looks through. `partial`: not every service was read (the site has more than the cap, or a page failed);
|
|
171
|
+
* `failed`: a page failed. */
|
|
172
|
+
interface Pool { services: WixService[]; partial: boolean; failed: boolean }
|
|
173
|
+
|
|
174
|
+
/** The most matches a search shows in the section; past it, See All opens the services screen. */
|
|
175
|
+
const SEARCH_ROWS = 20;
|
|
176
|
+
|
|
177
|
+
interface View { more?: boolean; query?: string; catalogEmpty?: boolean; failed?: boolean }
|
|
178
|
+
|
|
96
179
|
export function registerServicesSection(serviceName: string, options: ServicesSectionOptions) {
|
|
97
|
-
const view = (status: ServicesSectionState["status"], rows: ServiceRow[], more:
|
|
98
|
-
status
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
180
|
+
const view = (status: ServicesSectionState["status"], rows: ServiceRow[], { more = false, query = "", catalogEmpty = false, failed = false }: View = {}): ServicesSectionState => {
|
|
181
|
+
const empty = status === "ready" && !rows.length;
|
|
182
|
+
// A search that did not read every service never says "no match", since the service may be past what was read: a failed read says
|
|
183
|
+
// so, and one past the cap leaves See All to the services screen, which pages the rest.
|
|
184
|
+
const emptyText = !empty ? ""
|
|
185
|
+
: !query.trim() ? copyParam("services.emptyText", "There are no services to book yet.")
|
|
186
|
+
: failed ? copyParam("services.errorText", "Services could not be loaded.")
|
|
187
|
+
: more ? ""
|
|
188
|
+
: copyParam("services.noMatchText", "No services match your search.");
|
|
189
|
+
return {
|
|
190
|
+
status, serviceRows: rows, emptyText,
|
|
191
|
+
notLoading: status !== "loading", notErrored: status !== "error",
|
|
192
|
+
noRows: !rows.length, noEmpty: !emptyText,
|
|
193
|
+
noMore: !more,
|
|
194
|
+
query, noSearch: status !== "ready" || catalogEmpty,
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
// What the last load read: the first page the section shows, and (once a search needs it) every service of the site.
|
|
198
|
+
let first: WixService[] = [];
|
|
199
|
+
let firstMore = false;
|
|
200
|
+
let pool: Pool | undefined;
|
|
201
|
+
// Only the newest load and the newest search answer: a slow read never lands over what came after it.
|
|
202
|
+
let loads = 0;
|
|
203
|
+
let searches = 0;
|
|
204
|
+
// What was typed last, as soon as the search action runs: a load that lands while a search still reads answers for it (the state
|
|
205
|
+
// only carries the query once that search returns).
|
|
206
|
+
let typed = "";
|
|
207
|
+
// The full read in flight and the load it belongs to: keystrokes share it, and a newer load starts its own.
|
|
208
|
+
let reading: { seq: number; read: Promise<Pool> } | undefined;
|
|
209
|
+
/** Every service the search looks through, read once per load (`seq`), a failed read included: the next keystroke never asks a
|
|
210
|
+
* failing backend again, and the next appearance (a new load) does. */
|
|
211
|
+
const readAll = (seq: number): Promise<Pool> => {
|
|
212
|
+
if (pool) return Promise.resolve(pool);
|
|
213
|
+
if (!firstMore) return Promise.resolve(pool = { services: first, partial: false, failed: false });
|
|
214
|
+
if (reading?.seq === seq) return reading.read;
|
|
215
|
+
const read = (async (): Promise<Pool> => {
|
|
216
|
+
const out: WixService[] = [];
|
|
217
|
+
let result: Pool;
|
|
218
|
+
try {
|
|
219
|
+
let next = "0";
|
|
220
|
+
for (let page = 0; page < MAX_PAGES && next; page++) {
|
|
221
|
+
const response = await queryServices({ query: { filter: FILTER, paging: { limit: PAGE, offset: Number(next) }, sort: SORT } });
|
|
222
|
+
const services = response.services ?? [];
|
|
223
|
+
out.push(...services.filter((service) => !!service.id));
|
|
224
|
+
next = offsetPage(next, services.length, PAGE, response.pagingMetadata).nextCursor;
|
|
225
|
+
}
|
|
226
|
+
result = { services: out, partial: !!next, failed: false };
|
|
227
|
+
} catch (error) {
|
|
228
|
+
console.warn("services_section: the services to search could not be read", error);
|
|
229
|
+
result = { services: out.length ? out : first, partial: true, failed: true };
|
|
230
|
+
}
|
|
231
|
+
if (seq === loads) pool = result;
|
|
232
|
+
if (reading?.seq === seq) reading = undefined;
|
|
233
|
+
return result;
|
|
234
|
+
})();
|
|
235
|
+
reading = { seq, read };
|
|
236
|
+
return read;
|
|
237
|
+
};
|
|
238
|
+
// At most SEARCH_ROWS matches, a page's worth of rows; See All when there are more, or when the search could not read every service.
|
|
239
|
+
const found = (from: Pool, query: string): ServicesSectionState => {
|
|
240
|
+
const hits = from.services.filter((service) => matches(service, query));
|
|
241
|
+
return view("ready", hits.slice(0, SEARCH_ROWS).map(serviceRow), { more: from.partial || hits.length > SEARCH_ROWS, query, failed: from.failed });
|
|
242
|
+
};
|
|
243
|
+
const listed = (query: string): ServicesSectionState =>
|
|
244
|
+
view("ready", first.slice(0, options.limit).map(serviceRow), { more: firstMore, query, catalogEmpty: !first.length });
|
|
245
|
+
// Annotated: the actions read `service.getState()`, so an inferred type would refer to itself (TS7022 under the strict check).
|
|
246
|
+
const initial = view("loading", []);
|
|
247
|
+
const service: ServiceHandle<ServicesSectionState> = defineService<ServicesSectionState, Actions>(serviceName, {
|
|
248
|
+
state: initial,
|
|
249
|
+
// A cold boot starts from the loading state, never last run's snapshot, and the section reads its services on every appearance
|
|
250
|
+
// anyway. The rows are `serviceRows`, a key no earlier worker wrote, so a runtime that ignores `notHydrated` cannot restore rows
|
|
251
|
+
// without this worker's fields either (a missing `noBook` shows an empty Book pill).
|
|
252
|
+
notHydrated: Object.keys(initial) as Array<keyof ServicesSectionState>,
|
|
106
253
|
actions: {
|
|
107
254
|
load: async (_state, arg) => {
|
|
108
255
|
stashCopy(arg);
|
|
256
|
+
const seq = ++loads;
|
|
109
257
|
try {
|
|
110
258
|
// One more than the section shows is enough to know there is more. The order is the business's own, as on the services screen.
|
|
111
|
-
const response = await queryServices({ query: { filter:
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
259
|
+
const response = await queryServices({ query: { filter: FILTER, paging: { limit: options.limit + 1 }, sort: SORT } });
|
|
260
|
+
if (seq !== loads) return undefined;
|
|
261
|
+
first = (response.services ?? []).filter((service) => !!service.id);
|
|
262
|
+
firstMore = first.length > options.limit;
|
|
263
|
+
pool = undefined;
|
|
264
|
+
// A load on coming back to the page (the onAppear again) keeps what was typed, including what changed while it read.
|
|
265
|
+
if (typed.trim()) {
|
|
266
|
+
const read = await readAll(seq);
|
|
267
|
+
if (seq !== loads) return undefined;
|
|
268
|
+
if (typed.trim()) return found(read, typed);
|
|
269
|
+
}
|
|
270
|
+
return listed(typed);
|
|
115
271
|
} catch (error) {
|
|
272
|
+
if (seq !== loads) return undefined;
|
|
116
273
|
console.warn("services_section: the services could not be read", error);
|
|
117
|
-
return view("error", [],
|
|
274
|
+
return view("error", [], { query: typed });
|
|
118
275
|
}
|
|
119
276
|
},
|
|
277
|
+
search: async (state, arg) => {
|
|
278
|
+
const query = queryOf(arg);
|
|
279
|
+
typed = query;
|
|
280
|
+
const mine = ++searches;
|
|
281
|
+
const seq = loads;
|
|
282
|
+
// Typed before the first read landed: kept, and the load applies it.
|
|
283
|
+
if (state.status !== "ready") return { ...state, query };
|
|
284
|
+
if (!query.trim()) return listed(query);
|
|
285
|
+
const read = await readAll(seq);
|
|
286
|
+
// A newer search, or a newer load (which answers for what is typed), wins over this one.
|
|
287
|
+
if (mine !== searches || seq !== loads || service.getState().status !== "ready") return undefined;
|
|
288
|
+
return found(read, query);
|
|
289
|
+
},
|
|
120
290
|
},
|
|
121
291
|
});
|
|
292
|
+
return service;
|
|
122
293
|
}
|