Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1757 lines (1625 loc) · 64.3 KB
/
Copy pathscript.js
File metadata and controls
1757 lines (1625 loc) · 64.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const categories = [
{
id: "all",
name: "All Pets",
detail: "Food, toys, care, and daily essentials",
bg: "linear-gradient(135deg, #dff6f2, #fff2cf)",
accent: "#275948",
},
{
id: "dog",
name: "Dog",
detail: "Meals, chews, leashes, and outdoor gear",
bg: "linear-gradient(135deg, #e8f3ff, #fff1e8)",
accent: "#f26b5b",
},
{
id: "cat",
name: "Cat",
detail: "Litter, scratchers, treats, and cozy beds",
bg: "linear-gradient(135deg, #f2ecff, #e9f8ec)",
accent: "#6aa84f",
},
{
id: "small",
name: "Small Pets",
detail: "Habitat care, hay, snacks, and enrichment",
bg: "linear-gradient(135deg, #fff6dc, #e5f7f6)",
accent: "#d99d20",
},
];
const products = [
{
id: "kibble",
name: "Leap & Leash Chicken Kibble",
category: "dog",
price: 28,
meta: "Chicken and sweet potato recipe, 4 lb",
description:
"A complete everyday kibble made for steady energy, healthy coats, and simple mealtime routines. The smaller 4 lb bag is easy to store and ideal for trying a new recipe before committing to a larger size.",
image: "assets/product-dog-food-photo.png",
bg: "linear-gradient(135deg, #e5f7f6, #fff1dc)",
accent: "#275948",
},
{
id: "cat-tree",
name: "Window Perch Cat Tree",
category: "cat",
price: 64,
meta: "Compact scratch tower with nap shelf",
description:
"A space-friendly cat tree with sisal posts for scratching and raised perches for watching the room. The soft surfaces make it comfortable for naps while keeping the footprint practical for apartments.",
image: "assets/product-cat-tree-photo.png",
bg: "linear-gradient(135deg, #eee7ff, #f7f3df)",
accent: "#6d62b6",
},
{
id: "rope",
name: "Tough Loop Rope Toy",
category: "dog",
price: 14,
meta: "Braided cotton with reinforced handle",
description:
"A durable rope toy designed for tug, fetch, and everyday chewing. The loop shape is easy to grip, while the braided cotton texture helps keep play sessions engaging.",
image: "assets/product-rope-photo.png",
bg: "linear-gradient(135deg, #ffe7df, #e5f7f6)",
accent: "#f26b5b",
},
{
id: "litter",
name: "Low-Track Cloud Litter",
category: "cat",
price: 22,
meta: "Unscented clumping litter, 18 lb",
description:
"An unscented clumping litter made for low dust and easier daily scooping. It is a practical choice for indoor cats and households that prefer simple odor control without added fragrance.",
image: "assets/product-litter-photo.png",
bg: "linear-gradient(135deg, #f3f7fa, #e8f3ff)",
accent: "#4f8ba8",
},
{
id: "hay",
name: "Orchard Grass Hay Box",
category: "small",
price: 18,
meta: "Hand-packed hay for rabbits and guinea pigs",
description:
"A fresh orchard grass hay box for rabbits, guinea pigs, and other small pets. The loose-packed format makes it easy to portion into habitats while supporting healthy chewing and daily enrichment.",
image: "assets/product-hay-photo.png",
bg: "linear-gradient(135deg, #edf6df, #fff6dc)",
accent: "#6aa84f",
},
{
id: "shampoo",
name: "Oat & Aloe Wash",
category: "dog",
price: 16,
meta: "Gentle shampoo for sensitive coats",
description:
"A gentle wash for dogs with sensitive skin and everyday coat care needs. Oat and aloe ingredients help the bath feel soothing while leaving the coat clean without a heavy scent.",
image: "assets/product-shampoo-photo.png",
bg: "linear-gradient(135deg, #e5f7f6, #fff)",
accent: "#275948",
},
{
id: "treats",
name: "Tuna Snap Treats",
category: "cat",
price: 9,
meta: "Protein-rich bite-size rewards",
description:
"Small tuna treats made for quick rewards, training moments, or a little between-meal excitement. The bite-size pieces are easy to portion and keep fresh in the resealable pouch.",
image: "assets/product-treats-photo.png",
bg: "linear-gradient(135deg, #fff1dc, #e8f3ff)",
accent: "#d99d20",
},
{
id: "hideout",
name: "Willow Nest Hideout",
category: "small",
price: 24,
meta: "Chew-safe hideaway for small companions",
description:
"A woven willow hideout that gives small pets a cozy place to rest, explore, and nibble. The open arch makes it easy to place in a habitat while keeping the look natural and tidy.",
image: "assets/product-hideout-photo.png",
bg: "linear-gradient(135deg, #f7f3df, #edf6df)",
accent: "#a37138",
},
];
const productGrid = document.querySelector("#productGrid");
const heroSection = document.querySelector("#heroSection");
const storefrontView = document.querySelector("#storefrontView");
const faqPage = document.querySelector("#faqPage");
const returnsPage = document.querySelector("#returnsPage");
const returnForm = document.querySelector("#returnForm");
const returnOrderNumber = document.querySelector("#returnOrderNumber");
const returnMessage = document.querySelector("#returnMessage");
const returnDetails = document.querySelector("#returnDetails");
const categoryFilter = document.querySelector("#categoryFilter");
const sortSelect = document.querySelector("#sortSelect");
const searchInput = document.querySelector("#searchInput");
const productEyebrow = document.querySelector("#productEyebrow");
const cartButton = document.querySelector("#cartButton");
const closeCart = document.querySelector("#closeCart");
const cartDrawer = document.querySelector("#cartDrawer");
const cartItems = document.querySelector("#cartItems");
const cartCount = document.querySelector("#cartCount");
const cartTotal = document.querySelector("#cartTotal");
const accountButton = document.querySelector("#accountButton");
const accountMenu = document.querySelector("#accountMenu");
const accountPage = document.querySelector("#accountPage");
const accountTitle = document.querySelector("#accountTitle");
const accountSidebar = document.querySelector("#accountSidebar");
const accountContent = document.querySelector("#accountContent");
const accountBackButton = document.querySelector("#accountBackButton");
const logoutButton = document.querySelector("#logoutButton");
const authModal = document.querySelector("#authModal");
const closeAuth = document.querySelector("#closeAuth");
const signInTab = document.querySelector("#signInTab");
const registerTab = document.querySelector("#registerTab");
const authForm = document.querySelector("#authForm");
const authNameField = document.querySelector("#authNameField");
const authName = document.querySelector("#authName");
const authEmail = document.querySelector("#authEmail");
const authPassword = document.querySelector("#authPassword");
const authMessage = document.querySelector("#authMessage");
const authSubmit = document.querySelector("#authSubmit");
const productModal = document.querySelector("#productModal");
const closeProductModal = document.querySelector("#closeProductModal");
const productDialogContent = document.querySelector("#productDialogContent");
const checkoutButton = document.querySelector("#checkoutButton");
const checkoutModal = document.querySelector("#checkoutModal");
const closeCheckout = document.querySelector("#closeCheckout");
const checkoutForm = document.querySelector("#checkoutForm");
const addressStep = document.querySelector("#addressStep");
const paymentStep = document.querySelector("#paymentStep");
const reviewStep = document.querySelector("#reviewStep");
const continueToPayment = document.querySelector("#continueToPayment");
const backToAddress = document.querySelector("#backToAddress");
const continueToReview = document.querySelector("#continueToReview");
const backToPayment = document.querySelector("#backToPayment");
const checkoutReviewSummary = document.querySelector("#checkoutReviewSummary");
const checkoutMessage = document.querySelector("#checkoutMessage");
const checkoutTitle = document.querySelector("#checkoutTitle");
const checkoutSuccess = document.querySelector("#checkoutSuccess");
const checkoutSummaryCard = document.querySelector(".checkout-summary-card");
const checkoutSummaryItems = document.querySelector("#checkoutSummaryItems");
const checkoutSummaryTotals = document.querySelector("#checkoutSummaryTotals");
const successSummary = document.querySelector("#successSummary");
const finishOrder = document.querySelector("#finishOrder");
const customerName = document.querySelector("#customerName");
const customerEmail = document.querySelector("#customerEmail");
const savedAddressField = document.querySelector("#savedAddressField");
const savedAddressSelect = document.querySelector("#savedAddressSelect");
const selectedAddressDisplay = document.querySelector("#selectedAddressDisplay");
const addressFields = document.querySelector("#addressFields");
const customerStreet = document.querySelector("#customerStreet");
const customerZip = document.querySelector("#customerZip");
const customerCity = document.querySelector("#customerCity");
const customerCountry = document.querySelector("#customerCountry");
const addressSuggestions = document.querySelector("#addressSuggestions");
const dateField = document.querySelector(".date-field");
const deliveryDate = document.querySelector("#deliveryDate");
const autocompleteAddresses = [
{
street: "1187 Willow Bark Avenue",
zip: "98103",
city: "Seattle",
country: "United States",
},
{
street: "42 Parkside Mews",
zip: "10024",
city: "New York",
country: "United States",
},
{
street: "7308 Sunnybrook Drive",
zip: "60614",
city: "Chicago",
country: "United States",
},
{
street: "15 Laurel Green",
zip: "SW1A 1AA",
city: "London",
country: "United Kingdom",
},
{
street: "8 Kottbusser Damm",
zip: "10967",
city: "Berlin",
country: "Germany",
},
];
const FREE_SHIPPING_THRESHOLD = 50;
const STANDARD_SHIPPING = 4.99;
const demoUsers = [
{
name: "Emma Carter",
label: "New Customer",
email: "emma.carter@example.com",
password: "Welcome123!",
discount: 0,
addresses: [
{
name: "Emma Carter",
street: "1840 Linden Street",
zip: "97205",
city: "Portland",
country: "United States",
},
],
orders: [
{ number: "TT-184215", date: "Apr 18", total: 42.0, status: "Delivered" },
{ number: "TT-173902", date: "Mar 29", total: 28.0, status: "Delivered" },
],
},
{
name: "Daniel Hughes",
label: "VIP Customer",
email: "daniel.hughes@example.com",
password: "Loyalty123!",
discount: 0.1,
addresses: [
{
name: "Daniel Hughes",
street: "905 Maple Ridge Lane",
zip: "78703",
city: "Austin",
country: "United States",
},
{
name: "Daniel Hughes",
street: "22 Harbor View Apt 4B",
zip: "33602",
city: "Tampa",
country: "United States",
},
],
orders: [
{ number: "TT-215884", date: "Apr 26", total: 77.39, status: "Out for delivery" },
{ number: "TT-203641", date: "Apr 03", total: 51.3, status: "Delivered" },
],
},
{
name: "Olivia Martin",
label: "Admin",
email: "olivia.admin@example.com",
password: "Admin123!",
role: "admin",
discount: 0,
addresses: [],
orders: [],
},
];
const additionalDemoOrders = [
{ number: "TT-590768", date: "May 06", customer: "Maya Brooks", email: "maya.brooks@example.com", address: "44 Cedar Point Road, 02118 Boston, United States", status: "Processing", payment: "Demo card ending in 4242", total: 68.47 },
{ number: "TT-589412", date: "May 05", customer: "Noah Patel", email: "noah.patel@example.com", address: "718 West Olive Street, 94110 San Francisco, United States", status: "Packed", payment: "PayPal demo", total: 34.98 },
{ number: "TT-588903", date: "May 04", customer: "Avery Kim", email: "avery.kim@example.com", address: "1299 Lakeview Drive, 60614 Chicago, United States", status: "Out for delivery", payment: "Demo card ending in 4242", total: 91.25 },
{ number: "TT-587644", date: "May 03", customer: "Lucas Bennett", email: "lucas.bennett@example.com", address: "605 Pine Hollow Lane, 98109 Seattle, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 22.49 },
{ number: "TT-586732", date: "May 02", customer: "Sofia Alvarez", email: "sofia.alvarez@example.com", address: "882 Gardenia Court, 33133 Miami, United States", status: "Processing", payment: "PayPal demo", total: 57.92 },
{ number: "TT-585219", date: "May 01", customer: "Ethan Wright", email: "ethan.wright@example.com", address: "230 North Birch Avenue, 80203 Denver, United States", status: "Cancelled", payment: "Demo card ending in 4242", total: 19.99 },
{ number: "TT-584801", date: "Apr 30", customer: "Harper Scott", email: "harper.scott@example.com", address: "54 Magnolia Place, 37203 Nashville, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 73.16 },
{ number: "TT-583672", date: "Apr 29", customer: "Liam Morgan", email: "liam.morgan@example.com", address: "4019 Ridgecrest Way, 85004 Phoenix, United States", status: "Packed", payment: "PayPal demo", total: 46.38 },
{ number: "TT-582445", date: "Apr 28", customer: "Chloe Turner", email: "chloe.turner@example.com", address: "903 Meadow Brook Road, 55402 Minneapolis, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 115.7 },
{ number: "TT-581936", date: "Apr 27", customer: "Mason Reed", email: "mason.reed@example.com", address: "77 South Elm Street, 28202 Charlotte, United States", status: "Out for delivery", payment: "Demo card ending in 4242", total: 39.45 },
{ number: "TT-580744", date: "Apr 26", customer: "Isabella Young", email: "isabella.young@example.com", address: "1420 Summit Avenue, 97209 Portland, United States", status: "Delivered", payment: "PayPal demo", total: 84.63 },
{ number: "TT-579508", date: "Apr 25", customer: "Jackson Hall", email: "jackson.hall@example.com", address: "510 Bayberry Lane, 19103 Philadelphia, United States", status: "Processing", payment: "Demo card ending in 4242", total: 27.5 },
{ number: "TT-578419", date: "Apr 24", customer: "Grace Nelson", email: "grace.nelson@example.com", address: "2815 Orange Grove Drive, 90039 Los Angeles, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 62.84 },
{ number: "TT-577236", date: "Apr 23", customer: "Henry Carter", email: "henry.carter@example.com", address: "16 Franklin Terrace, 20002 Washington, United States", status: "Packed", payment: "PayPal demo", total: 49.77 },
{ number: "TT-576128", date: "Apr 22", customer: "Ella Foster", email: "ella.foster@example.com", address: "900 Riverbend Circle, 63108 St. Louis, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 31.2 },
{ number: "TT-575902", date: "Apr 21", customer: "Benjamin Clark", email: "benjamin.clark@example.com", address: "707 Spruce Street, 15222 Pittsburgh, United States", status: "Cancelled", payment: "Demo card ending in 4242", total: 96.18 },
{ number: "TT-574613", date: "Apr 20", customer: "Mila Torres", email: "mila.torres@example.com", address: "118 Crescent Lane, 30308 Atlanta, United States", status: "Delivered", payment: "PayPal demo", total: 55.04 },
{ number: "TT-573884", date: "Apr 19", customer: "Logan Rivera", email: "logan.rivera@example.com", address: "635 Highland Park Road, 46204 Indianapolis, United States", status: "Out for delivery", payment: "Demo card ending in 4242", total: 71.33 },
{ number: "TT-572491", date: "Apr 18", customer: "Nora Evans", email: "nora.evans@example.com", address: "49 Pearl Street, 10013 New York, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 43.28 },
{ number: "TT-571620", date: "Apr 17", customer: "Owen Murphy", email: "owen.murphy@example.com", address: "322 Oak Knoll Avenue, 78704 Austin, United States", status: "Processing", payment: "PayPal demo", total: 88.11 },
{ number: "TT-570537", date: "Apr 16", customer: "Amelia Cox", email: "amelia.cox@example.com", address: "1001 Brookside Drive, 64108 Kansas City, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 25.99 },
{ number: "TT-569408", date: "Apr 15", customer: "Sebastian Price", email: "sebastian.price@example.com", address: "217 Willow Bend Road, 70115 New Orleans, United States", status: "Packed", payment: "Demo card ending in 4242", total: 64.72 },
{ number: "TT-568276", date: "Apr 14", customer: "Lily Sanders", email: "lily.sanders@example.com", address: "870 Market Lane, 92103 San Diego, United States", status: "Delivered", payment: "PayPal demo", total: 103.46 },
{ number: "TT-567135", date: "Apr 13", customer: "Caleb Morris", email: "caleb.morris@example.com", address: "39 Aspen Grove, 43215 Columbus, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 38.69 },
{ number: "TT-566804", date: "Apr 12", customer: "Zoe Peterson", email: "zoe.peterson@example.com", address: "420 Sycamore Lane, 29403 Charleston, United States", status: "Processing", payment: "Demo card ending in 4242", total: 76.9 },
{ number: "TT-565771", date: "Apr 11", customer: "Julian Gray", email: "julian.gray@example.com", address: "1516 Forest Avenue, 97214 Portland, United States", status: "Delivered", payment: "PayPal demo", total: 52.1 },
{ number: "TT-564660", date: "Apr 10", customer: "Victoria James", email: "victoria.james@example.com", address: "720 East Maple Street, 60607 Chicago, United States", status: "Out for delivery", payment: "Demo card ending in 4242", total: 29.95 },
{ number: "TT-563548", date: "Apr 09", customer: "Isaac Wood", email: "isaac.wood@example.com", address: "905 Parkview Court, 32801 Orlando, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 47.64 },
{ number: "TT-562337", date: "Apr 08", customer: "Ruby Coleman", email: "ruby.coleman@example.com", address: "63 Beacon Hill Road, 02108 Boston, United States", status: "Packed", payment: "PayPal demo", total: 66.52 },
{ number: "TT-561204", date: "Apr 07", customer: "Miles Henderson", email: "miles.henderson@example.com", address: "2400 Sunset Boulevard, 90026 Los Angeles, United States", status: "Delivered", payment: "Demo card ending in 4242", total: 93.07 },
];
let storeOrders = demoUsers
.filter((user) => user.role !== "admin")
.flatMap((user) =>
(user.orders || []).map((order, index) => ({
...order,
customer: user.name,
email: user.email,
status: order.status || (index === 0 ? "Processing" : "Delivered"),
payment: user.discount ? "Demo card ending in 4242" : "PayPal demo",
channel: "Demo store",
address: formatAddress(user.addresses?.[0] || createEmptyAddress()),
})),
)
.concat(additionalDemoOrders);
let activeCategory = "all";
let cart = {};
let currentUser = null;
let orderHistory = [];
let savedAddresses = [];
let activeAccountView = "profile";
let adminOrderSearchQuery = "";
let adminStatusFilter = "all";
let adminOrderPage = 1;
let authMode = "signin";
let checkoutDetails = {
name: "",
email: "",
address: {
name: "",
street: "",
zip: "",
city: "",
country: "",
},
payment: "Demo card ending in 4242",
deliveryDate: "",
};
let activeCheckoutPanel = "address";
function currency(value) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(value);
}
function escapeHtml(value) {
return String(value ?? "").replace(/[&<>"']/g, (char) => {
const entities = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
return entities[char];
});
}
function createEmptyAddress() {
return {
name: "",
street: "",
zip: "",
city: "",
country: "",
};
}
function normalizeAddress(address = {}) {
if (typeof address === "string") {
return {
name: "",
street: address,
zip: "",
city: "",
country: "",
};
}
return {
name: String(address.name ?? ""),
street: String(address.street ?? ""),
zip: String(address.zip ?? ""),
city: String(address.city ?? ""),
country: String(address.country ?? ""),
};
}
function addressKey(address) {
const normalized = normalizeAddress(address);
return [normalized.name, normalized.street, normalized.zip, normalized.city, normalized.country]
.map((part) => part.trim().toLowerCase())
.join("|");
}
function isCompleteAddress(address) {
const normalized = normalizeAddress(address);
return Boolean(
normalized.name.trim() &&
normalized.street.trim() &&
normalized.zip.trim() &&
normalized.city.trim() &&
normalized.country.trim(),
);
}
function formatAddress(address, options = {}) {
const normalized = normalizeAddress(address);
const lines = [normalized.street, `${normalized.zip} ${normalized.city}`.trim(), normalized.country].filter(Boolean);
if (options.includeName && normalized.name) lines.unshift(normalized.name);
return lines
.filter(Boolean)
.join(", ");
}
function getAddressFromFields() {
return {
name: customerName.value.trim(),
street: customerStreet.value.trim(),
zip: customerZip.value.trim(),
city: customerCity.value.trim(),
country: customerCountry.value.trim(),
};
}
function fillAddressFields(address) {
const normalized = normalizeAddress(address);
if (normalized.name) customerName.value = normalized.name;
customerStreet.value = normalized.street;
customerZip.value = normalized.zip;
customerCity.value = normalized.city;
customerCountry.value = normalized.country;
}
function formatDateInput(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
function parseDemoOrderDate(value) {
const date = new Date(`${value}, 2026 00:00:00`);
return Number.isNaN(date.getTime()) ? null : date;
}
function formatDeliveryDate(value) {
if (!value) return "No delivery date selected";
const date = new Date(`${value}T00:00:00`);
return new Intl.DateTimeFormat("en-US", {
weekday: "short",
month: "short",
day: "numeric",
year: "numeric",
}).format(date);
}
function configureDeliveryDate() {
const min = new Date();
min.setDate(min.getDate() + 3);
const max = new Date();
max.setFullYear(max.getFullYear() + 1);
deliveryDate.min = formatDateInput(min);
deliveryDate.max = formatDateInput(max);
if (!deliveryDate.value || deliveryDate.value < deliveryDate.min || deliveryDate.value > deliveryDate.max) {
deliveryDate.value = deliveryDate.min;
}
}
function openDeliveryDatePicker() {
deliveryDate.focus({ preventScroll: true });
if (typeof deliveryDate.showPicker !== "function") return;
try {
deliveryDate.showPicker();
} catch (error) {
// Browsers may block showPicker unless it comes directly from a user action.
}
}
function setAddressFieldsVisible(isVisible) {
addressFields.hidden = !isVisible;
[customerStreet, customerZip, customerCity, customerCountry].forEach((field) => {
field.disabled = !isVisible;
});
if (!isVisible) hideAddressSuggestions();
}
function renderSelectedAddressDisplay(address) {
const normalized = normalizeAddress(address);
selectedAddressDisplay.hidden = false;
selectedAddressDisplay.innerHTML = `
<p>${escapeHtml(normalized.street)}</p>
<p>${escapeHtml(`${normalized.zip} ${normalized.city}`.trim())}</p>
<p>${escapeHtml(normalized.country)}</p>
`;
}
function useSavedAddress(index) {
const address = normalizeAddress(savedAddresses[index]);
if (!address.street) return;
customerName.value = address.name || customerName.value || currentUser?.name || "";
fillAddressFields(address);
renderSelectedAddressDisplay(address);
setAddressFieldsVisible(false);
}
function useNewAddress() {
selectedAddressDisplay.hidden = true;
selectedAddressDisplay.innerHTML = "";
fillAddressFields({
...createEmptyAddress(),
name: customerName.value || currentUser?.name || "",
});
setAddressFieldsVisible(true);
}
function hideAddressSuggestions() {
addressSuggestions.hidden = true;
addressSuggestions.innerHTML = "";
customerStreet.setAttribute("aria-expanded", "false");
}
function getAddressSuggestionMatches() {
if (addressFields.hidden) return [];
const query = customerStreet.value.trim().toLowerCase();
if (!query) return [];
const matches = autocompleteAddresses.filter((address) =>
`${address.street} ${address.zip} ${address.city} ${address.country}`.toLowerCase().includes(query),
);
return matches.length ? matches : autocompleteAddresses;
}
function renderAddressSuggestions() {
const matches = getAddressSuggestionMatches();
if (!matches.length) {
hideAddressSuggestions();
return;
}
addressSuggestions.innerHTML = matches
.map(
(address, index) => `
<button type="button" role="option" data-address-suggestion="${index}">
<span>${escapeHtml(address.street)}</span>
<small>${escapeHtml(`${address.zip} ${address.city}, ${address.country}`)}</small>
</button>
`,
)
.join("");
addressSuggestions.hidden = false;
customerStreet.setAttribute("aria-expanded", "true");
}
function selectAddressSuggestion(index) {
const matches = getAddressSuggestionMatches();
const address = matches[index];
if (!address) return;
if (!savedAddressField.hidden) savedAddressSelect.value = "new";
fillAddressFields(address);
hideAddressSuggestions();
customerZip.focus();
}
function getCartEntries() {
return Object.entries(cart)
.map(([id, qty]) => {
const product = products.find((item) => item.id === id);
return product ? { ...product, qty, lineTotal: product.price * qty } : null;
})
.filter(Boolean);
}
function getOrderTotals() {
const entries = getCartEntries();
const subtotal = entries.reduce((sum, item) => sum + item.lineTotal, 0);
const discount = currentUser?.discount ? subtotal * currentUser.discount : 0;
const shippingBase = subtotal - discount;
const shipping = shippingBase === 0 || shippingBase >= FREE_SHIPPING_THRESHOLD ? 0 : STANDARD_SHIPPING;
return {
entries,
subtotal,
discount,
shippingBase,
shipping,
total: subtotal - discount + shipping,
};
}
function getFilteredProducts() {
const query = searchInput.value.trim().toLowerCase();
const filtered = products.filter((product) => {
const categoryMatch = activeCategory === "all" || product.category === activeCategory;
const searchMatch = `${product.name} ${product.meta} ${product.category}`.toLowerCase().includes(query);
return categoryMatch && searchMatch;
});
return filtered.sort((a, b) => {
if (sortSelect.value === "price-low") return a.price - b.price;
if (sortSelect.value === "price-high") return b.price - a.price;
return products.indexOf(a) - products.indexOf(b);
});
}
function renderProducts() {
const visibleProducts = getFilteredProducts();
const categoryName = categories.find((category) => category.id === activeCategory)?.name ?? "All Pets";
productEyebrow.textContent = activeCategory === "all" ? "Shop" : `${categoryName} products`;
productGrid.innerHTML =
visibleProducts
.map(
(product) => {
const qty = cart[product.id] || 0;
const productActionAttributes = qty
? ""
: `data-view-product="${product.id}" role="button" aria-haspopup="dialog" aria-controls="productModal" aria-label="View details for ${escapeHtml(product.name)}, ${currency(product.price)}" tabindex="0"`;
return `
<article class="product-card ${qty ? "in-cart" : ""}" ${productActionAttributes}>
<div class="product-art ${product.image ? "has-image" : ""}" style="--product-bg: ${product.bg}; --category-accent: ${product.accent}">
${product.image ? `<img src="${product.image}" alt="${product.name}" loading="lazy" />` : ""}
</div>
<div>
<h3>${product.name}</h3>
<p class="product-meta">${product.meta}</p>
</div>
<div class="product-bottom">
<span class="price">${currency(product.price)}</span>
${
qty
? `<div class="qty-control product-qty" aria-label="${product.name} quantity">
<button type="button" data-decrease="${product.id}">-</button>
<strong>${qty}</strong>
<button type="button" data-increase="${product.id}">+</button>
</div>`
: ""
}
</div>
</article>
`;
},
)
.join("") || `<p class="empty-state">No products match that search.</p>`;
}
function openProductModal(productId) {
const product = products.find((item) => item.id === productId);
if (!product) return;
productDialogContent.innerHTML = `
<div class="product-dialog-grid">
<div class="product-dialog-art">
${product.image ? `<img src="${product.image}" alt="${product.name}" />` : ""}
</div>
<div class="product-dialog-info">
<h2 id="productDialogTitle">${product.name}</h2>
<p>${product.description || product.meta}</p>
<strong class="dialog-price">${currency(product.price)}</strong>
<button class="button primary" type="button" data-product="${product.id}">Add to cart</button>
</div>
</div>
`;
productModal.classList.add("open");
productModal.setAttribute("aria-hidden", "false");
closeCartDrawer();
}
function closeProductDetails() {
productModal.classList.remove("open");
productModal.setAttribute("aria-hidden", "true");
}
function renderCart() {
const { entries, subtotal, discount, shippingBase } = getOrderTotals();
const itemCount = entries.reduce((sum, item) => sum + item.qty, 0);
cartCount.textContent = itemCount;
cartCount.classList.toggle("is-empty", itemCount === 0);
cartTotal.innerHTML = `${renderFreeShippingHint(shippingBase, entries)}${renderPriceBreakdown(subtotal, discount)}`;
checkoutButton.disabled = itemCount === 0;
cartItems.innerHTML =
entries
.map(
(product) => `
<div class="cart-row">
<div>
<strong>${product.name}</strong>
<span>${currency(product.price)} each</span>
</div>
<div class="qty-control" aria-label="${product.name} quantity">
<button type="button" data-decrease="${product.id}">-</button>
<strong>${product.qty}</strong>
<button type="button" data-increase="${product.id}">+</button>
</div>
</div>
`,
)
.join("") || `<p class="empty-state">Your cart is ready for treats, toys, and care picks.</p>`;
if (!checkoutModal.hidden) renderCheckoutSummary();
}
function renderPriceBreakdown(subtotal, discount) {
if (!discount) {
return `
<div class="total-row"><span>Subtotal</span><strong>${currency(subtotal)}</strong></div>
`;
}
return `
<div class="total-row original-total"><span>Original amount</span><strong>${currency(subtotal)}</strong></div>
<div class="total-row discount-row"><span>VIP discount</span><strong>-${currency(discount)}</strong></div>
`;
}
function renderFreeShippingHint(shippingBase, entries) {
if (!entries.length || shippingBase <= 0 || shippingBase >= FREE_SHIPPING_THRESHOLD) return "";
return `<p class="shipping-hint">Add ${currency(FREE_SHIPPING_THRESHOLD - shippingBase)} more for free shipping.</p>`;
}
function renderOrderSummary(target) {
const { entries, subtotal, discount, shipping, total } = getOrderTotals();
target.innerHTML = `
${renderPriceBreakdown(subtotal, discount)}
<div class="total-row"><span>Shipping</span><strong>${shipping === 0 ? "Free" : currency(shipping)}</strong></div>
<div class="total-row grand-total"><span>Total</span><strong>${currency(total)}</strong></div>
`;
}
function renderCheckoutSummary() {
const { entries } = getOrderTotals();
checkoutSummaryItems.innerHTML = entries
.map(
(item) => `
<div class="review-item">
<span>${item.qty} x ${item.name}</span>
<strong>${currency(item.lineTotal)}</strong>
</div>
`,
)
.join("");
renderOrderSummary(checkoutSummaryTotals);
}
function getSelectedPaymentMethod() {
return document.querySelector('input[name="paymentMethod"]:checked')?.value || "Demo card ending in 4242";
}
function getCheckoutAddress() {
const name = customerName.value.trim();
const usingSavedAddress = !savedAddressField.hidden && savedAddressSelect.value !== "new";
return usingSavedAddress
? {
...normalizeAddress(savedAddresses[Number(savedAddressSelect.value)]),
name,
}
: getAddressFromFields();
}
function validateAddressStep() {
const name = customerName.value.trim();
const email = customerEmail.value.trim();
const address = getCheckoutAddress();
if (!name || !email || !isCompleteAddress(address)) {
checkoutMessage.textContent =
"Please enter the recipient name, email address, street address, ZIP code, city, and country.";
if (!name) customerName.focus();
else if (!email) customerEmail.focus();
else if (!address.street) customerStreet.focus();
else if (!address.zip) customerZip.focus();
else if (!address.city) customerCity.focus();
else customerCountry.focus();
return false;
}
checkoutMessage.textContent = "";
return true;
}
function validatePaymentStep() {
configureDeliveryDate();
if (!deliveryDate.value || deliveryDate.value < deliveryDate.min || deliveryDate.value > deliveryDate.max) {
checkoutMessage.textContent = "Please choose a preferred delivery date within the available range.";
deliveryDate.focus();
return false;
}
checkoutMessage.textContent = "";
return true;
}
function syncCheckoutDetails() {
checkoutDetails = {
name: customerName.value.trim(),
email: customerEmail.value.trim(),
address: getCheckoutAddress(),
payment: getSelectedPaymentMethod(),
deliveryDate: deliveryDate.value,
};
}
function renderCheckoutReviewSummary() {
syncCheckoutDetails();
checkoutReviewSummary.innerHTML = `
<section class="review-detail-section" aria-label="Delivery details">
<h3>Delivery</h3>
<p>${escapeHtml(checkoutDetails.name)}</p>
<p>${escapeHtml(checkoutDetails.email)}</p>
<p>${escapeHtml(formatAddress(checkoutDetails.address))}</p>
<p>${escapeHtml(formatDeliveryDate(checkoutDetails.deliveryDate))}</p>
</section>
<section class="review-detail-section" aria-label="Payment details">
<h3>Payment</h3>
<p>${escapeHtml(checkoutDetails.payment)}</p>
</section>
`;
}
function showCheckoutPanel(panel) {
activeCheckoutPanel = panel;
addressStep.hidden = panel !== "address";
paymentStep.hidden = panel !== "payment";
reviewStep.hidden = panel !== "review";
checkoutMessage.textContent = "";
document.querySelectorAll("[data-step-indicator]").forEach((indicator) => {
indicator.classList.toggle("active", indicator.dataset.stepIndicator === panel);
});
if (panel === "review") renderCheckoutReviewSummary();
}
function showCheckoutStep(step) {
checkoutModal.dataset.step = step;
checkoutForm.hidden = step !== "details";
checkoutSuccess.hidden = step !== "success";
checkoutSummaryCard.hidden = step === "success";
finishOrder.hidden = step !== "success";
if (step === "details") checkoutMessage.textContent = "";
if (step === "details") {
checkoutTitle.textContent = "Checkout";
showCheckoutPanel(activeCheckoutPanel || "address");
}
renderCheckoutSummary();
}
function openCheckout() {
if (getOrderTotals().entries.length === 0) return;
if (currentUser) {
customerName.value = savedAddresses[0]?.name || currentUser.name;
customerEmail.value = currentUser.email;
customerName.disabled = false;
customerEmail.disabled = true;
renderCheckoutAddresses();
} else {
customerName.disabled = false;
customerEmail.disabled = false;
savedAddressField.hidden = true;
selectedAddressDisplay.hidden = true;
setAddressFieldsVisible(true);
}
storefrontView.hidden = true;
heroSection.hidden = true;
accountPage.hidden = true;
faqPage.hidden = true;
returnsPage.hidden = true;
checkoutModal.hidden = false;
checkoutModal.setAttribute("aria-hidden", "false");
closeCartDrawer();
activeCheckoutPanel = "address";
configureDeliveryDate();
showCheckoutStep("details");
checkoutModal.scrollIntoView({ behavior: "smooth", block: "start" });
customerName.focus({ preventScroll: true });
}
function renderCheckoutAddresses() {
if (!currentUser || savedAddresses.length === 0) {
savedAddressField.hidden = true;
selectedAddressDisplay.hidden = true;
useNewAddress();
return;
}
savedAddressField.hidden = false;
savedAddressSelect.innerHTML = `
${savedAddresses
.map((address, index) => `<option value="${index}">${escapeHtml(formatAddress(address, { includeName: true }))}</option>`)
.join("")}
<option value="new">Enter a new address</option>
`;
savedAddressSelect.value = "0";
useSavedAddress(0);
}
function closeCheckoutModal() {
checkoutModal.hidden = true;
checkoutModal.setAttribute("aria-hidden", "true");
checkoutModal.dataset.step = "";
hideAddressSuggestions();
heroSection.hidden = false;
storefrontView.hidden = false;
customerName.disabled = false;
customerEmail.disabled = false;
storefrontView.scrollIntoView({ behavior: "smooth", block: "start" });
}
function renderSuccess() {
const orderNumber = `TT-${Math.floor(100000 + Math.random() * 900000)}`;
const { entries, subtotal, discount, shipping, total } = getOrderTotals();
const name = escapeHtml(checkoutDetails.name) || "Guest customer";
const email = escapeHtml(checkoutDetails.email) || "No email entered";
const address = escapeHtml(formatAddress(checkoutDetails.address)) || "No address entered";
const payment = escapeHtml(checkoutDetails.payment);
const preferredDate = escapeHtml(formatDeliveryDate(checkoutDetails.deliveryDate));
successSummary.innerHTML = `
<div class="confirmation-meta">
<div>
<span>Order number</span>
<strong>${orderNumber}</strong>
</div>
<div>
<span>Est. delivery date</span>
<strong>${preferredDate}</strong>
</div>
</div>
<div class="confirmation-details">
<div>
<span>Email</span>
<p>${email}</p>
</div>
<div>
<span>Delivery address</span>
<p>${name}<br />${address}</p>
</div>
<div>
<span>Payment</span>
<p>${payment}</p>
</div>
</div>
<div class="success-items">
${entries
.map(
(item) => `
<div class="review-item">
<span>${item.qty} x ${item.name}</span>
<strong>${currency(item.lineTotal)}</strong>
</div>
`,
)
.join("")}
</div>
${renderPriceBreakdown(subtotal, discount)}
<div class="total-row"><span>Shipping</span><strong>${shipping === 0 ? "Free" : currency(shipping)}</strong></div>
<div class="total-row grand-total"><span>Total paid</span><strong>${currency(total)}</strong></div>
`;
storeOrders = [
{
number: orderNumber,