create-abcart-theme 0.10.1 → 1.0.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 +133 -127
- package/LICENSE +202 -202
- package/NOTICE +2 -2
- package/README.md +65 -65
- package/bin/create-abcart-theme.mjs +4 -4
- package/package.json +49 -49
- package/src/args.mjs +113 -113
- package/src/banner.mjs +21 -21
- package/src/cli.mjs +119 -119
- package/src/create.mjs +85 -82
- package/src/package-manager.mjs +32 -32
- package/src/style.mjs +17 -17
- package/templates/default/.agents/skills/{wocart-docs → abcart-docs}/SKILL.md +146 -146
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/SKILL.md +58 -58
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/inputs-and-rights.md +47 -47
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/layout-rtl-and-phone.md +36 -36
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/measure-the-design.md +60 -60
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/tokens-sections-content.md +103 -103
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/verify-and-report.md +102 -102
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/compare-design.cjs +230 -230
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/qa-pages.cjs +219 -219
- package/templates/default/.agents/skills/shopify-theme-asset-collector/SKILL.md +125 -125
- package/templates/default/.agents/skills/shopify-theme-asset-collector/scripts/collect_public_assets.py +370 -370
- package/templates/default/.dev.vars.example +12 -12
- package/templates/default/AGENTS.md +240 -240
- package/templates/default/README.md +184 -184
- package/templates/default/_gitignore +11 -11
- package/templates/default/astro.config.mjs +85 -85
- package/templates/default/package.json +36 -36
- package/templates/default/public/fonts/OFL.txt +96 -96
- package/templates/default/src/actions/index.ts +22 -22
- package/templates/default/src/components/cart/AddToCartButton.astro +45 -45
- package/templates/default/src/components/cart/CartDrawer.astro +47 -47
- package/templates/default/src/components/cart/CartSummary.astro +11 -11
- package/templates/default/src/components/cart/FreeShipping.astro +7 -7
- package/templates/default/src/components/checkout/CheckoutMain.astro +29 -29
- package/templates/default/src/components/checkout/OrderForm.astro +280 -280
- package/templates/default/src/components/checkout/ThankYouMain.astro +68 -68
- package/templates/default/src/components/landing/LandingImages.astro +51 -51
- package/templates/default/src/components/landing/LandingOrder.astro +46 -46
- package/templates/default/src/components/landing/LandingProduct.astro +41 -41
- package/templates/default/src/components/listing/ListingMain.astro +93 -93
- package/templates/default/src/components/listing/PageMain.astro +52 -52
- package/templates/default/src/components/listing/Pagination.astro +31 -31
- package/templates/default/src/components/listing/SortControl.astro +35 -35
- package/templates/default/src/components/product/BuyBox.astro +96 -96
- package/templates/default/src/components/product/Description.astro +24 -24
- package/templates/default/src/components/product/Gallery.astro +66 -66
- package/templates/default/src/components/product/MobileBuyBar.astro +29 -29
- package/templates/default/src/components/product/OfferChoice.astro +66 -66
- package/templates/default/src/components/product/ProductMain.astro +40 -40
- package/templates/default/src/components/product/ProductReviews.astro +72 -72
- package/templates/default/src/components/product/RelatedProducts.astro +30 -30
- package/templates/default/src/components/product/blocks/BenefitsBlock.astro +13 -13
- package/templates/default/src/components/product/blocks/CollapsibleBlock.astro +20 -20
- package/templates/default/src/components/product/blocks/DescriptionBlock.astro +19 -19
- package/templates/default/src/components/product/blocks/DetailsBlock.astro +33 -33
- package/templates/default/src/components/product/blocks/GalleryBlock.astro +12 -12
- package/templates/default/src/components/product/blocks/OrderBlock.astro +30 -30
- package/templates/default/src/components/product/blocks/PriceBlock.astro +23 -23
- package/templates/default/src/components/product/blocks/RatingBlock.astro +16 -16
- package/templates/default/src/components/product/blocks/StockBlock.astro +22 -22
- package/templates/default/src/components/product/blocks/TextBlock.astro +11 -11
- package/templates/default/src/components/product/blocks/TitleBlock.astro +20 -20
- package/templates/default/src/components/reviews/ReviewCard.astro +46 -46
- package/templates/default/src/components/reviews/ReviewForm.astro +113 -113
- package/templates/default/src/components/reviews/VoiceNote.astro +60 -60
- package/templates/default/src/components/sections/CollectionCards.astro +65 -65
- package/templates/default/src/components/sections/Countdown.astro +57 -57
- package/templates/default/src/components/sections/Faq.astro +44 -44
- package/templates/default/src/components/sections/FeaturedProduct.astro +71 -71
- package/templates/default/src/components/sections/Hero.astro +99 -99
- package/templates/default/src/components/sections/ImageBanner.astro +46 -46
- package/templates/default/src/components/sections/ProductGrid.astro +47 -47
- package/templates/default/src/components/sections/RichText.astro +35 -35
- package/templates/default/src/components/sections/Steps.astro +46 -46
- package/templates/default/src/components/sections/Testimonials.astro +57 -57
- package/templates/default/src/components/shell/Footer.astro +80 -80
- package/templates/default/src/components/shell/Header.astro +89 -89
- package/templates/default/src/components/shell/LanguageMenu.astro +33 -33
- package/templates/default/src/components/shell/MobileMenu.astro +65 -65
- package/templates/default/src/components/shell/SearchDialog.astro +19 -19
- package/templates/default/src/components/shell/TopBar.astro +54 -54
- package/templates/default/src/components/shell/WhatsAppWidget.astro +111 -111
- package/templates/default/src/components/store/ContactMain.astro +79 -79
- package/templates/default/src/components/store/SearchMain.astro +56 -56
- package/templates/default/src/components/store/TrackOrderMain.astro +161 -161
- package/templates/default/src/components/surface/Area.astro +20 -20
- package/templates/default/src/components/surface/PreviewBridge.astro +24 -24
- package/templates/default/src/components/surface/Sections.astro +35 -35
- package/templates/default/src/components/ui/Breadcrumbs.astro +36 -36
- package/templates/default/src/components/ui/Button.astro +45 -45
- package/templates/default/src/components/ui/EditorPlaceholder.astro +24 -24
- package/templates/default/src/components/ui/Icon.astro +93 -93
- package/templates/default/src/components/ui/Logo.astro +36 -36
- package/templates/default/src/components/ui/Photo.astro +46 -46
- package/templates/default/src/components/ui/Price.astro +24 -24
- package/templates/default/src/components/ui/ProductCard.astro +41 -41
- package/templates/default/src/components/ui/Rating.astro +22 -22
- package/templates/default/src/components/ui/Section.astro +34 -34
- package/templates/default/src/components/ui/SectionHeading.astro +19 -19
- package/templates/default/src/env.d.ts +23 -23
- package/templates/default/src/layouts/StoreLayout.astro +145 -145
- package/templates/default/src/lib/browser-copy.ts +37 -37
- package/templates/default/src/lib/context.ts +89 -89
- package/templates/default/src/lib/coupon-notice.ts +47 -47
- package/templates/default/src/lib/i18n.ts +32 -32
- package/templates/default/src/lib/images.ts +38 -38
- package/templates/default/src/lib/listing-query.ts +41 -41
- package/templates/default/src/lib/money.ts +16 -16
- package/templates/default/src/lib/motion.ts +16 -16
- package/templates/default/src/lib/nav.ts +26 -26
- package/templates/default/src/lib/offer-tiles.ts +105 -105
- package/templates/default/src/lib/order-outcome.ts +66 -66
- package/templates/default/src/lib/otp-message.ts +21 -21
- package/templates/default/src/lib/products.ts +38 -38
- package/templates/default/src/lib/review-refusals.ts +31 -31
- package/templates/default/src/lib/store.ts +31 -31
- package/templates/default/src/lib/whatsapp.ts +29 -29
- package/templates/default/src/middleware.ts +23 -23
- package/templates/default/src/pages/404.astro +22 -22
- package/templates/default/src/pages/{wocart-preview.astro → abcart-preview.astro} +43 -43
- package/templates/default/src/pages/{wocart-revalidate.ts → abcart-revalidate.ts} +10 -10
- package/templates/default/src/pages/{wocart-styles.css.ts → abcart-styles.css.ts} +5 -5
- package/templates/default/src/pages/{wocart-theme.json.ts → abcart-theme.json.ts} +5 -5
- package/templates/default/src/pages/api/abandoned/convert.ts +4 -4
- package/templates/default/src/pages/api/abandoned/upsert.ts +4 -4
- package/templates/default/src/pages/api/cart/validate.ts +4 -4
- package/templates/default/src/pages/api/communes/[wilayaId].ts +4 -4
- package/templates/default/src/pages/api/otp/send.ts +4 -4
- package/templates/default/src/pages/api/otp/verify.ts +4 -4
- package/templates/default/src/pages/api/reviews/[productId].ts +3 -3
- package/templates/default/src/pages/api/reviews/eligibility.ts +3 -3
- package/templates/default/src/pages/api/reviews/submit.ts +3 -3
- package/templates/default/src/pages/api/reviews/upload.ts +3 -3
- package/templates/default/src/pages/category/[slug].astro +20 -20
- package/templates/default/src/pages/checkout.astro +25 -25
- package/templates/default/src/pages/collections/[handle].astro +20 -20
- package/templates/default/src/pages/contact.astro +17 -17
- package/templates/default/src/pages/index.astro +9 -9
- package/templates/default/src/pages/lp/[slug].astro +47 -47
- package/templates/default/src/pages/pages/[slug].astro +16 -16
- package/templates/default/src/pages/products/[handle].astro +37 -37
- package/templates/default/src/pages/products/index.astro +15 -15
- package/templates/default/src/pages/robots.txt.ts +9 -9
- package/templates/default/src/pages/search.astro +19 -19
- package/templates/default/src/pages/sitemap.xml.ts +12 -12
- package/templates/default/src/pages/thank-you.astro +33 -33
- package/templates/default/src/pages/track-order.astro +17 -17
- package/templates/default/src/scripts/cart.ts +371 -371
- package/templates/default/src/scripts/checkout/coupon.ts +96 -96
- package/templates/default/src/scripts/checkout/delivery.ts +86 -86
- package/templates/default/src/scripts/checkout/form.ts +54 -54
- package/templates/default/src/scripts/checkout/otp.ts +106 -106
- package/templates/default/src/scripts/checkout/picker.ts +169 -169
- package/templates/default/src/scripts/checkout/quote.ts +169 -169
- package/templates/default/src/scripts/checkout/submit.ts +113 -113
- package/templates/default/src/scripts/checkout/tracking.ts +74 -74
- package/templates/default/src/scripts/countdown.ts +29 -29
- package/templates/default/src/scripts/events.ts +4 -4
- package/templates/default/src/scripts/hero.ts +49 -49
- package/templates/default/src/scripts/listing.ts +13 -13
- package/templates/default/src/scripts/order-form.ts +100 -100
- package/templates/default/src/scripts/product.ts +306 -306
- package/templates/default/src/scripts/public-config.ts +79 -79
- package/templates/default/src/scripts/reviews.ts +358 -358
- package/templates/default/src/scripts/site.ts +131 -131
- package/templates/default/src/scripts/thank-you.ts +11 -11
- package/templates/default/src/scripts/voice-note.ts +94 -94
- package/templates/default/src/styles/base.css +1030 -1030
- package/templates/default/src/styles/checkout.css +1111 -1111
- package/templates/default/src/styles/global.css +12 -12
- package/templates/default/src/styles/product.css +871 -871
- package/templates/default/src/styles/reviews.css +533 -533
- package/templates/default/src/styles/sections.css +931 -931
- package/templates/default/src/styles/shell.css +970 -970
- package/templates/default/src/styles/tokens.css +583 -583
- package/templates/default/src/theme/api-routes.ts +10 -10
- package/templates/default/src/theme/content/catalog.ts +117 -117
- package/templates/default/src/theme/content/checkout.ts +168 -168
- package/templates/default/src/theme/content/editor.ts +21 -21
- package/templates/default/src/theme/content/index.ts +45 -45
- package/templates/default/src/theme/content/reviews.ts +76 -76
- package/templates/default/src/theme/content/shell.ts +59 -59
- package/templates/default/src/theme/content/types.ts +12 -12
- package/templates/default/src/theme/documents/catalog.json +11 -11
- package/templates/default/src/theme/documents/category.json +11 -11
- package/templates/default/src/theme/documents/checkout.json +11 -11
- package/templates/default/src/theme/documents/collection.json +11 -11
- package/templates/default/src/theme/documents/contact.json +11 -11
- package/templates/default/src/theme/documents/footer.area.json +11 -11
- package/templates/default/src/theme/documents/header.area.json +16 -16
- package/templates/default/src/theme/documents/home.json +313 -313
- package/templates/default/src/theme/documents/landing.json +8 -8
- package/templates/default/src/theme/documents/page.json +11 -11
- package/templates/default/src/theme/documents/product.json +125 -125
- package/templates/default/src/theme/documents/search.json +11 -11
- package/templates/default/src/theme/documents/thank-you.json +11 -11
- package/templates/default/src/theme/documents/track-order.json +11 -11
- package/templates/default/src/theme/fields.ts +61 -61
- package/templates/default/src/theme/key.ts +11 -11
- package/templates/default/src/theme/labels.ts +231 -231
- package/templates/default/src/theme/locales.ts +11 -11
- package/templates/default/src/theme/palette.ts +14 -14
- package/templates/default/src/theme/sections/collection-cards.ts +65 -65
- package/templates/default/src/theme/sections/faq.ts +74 -74
- package/templates/default/src/theme/sections/featured-product.ts +42 -42
- package/templates/default/src/theme/sections/hero.ts +47 -47
- package/templates/default/src/theme/sections/image-banner.ts +25 -25
- package/templates/default/src/theme/sections/index.ts +40 -40
- package/templates/default/src/theme/sections/landing.ts +150 -150
- package/templates/default/src/theme/sections/listing.ts +84 -84
- package/templates/default/src/theme/sections/product-grid.ts +42 -42
- package/templates/default/src/theme/sections/product.ts +297 -297
- package/templates/default/src/theme/sections/rich-text.ts +39 -39
- package/templates/default/src/theme/sections/shared.ts +54 -54
- package/templates/default/src/theme/sections/shell.ts +85 -85
- package/templates/default/src/theme/sections/store-pages.ts +101 -101
- package/templates/default/src/theme/sections/testimonials.ts +61 -61
- package/templates/default/src/theme/sections/thank-you.ts +35 -35
- package/templates/default/src/theme/settings.ts +124 -124
- package/templates/default/src/theme/theme.ts +65 -65
- package/templates/default/tests/fixtures/canvas-tote.json +62 -62
- package/templates/default/tests/fixtures/linen-shirt.json +128 -128
- package/templates/default/tests/unit/checkout.test.ts +241 -241
- package/templates/default/tests/unit/contract.test.ts +170 -169
- package/templates/default/tests/unit/contrast.test.ts +68 -68
- package/templates/default/tests/unit/copy.test.ts +101 -101
- package/templates/default/tests/unit/design-values.test.ts +95 -95
- package/templates/default/tests/unit/editability.test.ts +180 -180
- package/templates/default/tests/unit/fixtures.ts +91 -91
- package/templates/default/tests/unit/footer.test.ts +54 -54
- package/templates/default/tests/unit/guards.test.ts +340 -340
- package/templates/default/tests/unit/images.test.ts +26 -26
- package/templates/default/tests/unit/landing.test.ts +151 -151
- package/templates/default/tests/unit/languages.test.ts +42 -42
- package/templates/default/tests/unit/middleware.test.ts +53 -53
- package/templates/default/tests/unit/offer-tiles.test.ts +75 -75
- package/templates/default/tests/unit/render.ts +38 -38
- package/templates/default/tests/unit/reviews.test.ts +25 -25
- package/templates/default/tests/unit/rules.test.ts +59 -59
- package/templates/default/tests/unit/source.ts +75 -75
- package/templates/default/tests/unit/store-pages.test.ts +147 -147
- package/templates/default/tests/unit/studio.test.ts +202 -202
- package/templates/default/tests/unit/testimonials.test.ts +52 -52
- package/templates/default/tests/unit/thank-you.test.ts +52 -52
- package/templates/default/tests/unit/tracking.test.ts +29 -29
- package/templates/default/tests/unit/voice-note.test.ts +42 -42
- package/templates/default/tsconfig.json +13 -13
- package/templates/default/vitest.config.ts +15 -15
- package/templates/default/wrangler.jsonc +16 -16
|
@@ -1,358 +1,358 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reviews: play a voice note, open a photo, show the next page, write one.
|
|
3
|
-
* Writing proves the purchase first, then sends each photo (shrunk in the
|
|
4
|
-
* browser) and the voice note the moment it is added, with its progress and a
|
|
5
|
-
* retry, so sending the review only names what already arrived. Every rule is
|
|
6
|
-
* the server's; this script shows its answers in the page's words.
|
|
7
|
-
*/
|
|
8
|
-
import { REVIEW_TEXT_LIMITS, STOREFRONT_API_PATHS, storefrontApiPath, type Review, type ReviewEligibility } from "@abcart/storefront";
|
|
9
|
-
import { canRecordVoice, createReviewSession, createVoiceRecorder, type VoiceRecorder } from "@abcart/storefront/review-media";
|
|
10
|
-
import { formatMessage } from "@abcart/theme/messages";
|
|
11
|
-
import { bindEach } from "@abcart/theme/lifecycle";
|
|
12
|
-
import { reviewRefusalWords, type ReviewStep } from "@/lib/review-refusals";
|
|
13
|
-
import { readPublicConfig } from "./public-config";
|
|
14
|
-
import { bindVoice, clock } from "./voice-note";
|
|
15
|
-
|
|
16
|
-
const config = readPublicConfig();
|
|
17
|
-
const PAGE = 6;
|
|
18
|
-
|
|
19
|
-
// ── Photos and the next page ──────────────────────────────────────────────
|
|
20
|
-
|
|
21
|
-
bindEach<HTMLElement>("#reviews", (section) => {
|
|
22
|
-
const dialog = section.querySelector<HTMLDialogElement>("[data-photo-dialog]");
|
|
23
|
-
const full = dialog?.querySelector<HTMLImageElement>("[data-photo-full]");
|
|
24
|
-
section.addEventListener("click", (event) => {
|
|
25
|
-
const photo = (event.target as HTMLElement).closest<HTMLElement>("[data-review-photo]");
|
|
26
|
-
if (!photo || !dialog || !full) return;
|
|
27
|
-
full.src = photo.dataset.reviewPhoto ?? "";
|
|
28
|
-
dialog.showModal();
|
|
29
|
-
});
|
|
30
|
-
dialog?.addEventListener("click", (event) => {
|
|
31
|
-
if (event.target === dialog || (event.target as HTMLElement).closest("[data-photo-close]")) dialog.close();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const list = section.querySelector<HTMLElement>("[data-reviews-list]");
|
|
35
|
-
const template = section.querySelector<HTMLTemplateElement>("[data-review-template]");
|
|
36
|
-
const more = section.querySelector<HTMLButtonElement>("[data-reviews-more]");
|
|
37
|
-
if (!list || !template || !more) return;
|
|
38
|
-
more.addEventListener("click", async () => {
|
|
39
|
-
more.disabled = true;
|
|
40
|
-
try {
|
|
41
|
-
const loaded = Number(list.dataset.loaded) || 0;
|
|
42
|
-
const response = await fetch(`${storefrontApiPath("reviewsList", { productId: list.dataset.productId ?? "" })}?limit=${PAGE}&offset=${loaded}`);
|
|
43
|
-
const answer = (await response.json()) as { data?: Review[]; total?: number };
|
|
44
|
-
for (const review of answer.data ?? []) list.append(card(template, review));
|
|
45
|
-
list.dataset.loaded = String(loaded + (answer.data?.length ?? 0));
|
|
46
|
-
if (Number(list.dataset.loaded) >= (answer.total ?? 0) || (answer.data?.length ?? 0) === 0) more.parentElement?.remove();
|
|
47
|
-
} finally {
|
|
48
|
-
more.disabled = false;
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
function card(template: HTMLTemplateElement, review: Review): HTMLElement {
|
|
54
|
-
const node = template.content.firstElementChild!.cloneNode(true) as HTMLElement;
|
|
55
|
-
const set = (selector: string, text: string | null | undefined) => {
|
|
56
|
-
const element = node.querySelector<HTMLElement>(selector);
|
|
57
|
-
if (!element) return;
|
|
58
|
-
element.textContent = text ?? "";
|
|
59
|
-
element.hidden = !text;
|
|
60
|
-
};
|
|
61
|
-
set("[data-review-name]", review.customerName);
|
|
62
|
-
set("[data-review-initial]", review.customerName.slice(0, 1));
|
|
63
|
-
set("[data-review-title]", review.title);
|
|
64
|
-
set("[data-review-body]", review.body);
|
|
65
|
-
const date = node.querySelector<HTMLTimeElement>("[data-review-date]");
|
|
66
|
-
if (date) {
|
|
67
|
-
date.dateTime = review.createdAt;
|
|
68
|
-
date.textContent = new Intl.DateTimeFormat(config.localeTag, { day: "numeric", month: "long", year: "numeric" }).format(new Date(review.createdAt));
|
|
69
|
-
}
|
|
70
|
-
node.querySelector<HTMLElement>("[data-review-verified]")!.hidden = !review.verified;
|
|
71
|
-
const rating = Math.round(review.rating);
|
|
72
|
-
const stars = node.querySelector<HTMLElement>("[data-review-stars]");
|
|
73
|
-
stars?.querySelectorAll("svg").forEach((star, index) => star.classList.toggle("star-empty", index >= rating));
|
|
74
|
-
stars?.setAttribute("aria-label", formatMessage(config.content, "a11y.rating", { rating: String(rating) }, { locale: config.localeTag }));
|
|
75
|
-
const photos = node.querySelector<HTMLElement>("[data-review-photos]");
|
|
76
|
-
const images = review.media.filter((media) => media.kind === "image");
|
|
77
|
-
if (photos) {
|
|
78
|
-
photos.hidden = images.length === 0;
|
|
79
|
-
for (const image of images) {
|
|
80
|
-
const button = document.createElement("button");
|
|
81
|
-
button.type = "button";
|
|
82
|
-
button.className = "review-card__photo";
|
|
83
|
-
button.dataset.reviewPhoto = image.url;
|
|
84
|
-
button.setAttribute("aria-label", config.content["reviews.openPhoto"] ?? "");
|
|
85
|
-
const img = document.createElement("img");
|
|
86
|
-
img.src = image.thumbUrl ?? image.url;
|
|
87
|
-
img.alt = "";
|
|
88
|
-
img.width = 76;
|
|
89
|
-
img.height = 76;
|
|
90
|
-
img.loading = "lazy";
|
|
91
|
-
button.append(img);
|
|
92
|
-
photos.append(button);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const voice = review.media.find((media) => media.kind === "audio");
|
|
96
|
-
const voiceBox = node.querySelector<HTMLElement>("[data-review-voice]");
|
|
97
|
-
if (voiceBox) {
|
|
98
|
-
voiceBox.hidden = !voice;
|
|
99
|
-
if (voice) {
|
|
100
|
-
const player = voiceBox.querySelector<HTMLElement>("[data-voice-note]")!;
|
|
101
|
-
player.querySelector<HTMLAudioElement>("[data-voice-audio]")!.src = voice.url;
|
|
102
|
-
player.dataset.durationMs = String(voice.durationMs ?? 0);
|
|
103
|
-
const time = player.querySelector<HTMLElement>("[data-voice-time]")!;
|
|
104
|
-
time.dataset.length = voice.durationMs ? clock(voice.durationMs / 1000) : "";
|
|
105
|
-
time.textContent = time.dataset.length;
|
|
106
|
-
bindVoice(player);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return node;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// ── Writing a review ──────────────────────────────────────────────────────
|
|
113
|
-
|
|
114
|
-
type Copy = Record<string, string>;
|
|
115
|
-
interface Upload {
|
|
116
|
-
kind: "image" | "audio";
|
|
117
|
-
node: HTMLLIElement;
|
|
118
|
-
file: Blob;
|
|
119
|
-
durationMs?: number;
|
|
120
|
-
uploadId: string | null;
|
|
121
|
-
state: "sending" | "sent" | "failed";
|
|
122
|
-
cancel?: () => void;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
bindEach<HTMLDialogElement>("[data-review-dialog]", (dialog) => {
|
|
126
|
-
const root = dialog.querySelector<HTMLElement>("[data-review-form]")!;
|
|
127
|
-
const copy: Copy = JSON.parse(root.dataset.copy ?? "{}");
|
|
128
|
-
const say = (key: string, values?: Record<string, string | number>) => formatMessage(copy, key, values, { locale: config.localeTag });
|
|
129
|
-
const $ = <T extends Element>(selector: string) => root.querySelector<T>(selector)!;
|
|
130
|
-
const find = $<HTMLFormElement>("[data-review-find]");
|
|
131
|
-
const write = $<HTMLFormElement>("[data-review-write]");
|
|
132
|
-
const error = $<HTMLElement>("[data-review-error]");
|
|
133
|
-
const success = $<HTMLElement>("[data-review-success]");
|
|
134
|
-
const fileTemplate = $<HTMLTemplateElement>("[data-review-file-template]");
|
|
135
|
-
const recordButton = $<HTMLButtonElement>("[data-review-record]");
|
|
136
|
-
const stopButton = $<HTMLButtonElement>("[data-review-stop]");
|
|
137
|
-
const timer = $<HTMLElement>("[data-review-timer]");
|
|
138
|
-
const stars = Array.from(root.querySelectorAll<HTMLButtonElement>("[data-star]"));
|
|
139
|
-
|
|
140
|
-
const session = createReviewSession({ routes: { eligibility: STOREFRONT_API_PATHS.reviewEligibility, upload: STOREFRONT_API_PATHS.reviewUpload, submit: STOREFRONT_API_PATHS.reviewSubmit } });
|
|
141
|
-
let rating = 0;
|
|
142
|
-
let files: Upload[] = [];
|
|
143
|
-
let recorder: VoiceRecorder | null = null;
|
|
144
|
-
|
|
145
|
-
const showError = (message: string) => {
|
|
146
|
-
error.textContent = message;
|
|
147
|
-
error.hidden = false;
|
|
148
|
-
error.scrollIntoView({ block: "nearest" });
|
|
149
|
-
};
|
|
150
|
-
const clearError = () => {
|
|
151
|
-
error.hidden = true;
|
|
152
|
-
error.textContent = "";
|
|
153
|
-
};
|
|
154
|
-
const refusal = (code: string | null, step: ReviewStep) => say(reviewRefusalWords(code, step), { count: REVIEW_TEXT_LIMITS.bodyMin });
|
|
155
|
-
const busy = (button: HTMLButtonElement, on: boolean) => {
|
|
156
|
-
button.disabled = on;
|
|
157
|
-
button.toggleAttribute("aria-busy", on);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
document.querySelectorAll<HTMLElement>("[data-review-open]").forEach((button) => button.addEventListener("click", () => dialog.showModal()));
|
|
161
|
-
dialog.querySelectorAll<HTMLElement>("[data-review-close]").forEach((button) => button.addEventListener("click", () => dialog.close()));
|
|
162
|
-
dialog.addEventListener("click", (event) => event.target === dialog && dialog.close());
|
|
163
|
-
dialog.addEventListener("close", () => recorder?.cancel());
|
|
164
|
-
|
|
165
|
-
find.addEventListener("submit", async (event) => {
|
|
166
|
-
event.preventDefault();
|
|
167
|
-
clearError();
|
|
168
|
-
const data = new FormData(find);
|
|
169
|
-
const orderNumber = String(data.get("orderNumber") ?? "").trim();
|
|
170
|
-
const phone = String(data.get("phone") ?? "").trim();
|
|
171
|
-
if (!orderNumber || !phone) return showError(say("reviews.errorOrder"));
|
|
172
|
-
const button = $<HTMLButtonElement>("[data-review-find-button]");
|
|
173
|
-
busy(button, true);
|
|
174
|
-
const found = await session.find({ orderNumber, phone });
|
|
175
|
-
busy(button, false);
|
|
176
|
-
if (found.status === "refused") return showError(refusal(found.code, "find"));
|
|
177
|
-
if (!found.eligibility.products.some((product) => !product.reviewed)) return showError(say("reviews.nothingLeft"));
|
|
178
|
-
openWriting(found.eligibility);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
function openWriting(eligibility: ReviewEligibility) {
|
|
182
|
-
$<HTMLElement>("[data-review-order]").textContent = say("reviews.orderFound", { order: eligibility.orderNumber });
|
|
183
|
-
const list = $<HTMLElement>("[data-review-product-list]");
|
|
184
|
-
const template = $<HTMLTemplateElement>("[data-review-product-template]");
|
|
185
|
-
list.replaceChildren();
|
|
186
|
-
const preferred =
|
|
187
|
-
eligibility.products.find((product) => product.productId === root.dataset.productId && !product.reviewed) ??
|
|
188
|
-
eligibility.products.find((product) => !product.reviewed);
|
|
189
|
-
for (const product of eligibility.products) {
|
|
190
|
-
const option = template.content.firstElementChild!.cloneNode(true) as HTMLLabelElement;
|
|
191
|
-
const input = option.querySelector("input")!;
|
|
192
|
-
input.value = product.productId;
|
|
193
|
-
input.disabled = product.reviewed;
|
|
194
|
-
input.checked = product.productId === preferred?.productId;
|
|
195
|
-
option.querySelector<HTMLElement>("[data-name]")!.textContent = product.name;
|
|
196
|
-
option.querySelector<HTMLElement>("[data-done]")!.hidden = !product.reviewed;
|
|
197
|
-
list.append(option);
|
|
198
|
-
}
|
|
199
|
-
$<HTMLElement>("[data-review-products]").hidden = eligibility.products.length < 2;
|
|
200
|
-
const media = eligibility.media;
|
|
201
|
-
$<HTMLElement>("[data-review-photos-block]").hidden = !media || media.maxPhotos === 0;
|
|
202
|
-
$<HTMLElement>("[data-review-voice-block]").hidden = !media || media.voiceMaxSeconds === null;
|
|
203
|
-
if (media?.maxPhotos) $<HTMLElement>("[data-review-photos-limit]").textContent = say("reviews.photosLimit", { count: media.maxPhotos });
|
|
204
|
-
if (media?.voiceMaxSeconds) {
|
|
205
|
-
$<HTMLElement>("[data-review-voice-limit]").textContent = say("reviews.voiceLimit", { count: media.voiceMaxSeconds });
|
|
206
|
-
recordButton.hidden = !canRecordVoice();
|
|
207
|
-
}
|
|
208
|
-
find.hidden = true;
|
|
209
|
-
write.hidden = false;
|
|
210
|
-
stars[0]?.focus();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
$<HTMLButtonElement>("[data-review-change]").addEventListener("click", () => {
|
|
214
|
-
recorder?.cancel();
|
|
215
|
-
session.reset();
|
|
216
|
-
files = [];
|
|
217
|
-
$<HTMLElement>("[data-review-photo-list]").replaceChildren();
|
|
218
|
-
$<HTMLElement>("[data-review-voice-list]").replaceChildren();
|
|
219
|
-
write.hidden = true;
|
|
220
|
-
find.hidden = false;
|
|
221
|
-
clearError();
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
const paintStars = (value: number) => stars.forEach((star) => star.classList.toggle("is-on", Number(star.dataset.star) <= value));
|
|
225
|
-
stars.forEach((star) => {
|
|
226
|
-
star.addEventListener("click", () => {
|
|
227
|
-
rating = Number(star.dataset.star);
|
|
228
|
-
stars.forEach((other) => other.setAttribute("aria-checked", String(other === star)));
|
|
229
|
-
paintStars(rating);
|
|
230
|
-
});
|
|
231
|
-
star.addEventListener("mouseenter", () => paintStars(Number(star.dataset.star)));
|
|
232
|
-
star.addEventListener("mouseleave", () => paintStars(rating));
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
const sentVoice = () => files.some((item) => item.kind === "audio" && item.state === "sent");
|
|
236
|
-
const bodyHint = () => ($<HTMLElement>("[data-review-body-hint]").hidden = !files.some((item) => item.kind === "audio"));
|
|
237
|
-
|
|
238
|
-
function show(item: Upload, state: Upload["state"], progress = 0, message?: string) {
|
|
239
|
-
item.state = state;
|
|
240
|
-
item.node.dataset.state = state;
|
|
241
|
-
item.node.querySelector<HTMLElement>("[data-status]")!.textContent = state === "failed" ? message ?? say("reviews.notSent") : state === "sent" ? "" : say("reviews.sending");
|
|
242
|
-
item.node.querySelector<HTMLElement>("[data-progress]")!.style.inlineSize = `${Math.round((state === "sent" ? 1 : progress) * 100)}%`;
|
|
243
|
-
item.node.querySelector<HTMLElement>("[data-retry]")!.hidden = state !== "failed";
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function send(item: Upload) {
|
|
247
|
-
show(item, "sending");
|
|
248
|
-
const upload = session.upload(item.file, { kind: item.kind, ...(item.durationMs ? { durationMs: item.durationMs } : {}), onProgress: (share) => show(item, "sending", share) });
|
|
249
|
-
item.cancel = upload.cancel;
|
|
250
|
-
void upload.result.then((result) => {
|
|
251
|
-
if (result.status === "cancelled" || !files.includes(item)) return;
|
|
252
|
-
if (result.status === "sent") {
|
|
253
|
-
item.uploadId = result.uploadId;
|
|
254
|
-
show(item, "sent");
|
|
255
|
-
} else show(item, "failed", 0, refusal(result.code, "upload"));
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function add(kind: Upload["kind"], file: Blob, durationMs?: number) {
|
|
260
|
-
const node = fileTemplate.content.firstElementChild!.cloneNode(true) as HTMLLIElement;
|
|
261
|
-
const item: Upload = { kind, node, file, ...(durationMs ? { durationMs } : {}), uploadId: null, state: "sending" };
|
|
262
|
-
const preview = URL.createObjectURL(file);
|
|
263
|
-
const media = node.querySelector<HTMLImageElement | HTMLAudioElement>(kind === "image" ? "img" : "audio")!;
|
|
264
|
-
media.src = preview;
|
|
265
|
-
media.hidden = false;
|
|
266
|
-
node.querySelector<HTMLButtonElement>("[data-remove]")!.addEventListener("click", () => {
|
|
267
|
-
item.cancel?.();
|
|
268
|
-
URL.revokeObjectURL(preview);
|
|
269
|
-
node.remove();
|
|
270
|
-
files = files.filter((other) => other !== item);
|
|
271
|
-
bodyHint();
|
|
272
|
-
});
|
|
273
|
-
node.querySelector<HTMLButtonElement>("[data-retry]")!.addEventListener("click", () => send(item));
|
|
274
|
-
files.push(item);
|
|
275
|
-
$<HTMLElement>(kind === "image" ? "[data-review-photo-list]" : "[data-review-voice-list]").append(node);
|
|
276
|
-
bodyHint();
|
|
277
|
-
send(item);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
$<HTMLInputElement>("[data-review-photo-file]").addEventListener("change", (event) => {
|
|
281
|
-
const input = event.target as HTMLInputElement;
|
|
282
|
-
const max = session.eligibility?.media?.maxPhotos ?? 0;
|
|
283
|
-
const room = max - files.filter((item) => item.kind === "image").length;
|
|
284
|
-
const picked = Array.from(input.files ?? []).filter((file) => file.type.startsWith("image/"));
|
|
285
|
-
if (picked.length > room) showError(say("reviews.tooManyPhotos", { count: max }));
|
|
286
|
-
for (const file of picked.slice(0, Math.max(room, 0))) add("image", file);
|
|
287
|
-
input.value = "";
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
const replaceVoice = (blob: Blob, durationMs?: number) => {
|
|
291
|
-
for (const item of files.filter((other) => other.kind === "audio")) item.node.querySelector<HTMLButtonElement>("[data-remove]")!.click();
|
|
292
|
-
add("audio", blob, durationMs);
|
|
293
|
-
};
|
|
294
|
-
$<HTMLInputElement>("[data-review-voice-file]").addEventListener("change", (event) => {
|
|
295
|
-
const input = event.target as HTMLInputElement;
|
|
296
|
-
const file = input.files?.[0];
|
|
297
|
-
if (file) replaceVoice(file);
|
|
298
|
-
input.value = "";
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
const finishRecording = (blob: Blob, durationMs: number) => {
|
|
302
|
-
recorder = null;
|
|
303
|
-
stopButton.hidden = true;
|
|
304
|
-
recordButton.hidden = false;
|
|
305
|
-
replaceVoice(blob, durationMs);
|
|
306
|
-
};
|
|
307
|
-
recordButton.addEventListener("click", async () => {
|
|
308
|
-
const max = session.eligibility?.media?.voiceMaxSeconds;
|
|
309
|
-
if (!max) return;
|
|
310
|
-
recorder = createVoiceRecorder({
|
|
311
|
-
maxSeconds: max,
|
|
312
|
-
onTick: (elapsed) => (timer.textContent = clock(elapsed / 1000)),
|
|
313
|
-
onLimit: (voice) => finishRecording(voice.blob, voice.durationMs),
|
|
314
|
-
});
|
|
315
|
-
try {
|
|
316
|
-
await recorder.start();
|
|
317
|
-
recordButton.hidden = true;
|
|
318
|
-
stopButton.hidden = false;
|
|
319
|
-
timer.textContent = "0:00";
|
|
320
|
-
} catch {
|
|
321
|
-
recorder = null;
|
|
322
|
-
showError(say("reviews.micDenied"));
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
stopButton.addEventListener("click", async () => {
|
|
326
|
-
if (!recorder || recorder.state !== "recording") return;
|
|
327
|
-
const voice = await recorder.stop();
|
|
328
|
-
finishRecording(voice.blob, voice.durationMs);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
write.addEventListener("submit", async (event) => {
|
|
332
|
-
event.preventDefault();
|
|
333
|
-
clearError();
|
|
334
|
-
const opened = session.eligibility;
|
|
335
|
-
if (!opened) return;
|
|
336
|
-
const productId = new FormData(write).get("productId")?.toString() ?? opened.products.find((product) => !product.reviewed)?.productId;
|
|
337
|
-
if (!productId) return showError(say("reviews.nothingLeft"));
|
|
338
|
-
if (!rating) return showError(say("reviews.ratingRequired"));
|
|
339
|
-
const body = $<HTMLTextAreaElement>("#review-body").value.trim();
|
|
340
|
-
if (body.length < REVIEW_TEXT_LIMITS.bodyMin && !sentVoice()) return showError(say("reviews.tooShort", { count: REVIEW_TEXT_LIMITS.bodyMin }));
|
|
341
|
-
if (files.some((item) => item.state !== "sent")) return showError(say("reviews.waitUploads"));
|
|
342
|
-
const button = $<HTMLButtonElement>("[data-review-submit]");
|
|
343
|
-
busy(button, true);
|
|
344
|
-
const title = $<HTMLInputElement>("#review-title").value.trim();
|
|
345
|
-
const submitted = await session.submit({
|
|
346
|
-
productId,
|
|
347
|
-
rating,
|
|
348
|
-
body,
|
|
349
|
-
...(title ? { title } : {}),
|
|
350
|
-
uploads: files.flatMap((item) => (item.uploadId ? [item.uploadId] : [])),
|
|
351
|
-
});
|
|
352
|
-
busy(button, false);
|
|
353
|
-
if (submitted.status === "refused") return showError(refusal(submitted.code, "submit"));
|
|
354
|
-
$<HTMLElement>("[data-review-success-text]").textContent = say(submitted.result.visibility === "published" ? "reviews.thanksPublished" : "reviews.thanksPending");
|
|
355
|
-
write.hidden = true;
|
|
356
|
-
success.hidden = false;
|
|
357
|
-
});
|
|
358
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Reviews: play a voice note, open a photo, show the next page, write one.
|
|
3
|
+
* Writing proves the purchase first, then sends each photo (shrunk in the
|
|
4
|
+
* browser) and the voice note the moment it is added, with its progress and a
|
|
5
|
+
* retry, so sending the review only names what already arrived. Every rule is
|
|
6
|
+
* the server's; this script shows its answers in the page's words.
|
|
7
|
+
*/
|
|
8
|
+
import { REVIEW_TEXT_LIMITS, STOREFRONT_API_PATHS, storefrontApiPath, type Review, type ReviewEligibility } from "@abcart/storefront";
|
|
9
|
+
import { canRecordVoice, createReviewSession, createVoiceRecorder, type VoiceRecorder } from "@abcart/storefront/review-media";
|
|
10
|
+
import { formatMessage } from "@abcart/theme/messages";
|
|
11
|
+
import { bindEach } from "@abcart/theme/lifecycle";
|
|
12
|
+
import { reviewRefusalWords, type ReviewStep } from "@/lib/review-refusals";
|
|
13
|
+
import { readPublicConfig } from "./public-config";
|
|
14
|
+
import { bindVoice, clock } from "./voice-note";
|
|
15
|
+
|
|
16
|
+
const config = readPublicConfig();
|
|
17
|
+
const PAGE = 6;
|
|
18
|
+
|
|
19
|
+
// ── Photos and the next page ──────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
bindEach<HTMLElement>("#reviews", (section) => {
|
|
22
|
+
const dialog = section.querySelector<HTMLDialogElement>("[data-photo-dialog]");
|
|
23
|
+
const full = dialog?.querySelector<HTMLImageElement>("[data-photo-full]");
|
|
24
|
+
section.addEventListener("click", (event) => {
|
|
25
|
+
const photo = (event.target as HTMLElement).closest<HTMLElement>("[data-review-photo]");
|
|
26
|
+
if (!photo || !dialog || !full) return;
|
|
27
|
+
full.src = photo.dataset.reviewPhoto ?? "";
|
|
28
|
+
dialog.showModal();
|
|
29
|
+
});
|
|
30
|
+
dialog?.addEventListener("click", (event) => {
|
|
31
|
+
if (event.target === dialog || (event.target as HTMLElement).closest("[data-photo-close]")) dialog.close();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const list = section.querySelector<HTMLElement>("[data-reviews-list]");
|
|
35
|
+
const template = section.querySelector<HTMLTemplateElement>("[data-review-template]");
|
|
36
|
+
const more = section.querySelector<HTMLButtonElement>("[data-reviews-more]");
|
|
37
|
+
if (!list || !template || !more) return;
|
|
38
|
+
more.addEventListener("click", async () => {
|
|
39
|
+
more.disabled = true;
|
|
40
|
+
try {
|
|
41
|
+
const loaded = Number(list.dataset.loaded) || 0;
|
|
42
|
+
const response = await fetch(`${storefrontApiPath("reviewsList", { productId: list.dataset.productId ?? "" })}?limit=${PAGE}&offset=${loaded}`);
|
|
43
|
+
const answer = (await response.json()) as { data?: Review[]; total?: number };
|
|
44
|
+
for (const review of answer.data ?? []) list.append(card(template, review));
|
|
45
|
+
list.dataset.loaded = String(loaded + (answer.data?.length ?? 0));
|
|
46
|
+
if (Number(list.dataset.loaded) >= (answer.total ?? 0) || (answer.data?.length ?? 0) === 0) more.parentElement?.remove();
|
|
47
|
+
} finally {
|
|
48
|
+
more.disabled = false;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
function card(template: HTMLTemplateElement, review: Review): HTMLElement {
|
|
54
|
+
const node = template.content.firstElementChild!.cloneNode(true) as HTMLElement;
|
|
55
|
+
const set = (selector: string, text: string | null | undefined) => {
|
|
56
|
+
const element = node.querySelector<HTMLElement>(selector);
|
|
57
|
+
if (!element) return;
|
|
58
|
+
element.textContent = text ?? "";
|
|
59
|
+
element.hidden = !text;
|
|
60
|
+
};
|
|
61
|
+
set("[data-review-name]", review.customerName);
|
|
62
|
+
set("[data-review-initial]", review.customerName.slice(0, 1));
|
|
63
|
+
set("[data-review-title]", review.title);
|
|
64
|
+
set("[data-review-body]", review.body);
|
|
65
|
+
const date = node.querySelector<HTMLTimeElement>("[data-review-date]");
|
|
66
|
+
if (date) {
|
|
67
|
+
date.dateTime = review.createdAt;
|
|
68
|
+
date.textContent = new Intl.DateTimeFormat(config.localeTag, { day: "numeric", month: "long", year: "numeric" }).format(new Date(review.createdAt));
|
|
69
|
+
}
|
|
70
|
+
node.querySelector<HTMLElement>("[data-review-verified]")!.hidden = !review.verified;
|
|
71
|
+
const rating = Math.round(review.rating);
|
|
72
|
+
const stars = node.querySelector<HTMLElement>("[data-review-stars]");
|
|
73
|
+
stars?.querySelectorAll("svg").forEach((star, index) => star.classList.toggle("star-empty", index >= rating));
|
|
74
|
+
stars?.setAttribute("aria-label", formatMessage(config.content, "a11y.rating", { rating: String(rating) }, { locale: config.localeTag }));
|
|
75
|
+
const photos = node.querySelector<HTMLElement>("[data-review-photos]");
|
|
76
|
+
const images = review.media.filter((media) => media.kind === "image");
|
|
77
|
+
if (photos) {
|
|
78
|
+
photos.hidden = images.length === 0;
|
|
79
|
+
for (const image of images) {
|
|
80
|
+
const button = document.createElement("button");
|
|
81
|
+
button.type = "button";
|
|
82
|
+
button.className = "review-card__photo";
|
|
83
|
+
button.dataset.reviewPhoto = image.url;
|
|
84
|
+
button.setAttribute("aria-label", config.content["reviews.openPhoto"] ?? "");
|
|
85
|
+
const img = document.createElement("img");
|
|
86
|
+
img.src = image.thumbUrl ?? image.url;
|
|
87
|
+
img.alt = "";
|
|
88
|
+
img.width = 76;
|
|
89
|
+
img.height = 76;
|
|
90
|
+
img.loading = "lazy";
|
|
91
|
+
button.append(img);
|
|
92
|
+
photos.append(button);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const voice = review.media.find((media) => media.kind === "audio");
|
|
96
|
+
const voiceBox = node.querySelector<HTMLElement>("[data-review-voice]");
|
|
97
|
+
if (voiceBox) {
|
|
98
|
+
voiceBox.hidden = !voice;
|
|
99
|
+
if (voice) {
|
|
100
|
+
const player = voiceBox.querySelector<HTMLElement>("[data-voice-note]")!;
|
|
101
|
+
player.querySelector<HTMLAudioElement>("[data-voice-audio]")!.src = voice.url;
|
|
102
|
+
player.dataset.durationMs = String(voice.durationMs ?? 0);
|
|
103
|
+
const time = player.querySelector<HTMLElement>("[data-voice-time]")!;
|
|
104
|
+
time.dataset.length = voice.durationMs ? clock(voice.durationMs / 1000) : "";
|
|
105
|
+
time.textContent = time.dataset.length;
|
|
106
|
+
bindVoice(player);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return node;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ── Writing a review ──────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
type Copy = Record<string, string>;
|
|
115
|
+
interface Upload {
|
|
116
|
+
kind: "image" | "audio";
|
|
117
|
+
node: HTMLLIElement;
|
|
118
|
+
file: Blob;
|
|
119
|
+
durationMs?: number;
|
|
120
|
+
uploadId: string | null;
|
|
121
|
+
state: "sending" | "sent" | "failed";
|
|
122
|
+
cancel?: () => void;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
bindEach<HTMLDialogElement>("[data-review-dialog]", (dialog) => {
|
|
126
|
+
const root = dialog.querySelector<HTMLElement>("[data-review-form]")!;
|
|
127
|
+
const copy: Copy = JSON.parse(root.dataset.copy ?? "{}");
|
|
128
|
+
const say = (key: string, values?: Record<string, string | number>) => formatMessage(copy, key, values, { locale: config.localeTag });
|
|
129
|
+
const $ = <T extends Element>(selector: string) => root.querySelector<T>(selector)!;
|
|
130
|
+
const find = $<HTMLFormElement>("[data-review-find]");
|
|
131
|
+
const write = $<HTMLFormElement>("[data-review-write]");
|
|
132
|
+
const error = $<HTMLElement>("[data-review-error]");
|
|
133
|
+
const success = $<HTMLElement>("[data-review-success]");
|
|
134
|
+
const fileTemplate = $<HTMLTemplateElement>("[data-review-file-template]");
|
|
135
|
+
const recordButton = $<HTMLButtonElement>("[data-review-record]");
|
|
136
|
+
const stopButton = $<HTMLButtonElement>("[data-review-stop]");
|
|
137
|
+
const timer = $<HTMLElement>("[data-review-timer]");
|
|
138
|
+
const stars = Array.from(root.querySelectorAll<HTMLButtonElement>("[data-star]"));
|
|
139
|
+
|
|
140
|
+
const session = createReviewSession({ routes: { eligibility: STOREFRONT_API_PATHS.reviewEligibility, upload: STOREFRONT_API_PATHS.reviewUpload, submit: STOREFRONT_API_PATHS.reviewSubmit } });
|
|
141
|
+
let rating = 0;
|
|
142
|
+
let files: Upload[] = [];
|
|
143
|
+
let recorder: VoiceRecorder | null = null;
|
|
144
|
+
|
|
145
|
+
const showError = (message: string) => {
|
|
146
|
+
error.textContent = message;
|
|
147
|
+
error.hidden = false;
|
|
148
|
+
error.scrollIntoView({ block: "nearest" });
|
|
149
|
+
};
|
|
150
|
+
const clearError = () => {
|
|
151
|
+
error.hidden = true;
|
|
152
|
+
error.textContent = "";
|
|
153
|
+
};
|
|
154
|
+
const refusal = (code: string | null, step: ReviewStep) => say(reviewRefusalWords(code, step), { count: REVIEW_TEXT_LIMITS.bodyMin });
|
|
155
|
+
const busy = (button: HTMLButtonElement, on: boolean) => {
|
|
156
|
+
button.disabled = on;
|
|
157
|
+
button.toggleAttribute("aria-busy", on);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
document.querySelectorAll<HTMLElement>("[data-review-open]").forEach((button) => button.addEventListener("click", () => dialog.showModal()));
|
|
161
|
+
dialog.querySelectorAll<HTMLElement>("[data-review-close]").forEach((button) => button.addEventListener("click", () => dialog.close()));
|
|
162
|
+
dialog.addEventListener("click", (event) => event.target === dialog && dialog.close());
|
|
163
|
+
dialog.addEventListener("close", () => recorder?.cancel());
|
|
164
|
+
|
|
165
|
+
find.addEventListener("submit", async (event) => {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
clearError();
|
|
168
|
+
const data = new FormData(find);
|
|
169
|
+
const orderNumber = String(data.get("orderNumber") ?? "").trim();
|
|
170
|
+
const phone = String(data.get("phone") ?? "").trim();
|
|
171
|
+
if (!orderNumber || !phone) return showError(say("reviews.errorOrder"));
|
|
172
|
+
const button = $<HTMLButtonElement>("[data-review-find-button]");
|
|
173
|
+
busy(button, true);
|
|
174
|
+
const found = await session.find({ orderNumber, phone });
|
|
175
|
+
busy(button, false);
|
|
176
|
+
if (found.status === "refused") return showError(refusal(found.code, "find"));
|
|
177
|
+
if (!found.eligibility.products.some((product) => !product.reviewed)) return showError(say("reviews.nothingLeft"));
|
|
178
|
+
openWriting(found.eligibility);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
function openWriting(eligibility: ReviewEligibility) {
|
|
182
|
+
$<HTMLElement>("[data-review-order]").textContent = say("reviews.orderFound", { order: eligibility.orderNumber });
|
|
183
|
+
const list = $<HTMLElement>("[data-review-product-list]");
|
|
184
|
+
const template = $<HTMLTemplateElement>("[data-review-product-template]");
|
|
185
|
+
list.replaceChildren();
|
|
186
|
+
const preferred =
|
|
187
|
+
eligibility.products.find((product) => product.productId === root.dataset.productId && !product.reviewed) ??
|
|
188
|
+
eligibility.products.find((product) => !product.reviewed);
|
|
189
|
+
for (const product of eligibility.products) {
|
|
190
|
+
const option = template.content.firstElementChild!.cloneNode(true) as HTMLLabelElement;
|
|
191
|
+
const input = option.querySelector("input")!;
|
|
192
|
+
input.value = product.productId;
|
|
193
|
+
input.disabled = product.reviewed;
|
|
194
|
+
input.checked = product.productId === preferred?.productId;
|
|
195
|
+
option.querySelector<HTMLElement>("[data-name]")!.textContent = product.name;
|
|
196
|
+
option.querySelector<HTMLElement>("[data-done]")!.hidden = !product.reviewed;
|
|
197
|
+
list.append(option);
|
|
198
|
+
}
|
|
199
|
+
$<HTMLElement>("[data-review-products]").hidden = eligibility.products.length < 2;
|
|
200
|
+
const media = eligibility.media;
|
|
201
|
+
$<HTMLElement>("[data-review-photos-block]").hidden = !media || media.maxPhotos === 0;
|
|
202
|
+
$<HTMLElement>("[data-review-voice-block]").hidden = !media || media.voiceMaxSeconds === null;
|
|
203
|
+
if (media?.maxPhotos) $<HTMLElement>("[data-review-photos-limit]").textContent = say("reviews.photosLimit", { count: media.maxPhotos });
|
|
204
|
+
if (media?.voiceMaxSeconds) {
|
|
205
|
+
$<HTMLElement>("[data-review-voice-limit]").textContent = say("reviews.voiceLimit", { count: media.voiceMaxSeconds });
|
|
206
|
+
recordButton.hidden = !canRecordVoice();
|
|
207
|
+
}
|
|
208
|
+
find.hidden = true;
|
|
209
|
+
write.hidden = false;
|
|
210
|
+
stars[0]?.focus();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
$<HTMLButtonElement>("[data-review-change]").addEventListener("click", () => {
|
|
214
|
+
recorder?.cancel();
|
|
215
|
+
session.reset();
|
|
216
|
+
files = [];
|
|
217
|
+
$<HTMLElement>("[data-review-photo-list]").replaceChildren();
|
|
218
|
+
$<HTMLElement>("[data-review-voice-list]").replaceChildren();
|
|
219
|
+
write.hidden = true;
|
|
220
|
+
find.hidden = false;
|
|
221
|
+
clearError();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const paintStars = (value: number) => stars.forEach((star) => star.classList.toggle("is-on", Number(star.dataset.star) <= value));
|
|
225
|
+
stars.forEach((star) => {
|
|
226
|
+
star.addEventListener("click", () => {
|
|
227
|
+
rating = Number(star.dataset.star);
|
|
228
|
+
stars.forEach((other) => other.setAttribute("aria-checked", String(other === star)));
|
|
229
|
+
paintStars(rating);
|
|
230
|
+
});
|
|
231
|
+
star.addEventListener("mouseenter", () => paintStars(Number(star.dataset.star)));
|
|
232
|
+
star.addEventListener("mouseleave", () => paintStars(rating));
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const sentVoice = () => files.some((item) => item.kind === "audio" && item.state === "sent");
|
|
236
|
+
const bodyHint = () => ($<HTMLElement>("[data-review-body-hint]").hidden = !files.some((item) => item.kind === "audio"));
|
|
237
|
+
|
|
238
|
+
function show(item: Upload, state: Upload["state"], progress = 0, message?: string) {
|
|
239
|
+
item.state = state;
|
|
240
|
+
item.node.dataset.state = state;
|
|
241
|
+
item.node.querySelector<HTMLElement>("[data-status]")!.textContent = state === "failed" ? message ?? say("reviews.notSent") : state === "sent" ? "" : say("reviews.sending");
|
|
242
|
+
item.node.querySelector<HTMLElement>("[data-progress]")!.style.inlineSize = `${Math.round((state === "sent" ? 1 : progress) * 100)}%`;
|
|
243
|
+
item.node.querySelector<HTMLElement>("[data-retry]")!.hidden = state !== "failed";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function send(item: Upload) {
|
|
247
|
+
show(item, "sending");
|
|
248
|
+
const upload = session.upload(item.file, { kind: item.kind, ...(item.durationMs ? { durationMs: item.durationMs } : {}), onProgress: (share) => show(item, "sending", share) });
|
|
249
|
+
item.cancel = upload.cancel;
|
|
250
|
+
void upload.result.then((result) => {
|
|
251
|
+
if (result.status === "cancelled" || !files.includes(item)) return;
|
|
252
|
+
if (result.status === "sent") {
|
|
253
|
+
item.uploadId = result.uploadId;
|
|
254
|
+
show(item, "sent");
|
|
255
|
+
} else show(item, "failed", 0, refusal(result.code, "upload"));
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function add(kind: Upload["kind"], file: Blob, durationMs?: number) {
|
|
260
|
+
const node = fileTemplate.content.firstElementChild!.cloneNode(true) as HTMLLIElement;
|
|
261
|
+
const item: Upload = { kind, node, file, ...(durationMs ? { durationMs } : {}), uploadId: null, state: "sending" };
|
|
262
|
+
const preview = URL.createObjectURL(file);
|
|
263
|
+
const media = node.querySelector<HTMLImageElement | HTMLAudioElement>(kind === "image" ? "img" : "audio")!;
|
|
264
|
+
media.src = preview;
|
|
265
|
+
media.hidden = false;
|
|
266
|
+
node.querySelector<HTMLButtonElement>("[data-remove]")!.addEventListener("click", () => {
|
|
267
|
+
item.cancel?.();
|
|
268
|
+
URL.revokeObjectURL(preview);
|
|
269
|
+
node.remove();
|
|
270
|
+
files = files.filter((other) => other !== item);
|
|
271
|
+
bodyHint();
|
|
272
|
+
});
|
|
273
|
+
node.querySelector<HTMLButtonElement>("[data-retry]")!.addEventListener("click", () => send(item));
|
|
274
|
+
files.push(item);
|
|
275
|
+
$<HTMLElement>(kind === "image" ? "[data-review-photo-list]" : "[data-review-voice-list]").append(node);
|
|
276
|
+
bodyHint();
|
|
277
|
+
send(item);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
$<HTMLInputElement>("[data-review-photo-file]").addEventListener("change", (event) => {
|
|
281
|
+
const input = event.target as HTMLInputElement;
|
|
282
|
+
const max = session.eligibility?.media?.maxPhotos ?? 0;
|
|
283
|
+
const room = max - files.filter((item) => item.kind === "image").length;
|
|
284
|
+
const picked = Array.from(input.files ?? []).filter((file) => file.type.startsWith("image/"));
|
|
285
|
+
if (picked.length > room) showError(say("reviews.tooManyPhotos", { count: max }));
|
|
286
|
+
for (const file of picked.slice(0, Math.max(room, 0))) add("image", file);
|
|
287
|
+
input.value = "";
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const replaceVoice = (blob: Blob, durationMs?: number) => {
|
|
291
|
+
for (const item of files.filter((other) => other.kind === "audio")) item.node.querySelector<HTMLButtonElement>("[data-remove]")!.click();
|
|
292
|
+
add("audio", blob, durationMs);
|
|
293
|
+
};
|
|
294
|
+
$<HTMLInputElement>("[data-review-voice-file]").addEventListener("change", (event) => {
|
|
295
|
+
const input = event.target as HTMLInputElement;
|
|
296
|
+
const file = input.files?.[0];
|
|
297
|
+
if (file) replaceVoice(file);
|
|
298
|
+
input.value = "";
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const finishRecording = (blob: Blob, durationMs: number) => {
|
|
302
|
+
recorder = null;
|
|
303
|
+
stopButton.hidden = true;
|
|
304
|
+
recordButton.hidden = false;
|
|
305
|
+
replaceVoice(blob, durationMs);
|
|
306
|
+
};
|
|
307
|
+
recordButton.addEventListener("click", async () => {
|
|
308
|
+
const max = session.eligibility?.media?.voiceMaxSeconds;
|
|
309
|
+
if (!max) return;
|
|
310
|
+
recorder = createVoiceRecorder({
|
|
311
|
+
maxSeconds: max,
|
|
312
|
+
onTick: (elapsed) => (timer.textContent = clock(elapsed / 1000)),
|
|
313
|
+
onLimit: (voice) => finishRecording(voice.blob, voice.durationMs),
|
|
314
|
+
});
|
|
315
|
+
try {
|
|
316
|
+
await recorder.start();
|
|
317
|
+
recordButton.hidden = true;
|
|
318
|
+
stopButton.hidden = false;
|
|
319
|
+
timer.textContent = "0:00";
|
|
320
|
+
} catch {
|
|
321
|
+
recorder = null;
|
|
322
|
+
showError(say("reviews.micDenied"));
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
stopButton.addEventListener("click", async () => {
|
|
326
|
+
if (!recorder || recorder.state !== "recording") return;
|
|
327
|
+
const voice = await recorder.stop();
|
|
328
|
+
finishRecording(voice.blob, voice.durationMs);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
write.addEventListener("submit", async (event) => {
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
clearError();
|
|
334
|
+
const opened = session.eligibility;
|
|
335
|
+
if (!opened) return;
|
|
336
|
+
const productId = new FormData(write).get("productId")?.toString() ?? opened.products.find((product) => !product.reviewed)?.productId;
|
|
337
|
+
if (!productId) return showError(say("reviews.nothingLeft"));
|
|
338
|
+
if (!rating) return showError(say("reviews.ratingRequired"));
|
|
339
|
+
const body = $<HTMLTextAreaElement>("#review-body").value.trim();
|
|
340
|
+
if (body.length < REVIEW_TEXT_LIMITS.bodyMin && !sentVoice()) return showError(say("reviews.tooShort", { count: REVIEW_TEXT_LIMITS.bodyMin }));
|
|
341
|
+
if (files.some((item) => item.state !== "sent")) return showError(say("reviews.waitUploads"));
|
|
342
|
+
const button = $<HTMLButtonElement>("[data-review-submit]");
|
|
343
|
+
busy(button, true);
|
|
344
|
+
const title = $<HTMLInputElement>("#review-title").value.trim();
|
|
345
|
+
const submitted = await session.submit({
|
|
346
|
+
productId,
|
|
347
|
+
rating,
|
|
348
|
+
body,
|
|
349
|
+
...(title ? { title } : {}),
|
|
350
|
+
uploads: files.flatMap((item) => (item.uploadId ? [item.uploadId] : [])),
|
|
351
|
+
});
|
|
352
|
+
busy(button, false);
|
|
353
|
+
if (submitted.status === "refused") return showError(refusal(submitted.code, "submit"));
|
|
354
|
+
$<HTMLElement>("[data-review-success-text]").textContent = say(submitted.result.visibility === "published" ? "reviews.thanksPublished" : "reviews.thanksPending");
|
|
355
|
+
write.hidden = true;
|
|
356
|
+
success.hidden = false;
|
|
357
|
+
});
|
|
358
|
+
});
|