@jay-framework/wix-bookings 0.26.0 → 0.26.3
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/agent-kit/designer/wix-bookings-dashboard-content.md +1 -1
- package/agent-kit/plugin/wix-bookings-setup.md +33 -5
- package/agent-kit/plugin/wix-bookings-troubleshooting.md +29 -3
- package/dist/{index.client-gTd3Cezm.d.ts → index.client-oaMPI34g.d.ts} +6 -3
- package/dist/index.client.d.ts +1 -1
- package/dist/index.client.js +397 -76
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -135
- package/dist/query-services-B1MS77NZ.js +176 -0
- package/dist/tools.d.ts +4 -2
- package/dist/tools.js +150 -23
- package/package.json +14 -14
- package/plugin.yaml +2 -0
- package/dist/config-loader-sfgwfXPV.js +0 -27
package/dist/index.client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { makeJayInit, makeJayStackComponent } from "@jay-framework/fullstack-component";
|
|
2
2
|
import { createJayContext, useGlobalContext, registerGlobalContext } from "@jay-framework/runtime";
|
|
3
3
|
import { wixFetch, WixApiError, WIX_CLIENT_CONTEXT, ensureSessionFresh } from "@jay-framework/wix-server-client/client";
|
|
4
|
-
import { createSignal,
|
|
4
|
+
import { registerReactiveGlobalContext, createSignal, useReactive } from "@jay-framework/component";
|
|
5
5
|
import { createActionCaller } from "@jay-framework/stack-client-runtime";
|
|
6
6
|
const MAX_SLOTS_PER_QUERY = 200;
|
|
7
7
|
const SERVICES_PAGE_SIZE = 100;
|
|
@@ -45,6 +45,11 @@ async function getBookingServiceById(client, serviceId) {
|
|
|
45
45
|
});
|
|
46
46
|
return result.services?.[0];
|
|
47
47
|
}
|
|
48
|
+
const DEFAULT_FORM_ID = "00000000-0000-0000-0000-000000000000";
|
|
49
|
+
function participantFormId(form) {
|
|
50
|
+
const id = form?.id ?? form?._id ?? "";
|
|
51
|
+
return id === DEFAULT_FORM_ID ? "" : id;
|
|
52
|
+
}
|
|
48
53
|
function formatPrice(service) {
|
|
49
54
|
const price = service.payment?.fixed?.price;
|
|
50
55
|
if (!price?.value) {
|
|
@@ -69,7 +74,7 @@ function mapService(raw) {
|
|
|
69
74
|
description: raw.description ?? "",
|
|
70
75
|
priceLabel: formatPrice(raw),
|
|
71
76
|
durationLabel: duration,
|
|
72
|
-
formId: raw.form
|
|
77
|
+
formId: participantFormId(raw.form)
|
|
73
78
|
};
|
|
74
79
|
}
|
|
75
80
|
function createSlotId(resourceId, localStartDate) {
|
|
@@ -503,11 +508,87 @@ var BookingFailure = /* @__PURE__ */ ((BookingFailure2) => {
|
|
|
503
508
|
const listServices = createActionCaller("wixBookings.listServices", "GET");
|
|
504
509
|
const listSlots = createActionCaller("wixBookings.listSlots", "GET");
|
|
505
510
|
const createBooking = createActionCaller("wixBookings.createBooking", "POST");
|
|
511
|
+
const GUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
512
|
+
function canonicalFormId(raw) {
|
|
513
|
+
if (typeof raw !== "string") return void 0;
|
|
514
|
+
const trimmed = raw.trim();
|
|
515
|
+
return GUID.test(trimmed) ? trimmed.toLowerCase() : void 0;
|
|
516
|
+
}
|
|
517
|
+
const WIX_FORMS_CONTEXT = createJayContext("wix:forms");
|
|
518
|
+
const REGISTERED_CHECKS = Symbol("wix-forms.registeredFieldChecks");
|
|
519
|
+
const EMIT_SUBMIT_OUTCOME = Symbol("wix-forms.emitSubmitOutcome");
|
|
520
|
+
function provideWixFormsContext() {
|
|
521
|
+
return registerReactiveGlobalContext(WIX_FORMS_CONTEXT, createWixFormsContextState);
|
|
522
|
+
}
|
|
523
|
+
function registeredFieldChecks(context, formId) {
|
|
524
|
+
const canonical = canonicalFormId(formId);
|
|
525
|
+
return canonical ? context[REGISTERED_CHECKS](canonical) : [];
|
|
526
|
+
}
|
|
527
|
+
function emitSubmitOutcome(context, event) {
|
|
528
|
+
context[EMIT_SUBMIT_OUTCOME](event);
|
|
529
|
+
}
|
|
530
|
+
function createWixFormsContextState() {
|
|
531
|
+
const registrationsByFormId = /* @__PURE__ */ new Map();
|
|
532
|
+
const keysWarnedAbout = /* @__PURE__ */ new Set();
|
|
533
|
+
function registerFieldChecks(formId, checks) {
|
|
534
|
+
const canonical = canonicalFormId(formId);
|
|
535
|
+
if (!canonical) {
|
|
536
|
+
warnAboutNonFormIdKey(formId);
|
|
537
|
+
return () => {
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
registrationsByFormId.set(canonical, [...registrationsFor(canonical), checks]);
|
|
541
|
+
return () => {
|
|
542
|
+
registrationsByFormId.set(
|
|
543
|
+
canonical,
|
|
544
|
+
registrationsFor(canonical).filter((registered) => registered !== checks)
|
|
545
|
+
);
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
function registrationsFor(canonical) {
|
|
549
|
+
return registrationsByFormId.get(canonical) ?? [];
|
|
550
|
+
}
|
|
551
|
+
function warnAboutNonFormIdKey(key) {
|
|
552
|
+
if (keysWarnedAbout.has(key)) return;
|
|
553
|
+
keysWarnedAbout.add(key);
|
|
554
|
+
console.warn(
|
|
555
|
+
`[wix-forms] field checks key "${key}" is not a form id and never matches; key checks by the form's id`
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
const submitOutcomeHandlers = /* @__PURE__ */ new Set();
|
|
559
|
+
function onSubmitOutcome(handler) {
|
|
560
|
+
submitOutcomeHandlers.add(handler);
|
|
561
|
+
return () => submitOutcomeHandlers.delete(handler);
|
|
562
|
+
}
|
|
563
|
+
function emitToHandlers(event) {
|
|
564
|
+
for (const handler of [...submitOutcomeHandlers]) handler({ event });
|
|
565
|
+
}
|
|
566
|
+
return {
|
|
567
|
+
registerFieldChecks,
|
|
568
|
+
onSubmitOutcome,
|
|
569
|
+
[REGISTERED_CHECKS]: registrationsFor,
|
|
570
|
+
[EMIT_SUBMIT_OUTCOME]: emitToHandlers
|
|
571
|
+
};
|
|
572
|
+
}
|
|
506
573
|
makeJayInit().withClient(async () => {
|
|
574
|
+
provideWixFormsContext();
|
|
507
575
|
console.log("[wix-forms] Client ready");
|
|
508
576
|
});
|
|
509
577
|
const getFormSummary = createActionCaller("wixForms.getFormSummary", "GET");
|
|
510
578
|
const submitForm = createActionCaller("wixForms.submitForm", "POST");
|
|
579
|
+
var FieldRule = /* @__PURE__ */ ((FieldRule2) => {
|
|
580
|
+
FieldRule2[FieldRule2["NONE"] = 0] = "NONE";
|
|
581
|
+
FieldRule2[FieldRule2["REQUIRED"] = 1] = "REQUIRED";
|
|
582
|
+
FieldRule2[FieldRule2["MIN_LENGTH"] = 2] = "MIN_LENGTH";
|
|
583
|
+
FieldRule2[FieldRule2["MAX_LENGTH"] = 3] = "MAX_LENGTH";
|
|
584
|
+
FieldRule2[FieldRule2["EMAIL"] = 4] = "EMAIL";
|
|
585
|
+
FieldRule2[FieldRule2["PHONE"] = 5] = "PHONE";
|
|
586
|
+
FieldRule2[FieldRule2["URL"] = 6] = "URL";
|
|
587
|
+
FieldRule2[FieldRule2["PATTERN"] = 7] = "PATTERN";
|
|
588
|
+
FieldRule2[FieldRule2["CUSTOM"] = 8] = "CUSTOM";
|
|
589
|
+
FieldRule2[FieldRule2["SERVER"] = 9] = "SERVER";
|
|
590
|
+
return FieldRule2;
|
|
591
|
+
})(FieldRule || {});
|
|
511
592
|
function validateFormSummaryField(field, value) {
|
|
512
593
|
const trimmed = (value ?? "").trim();
|
|
513
594
|
if (field.required && !trimmed) {
|
|
@@ -530,46 +611,124 @@ function validateFormSummaryField(field, value) {
|
|
|
530
611
|
}
|
|
531
612
|
return "";
|
|
532
613
|
}
|
|
533
|
-
function
|
|
614
|
+
function checkFormField(field, value) {
|
|
534
615
|
if (field.inputType === "checkbox") {
|
|
535
|
-
return field.required && value !== true ?
|
|
616
|
+
return field.required && value !== true ? requiredFailure(field) : PASSED$1;
|
|
536
617
|
}
|
|
537
618
|
if (field.inputType === "multiselect") {
|
|
538
619
|
const empty = !Array.isArray(value) || value.length === 0;
|
|
539
|
-
return field.required && empty ?
|
|
620
|
+
return field.required && empty ? requiredFailure(field) : PASSED$1;
|
|
540
621
|
}
|
|
541
622
|
const trimmed = (typeof value === "string" ? value : "").trim();
|
|
542
623
|
if (field.required && !trimmed) {
|
|
543
|
-
return
|
|
624
|
+
return requiredFailure(field);
|
|
544
625
|
}
|
|
545
626
|
if (!trimmed) {
|
|
546
|
-
return
|
|
627
|
+
return PASSED$1;
|
|
547
628
|
}
|
|
629
|
+
return checkTextRules(field, trimmed);
|
|
630
|
+
}
|
|
631
|
+
const PASSED$1 = { rule: FieldRule.NONE, message: "" };
|
|
632
|
+
function requiredFailure(field) {
|
|
633
|
+
return { rule: FieldRule.REQUIRED, message: `${field.label} is required.` };
|
|
634
|
+
}
|
|
635
|
+
function checkTextRules(field, trimmed) {
|
|
548
636
|
if (field.minLength !== void 0 && trimmed.length < field.minLength) {
|
|
549
|
-
return
|
|
637
|
+
return {
|
|
638
|
+
rule: FieldRule.MIN_LENGTH,
|
|
639
|
+
message: `${field.label} must be at least ${field.minLength} characters.`
|
|
640
|
+
};
|
|
550
641
|
}
|
|
551
642
|
if (field.maxLength !== void 0 && trimmed.length > field.maxLength) {
|
|
552
|
-
return
|
|
643
|
+
return {
|
|
644
|
+
rule: FieldRule.MAX_LENGTH,
|
|
645
|
+
message: `${field.label} must be at most ${field.maxLength} characters.`
|
|
646
|
+
};
|
|
553
647
|
}
|
|
554
648
|
if (field.inputType === "email" && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed)) {
|
|
555
|
-
return "Please enter a valid email address.";
|
|
649
|
+
return { rule: FieldRule.EMAIL, message: "Please enter a valid email address." };
|
|
556
650
|
}
|
|
557
651
|
if (field.inputType === "url" && !/^https?:\/\/.+/.test(trimmed)) {
|
|
558
|
-
return "Please enter a valid URL.";
|
|
652
|
+
return { rule: FieldRule.URL, message: "Please enter a valid URL." };
|
|
559
653
|
}
|
|
560
654
|
if (field.inputType === "tel" && !/^[+()\-\s\d]{7,}$/.test(trimmed)) {
|
|
561
|
-
return "Please enter a valid phone number.";
|
|
655
|
+
return { rule: FieldRule.PHONE, message: "Please enter a valid phone number." };
|
|
562
656
|
}
|
|
563
|
-
|
|
657
|
+
return checkPattern(field, trimmed);
|
|
658
|
+
}
|
|
659
|
+
function checkPattern(field, trimmed) {
|
|
660
|
+
if (!field.pattern) return PASSED$1;
|
|
661
|
+
try {
|
|
662
|
+
return new RegExp(field.pattern).test(trimmed) ? PASSED$1 : { rule: FieldRule.PATTERN, message: `${field.label} is not in the expected format.` };
|
|
663
|
+
} catch {
|
|
664
|
+
return PASSED$1;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
const PASSED = { rule: FieldRule.NONE, message: "" };
|
|
668
|
+
function createPageCheckRunner(formId, fields) {
|
|
669
|
+
const visibleTargets = new Set(
|
|
670
|
+
fields.filter((field) => !field.hidden).map((field) => field.target)
|
|
671
|
+
);
|
|
672
|
+
const targetsWarnedAbout = /* @__PURE__ */ new Set();
|
|
673
|
+
const registrationsScanned = /* @__PURE__ */ new WeakSet();
|
|
674
|
+
const checksLoggedAbout = /* @__PURE__ */ new WeakSet();
|
|
675
|
+
function warnAboutUnknownTargets(registrations) {
|
|
676
|
+
const unscanned = registrations.filter((checks) => !registrationsScanned.has(checks));
|
|
677
|
+
unscanned.forEach((checks) => registrationsScanned.add(checks));
|
|
678
|
+
for (const target of unscanned.flatMap((checks) => Object.keys(checks))) {
|
|
679
|
+
if (visibleTargets.has(target) || targetsWarnedAbout.has(target)) continue;
|
|
680
|
+
targetsWarnedAbout.add(target);
|
|
681
|
+
console.warn(
|
|
682
|
+
`[wix-forms] field checks registered for "${target}", which is not a visible field of form ${formId}; they never run`
|
|
683
|
+
);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
function runCheck(check, value, values) {
|
|
564
687
|
try {
|
|
565
|
-
|
|
566
|
-
|
|
688
|
+
return check(value, values);
|
|
689
|
+
} catch (error) {
|
|
690
|
+
if (!checksLoggedAbout.has(check)) {
|
|
691
|
+
checksLoggedAbout.add(check);
|
|
692
|
+
console.error(
|
|
693
|
+
"[wix-forms] a field check threw; treated as passing",
|
|
694
|
+
{ formId },
|
|
695
|
+
error
|
|
696
|
+
);
|
|
567
697
|
}
|
|
568
|
-
|
|
569
|
-
return "";
|
|
698
|
+
return void 0;
|
|
570
699
|
}
|
|
571
700
|
}
|
|
572
|
-
return
|
|
701
|
+
return (field, value, values) => {
|
|
702
|
+
const context = useGlobalContext(WIX_FORMS_CONTEXT);
|
|
703
|
+
if (!context) return PASSED;
|
|
704
|
+
const registrations = registeredFieldChecks(context, formId);
|
|
705
|
+
warnAboutUnknownTargets(registrations);
|
|
706
|
+
for (const checks of registrations) {
|
|
707
|
+
const fieldChecks = Object.prototype.hasOwnProperty.call(checks, field.target) ? checks[field.target] : [];
|
|
708
|
+
for (const check of Array.isArray(fieldChecks) ? fieldChecks : []) {
|
|
709
|
+
const message = runCheck(check, value, values);
|
|
710
|
+
if (typeof message === "string" && message) {
|
|
711
|
+
return { rule: FieldRule.CUSTOM, message };
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return PASSED;
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
function createSubmitOutcomeReporter(formId) {
|
|
719
|
+
const reportedFormId = canonicalFormId(formId) ?? formId;
|
|
720
|
+
return (outcome) => {
|
|
721
|
+
const context = useGlobalContext(WIX_FORMS_CONTEXT);
|
|
722
|
+
if (!context) return;
|
|
723
|
+
try {
|
|
724
|
+
emitSubmitOutcome(context, { formId: reportedFormId, ...outcome });
|
|
725
|
+
} catch (error) {
|
|
726
|
+
console.error("[wix-forms] an onSubmitOutcome handler threw", {
|
|
727
|
+
formId: reportedFormId,
|
|
728
|
+
error: error instanceof Error ? `${error.name}: ${error.message}` : String(error)
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
};
|
|
573
732
|
}
|
|
574
733
|
const SUBMIT_FAILED_MESSAGE = "We could not complete your request. Please try again.";
|
|
575
734
|
var SubmitState = /* @__PURE__ */ ((SubmitState2) => {
|
|
@@ -639,29 +798,36 @@ function emptyStoreFor(fields) {
|
|
|
639
798
|
}
|
|
640
799
|
return store;
|
|
641
800
|
}
|
|
801
|
+
function toggleSelection(field, { current, optionValue, checked }) {
|
|
802
|
+
const selected = new Set(Array.isArray(current) ? current : []);
|
|
803
|
+
if (checked) selected.add(optionValue);
|
|
804
|
+
else selected.delete(optionValue);
|
|
805
|
+
return field.options.map((option) => option.value).filter((value) => selected.has(value));
|
|
806
|
+
}
|
|
642
807
|
function seedControlValues(store, seeds) {
|
|
643
808
|
const seeded = { ...store };
|
|
809
|
+
const changedTargets = [];
|
|
644
810
|
for (const { target, value, dirty } of seeds) {
|
|
645
|
-
if (dirty ||
|
|
646
|
-
|
|
647
|
-
}
|
|
811
|
+
if (dirty || value === void 0) continue;
|
|
812
|
+
if (!sameFieldValue(seeded[target], value)) changedTargets.push(target);
|
|
648
813
|
seeded[target] = value;
|
|
649
814
|
}
|
|
650
|
-
return seeded;
|
|
815
|
+
return { store: seeded, changedTargets };
|
|
651
816
|
}
|
|
652
|
-
function
|
|
653
|
-
if (
|
|
654
|
-
|
|
655
|
-
|
|
817
|
+
function sameFieldValue(current, next) {
|
|
818
|
+
if (Array.isArray(current) && Array.isArray(next)) {
|
|
819
|
+
return current.length === next.length && current.every((item, index) => item === next[index]);
|
|
820
|
+
}
|
|
821
|
+
return current === next;
|
|
656
822
|
}
|
|
657
|
-
function validateVisibleFields(fields, values) {
|
|
658
|
-
const
|
|
823
|
+
function validateVisibleFields(fields, values, check = checkFormField) {
|
|
824
|
+
const failures = {};
|
|
659
825
|
for (const field of fields) {
|
|
660
826
|
if (field.hidden) continue;
|
|
661
|
-
const
|
|
662
|
-
if (
|
|
827
|
+
const result = check(field, values[field.target] ?? emptyValueFor(field));
|
|
828
|
+
if (result.rule !== FieldRule.NONE) failures[field.target] = result;
|
|
663
829
|
}
|
|
664
|
-
return
|
|
830
|
+
return failures;
|
|
665
831
|
}
|
|
666
832
|
var __assign = function() {
|
|
667
833
|
__assign = Object.assign || function __assign2(t) {
|
|
@@ -760,7 +926,8 @@ function fieldToViewState(field, state) {
|
|
|
760
926
|
label: field.label,
|
|
761
927
|
required: field.required,
|
|
762
928
|
error: state.error,
|
|
763
|
-
invalid: state.invalid
|
|
929
|
+
invalid: state.invalid,
|
|
930
|
+
errorRule: state.errorRule
|
|
764
931
|
};
|
|
765
932
|
if (field.inputType === "checkbox") {
|
|
766
933
|
viewState.value = state.value === true;
|
|
@@ -782,12 +949,13 @@ function fieldToViewState(field, state) {
|
|
|
782
949
|
}
|
|
783
950
|
return viewState;
|
|
784
951
|
}
|
|
785
|
-
function buildFieldsViewState(fields, errorByTarget = {}, invalidByTarget = {}, valueByTarget = {}) {
|
|
952
|
+
function buildFieldsViewState(fields, errorByTarget = {}, invalidByTarget = {}, valueByTarget = {}, ruleByTarget = {}) {
|
|
786
953
|
const viewState = {};
|
|
787
954
|
for (const field of fields) {
|
|
788
955
|
viewState[contractTagName(field.target)] = fieldToViewState(field, {
|
|
789
956
|
error: errorByTarget[field.target] ?? "",
|
|
790
957
|
invalid: invalidByTarget[field.target] ?? false,
|
|
958
|
+
errorRule: ruleByTarget[field.target] ?? FieldRule.NONE,
|
|
791
959
|
value: valueByTarget[field.target] ?? ""
|
|
792
960
|
});
|
|
793
961
|
}
|
|
@@ -799,7 +967,8 @@ function buildFormViewState(fields, overrides) {
|
|
|
799
967
|
fields,
|
|
800
968
|
overrides.errorByTarget,
|
|
801
969
|
overrides.invalidByTarget,
|
|
802
|
-
overrides.valueByTarget
|
|
970
|
+
overrides.valueByTarget,
|
|
971
|
+
overrides.ruleByTarget
|
|
803
972
|
),
|
|
804
973
|
isLoading: fields.length === 0 && !overrides.loadError,
|
|
805
974
|
loadError: overrides.loadError,
|
|
@@ -831,9 +1000,7 @@ function createSubmitFlight() {
|
|
|
831
1000
|
};
|
|
832
1001
|
}
|
|
833
1002
|
async function runFormSubmit(params) {
|
|
834
|
-
|
|
835
|
-
const generation = params.flight.begin();
|
|
836
|
-
params.setIsSubmitting(true);
|
|
1003
|
+
const { generation } = params;
|
|
837
1004
|
let result;
|
|
838
1005
|
try {
|
|
839
1006
|
await params.ensureSessionFresh();
|
|
@@ -869,29 +1036,40 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
869
1036
|
const [statusMessage, setStatusMessage] = createSignal("");
|
|
870
1037
|
const [errorByTarget, setErrorByTarget] = createSignal({});
|
|
871
1038
|
const [invalidByTarget, setInvalidByTarget] = createSignal({});
|
|
1039
|
+
const [ruleByTarget, setRuleByTarget] = createSignal({});
|
|
872
1040
|
const [isSubmitted, setIsSubmitted] = createSignal(false);
|
|
873
1041
|
const [submitFailure, setSubmitFailure] = createSignal(SubmitFailure.NONE);
|
|
874
1042
|
const [values, setValues] = createSignal({});
|
|
875
1043
|
const dirtyTargets = /* @__PURE__ */ new Set();
|
|
876
1044
|
let honeypotValue = "";
|
|
877
1045
|
const submitFlight = createSubmitFlight();
|
|
1046
|
+
const selectBaselineByTarget = /* @__PURE__ */ new Map();
|
|
1047
|
+
const runPageChecks = createPageCheckRunner(carryForward.formId, carryForward.fields);
|
|
1048
|
+
const reportSubmitOutcome = createSubmitOutcomeReporter(carryForward.formId);
|
|
878
1049
|
function setFieldValue(target, value) {
|
|
879
1050
|
setValues((current) => ({ ...current, [target]: value }));
|
|
880
1051
|
}
|
|
881
1052
|
function readFormValues() {
|
|
882
1053
|
return projectSubmissionValues(carryForward.fields, values());
|
|
883
1054
|
}
|
|
1055
|
+
function checkField(field, value = values()[field.target] ?? emptyValueFor(field), allValues = readFormValues()) {
|
|
1056
|
+
const builtIn = checkFormField(field, value);
|
|
1057
|
+
return builtIn.rule === FieldRule.NONE ? runPageChecks(field, value, allValues) : builtIn;
|
|
1058
|
+
}
|
|
884
1059
|
function validateField(field) {
|
|
885
|
-
const
|
|
1060
|
+
const { rule, message } = checkField(field);
|
|
886
1061
|
setErrorByTarget((current) => ({ ...current, [field.target]: message }));
|
|
887
1062
|
setInvalidByTarget((current) => ({ ...current, [field.target]: Boolean(message) }));
|
|
1063
|
+
setRuleByTarget((current) => ({ ...current, [field.target]: rule }));
|
|
888
1064
|
}
|
|
889
1065
|
function clearFieldError(field) {
|
|
890
1066
|
setErrorByTarget((current) => ({ ...current, [field.target]: "" }));
|
|
891
1067
|
setInvalidByTarget((current) => ({ ...current, [field.target]: false }));
|
|
1068
|
+
setRuleByTarget((current) => ({ ...current, [field.target]: FieldRule.NONE }));
|
|
892
1069
|
}
|
|
893
1070
|
function reflectToInput(field, value) {
|
|
894
|
-
refs[contractTagName(field.target)]
|
|
1071
|
+
const fieldRef = refs[contractTagName(field.target)];
|
|
1072
|
+
fieldRef?.input?.exec$((element) => {
|
|
895
1073
|
if (field.inputType === "checkbox") {
|
|
896
1074
|
element.checked = value === true;
|
|
897
1075
|
} else if (field.inputType === "multiselect") {
|
|
@@ -903,11 +1081,23 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
903
1081
|
element.value = typeof value === "string" ? value : "";
|
|
904
1082
|
}
|
|
905
1083
|
});
|
|
1084
|
+
if (field.inputType === "multiselect") {
|
|
1085
|
+
const selected = Array.isArray(value) ? value : [];
|
|
1086
|
+
fieldRef?.options?.toggle?.map((option, item) => void option.exec$((element) => {
|
|
1087
|
+
element.checked = selected.includes(item.value);
|
|
1088
|
+
}));
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
function handleNativeBlur(field) {
|
|
1092
|
+
if (dirtyTargets.has(field.target) || errorByTarget()[field.target])
|
|
1093
|
+
validateField(field);
|
|
906
1094
|
}
|
|
907
1095
|
function handleNativeInput(field, event) {
|
|
908
1096
|
dirtyTargets.add(field.target);
|
|
909
1097
|
setFieldValue(field.target, readNativeValue(field, event.target));
|
|
910
|
-
|
|
1098
|
+
if (errorByTarget()[field.target] && checkField(field).rule === FieldRule.NONE) {
|
|
1099
|
+
clearFieldError(field);
|
|
1100
|
+
}
|
|
911
1101
|
}
|
|
912
1102
|
function handleControl(field, event) {
|
|
913
1103
|
dirtyTargets.add(field.target);
|
|
@@ -916,23 +1106,63 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
916
1106
|
reflectToInput(field, value);
|
|
917
1107
|
validateField(field);
|
|
918
1108
|
}
|
|
1109
|
+
function handleOptionToggle(field, change) {
|
|
1110
|
+
dirtyTargets.add(field.target);
|
|
1111
|
+
setFieldValue(field.target, toggleSelection(field, { current: values()[field.target], ...change }));
|
|
1112
|
+
validateField(field);
|
|
1113
|
+
}
|
|
919
1114
|
wireFieldEvents(refs, carryForward.fields, {
|
|
920
1115
|
onNativeInput: handleNativeInput,
|
|
921
|
-
onNativeBlur:
|
|
922
|
-
onControl: handleControl
|
|
1116
|
+
onNativeBlur: handleNativeBlur,
|
|
1117
|
+
onControl: handleControl,
|
|
1118
|
+
onOptionToggle: handleOptionToggle
|
|
923
1119
|
});
|
|
924
1120
|
wireHoneypot(refs, (value) => {
|
|
925
1121
|
honeypotValue = value;
|
|
926
1122
|
});
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
1123
|
+
const reactive = useReactive();
|
|
1124
|
+
function afterRemount(work) {
|
|
1125
|
+
void reactive.toBeClean().then(work);
|
|
1126
|
+
}
|
|
930
1127
|
afterRemount(() => wireNativeFormSubmit(refs, submit));
|
|
1128
|
+
afterRemount(() => void seedAndReflect().then(captureSelectBaselines));
|
|
1129
|
+
afterRemount(() => watchControlValues(refs, carryForward.fields, seedAndReflect));
|
|
1130
|
+
async function seedAndReflect() {
|
|
1131
|
+
const changedTargets = await seedControlsIntoStore(refs, carryForward.fields, dirtyTargets, setValues);
|
|
1132
|
+
for (const field of carryForward.fields) {
|
|
1133
|
+
if (!field.hidden && changedTargets.includes(field.target)) {
|
|
1134
|
+
reflectToInput(field, values()[field.target]);
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
async function captureSelectBaselines() {
|
|
1139
|
+
await Promise.all(reconcilableFields(carryForward.fields).filter((field) => field.inputType === "select").map(async (field) => {
|
|
1140
|
+
const domValue = await readBoundInputValue(refs, field);
|
|
1141
|
+
if (domValue !== void 0)
|
|
1142
|
+
selectBaselineByTarget.set(field.target, domValue);
|
|
1143
|
+
}));
|
|
1144
|
+
}
|
|
1145
|
+
async function reconcileNativeInputs() {
|
|
1146
|
+
await Promise.all(reconcilableFields(carryForward.fields).map(async (field) => {
|
|
1147
|
+
const domValue = await readBoundInputValue(refs, field);
|
|
1148
|
+
if (domValue === void 0 || !isReconcileNeeded(field, domValue))
|
|
1149
|
+
return;
|
|
1150
|
+
dirtyTargets.add(field.target);
|
|
1151
|
+
setFieldValue(field.target, domValue);
|
|
1152
|
+
}));
|
|
1153
|
+
}
|
|
1154
|
+
function isReconcileNeeded(field, domValue) {
|
|
1155
|
+
if (field.inputType === "select" && domValue === (selectBaselineByTarget.get(field.target) ?? "")) {
|
|
1156
|
+
return false;
|
|
1157
|
+
}
|
|
1158
|
+
return domValue !== (values()[field.target] ?? emptyValueFor(field));
|
|
1159
|
+
}
|
|
931
1160
|
function resetForm() {
|
|
932
1161
|
submitFlight.invalidate();
|
|
933
1162
|
setValues(emptyStoreFor(carryForward.fields));
|
|
934
1163
|
setErrorByTarget({});
|
|
935
1164
|
setInvalidByTarget({});
|
|
1165
|
+
setRuleByTarget({});
|
|
936
1166
|
setStatusMessage("");
|
|
937
1167
|
setSubmitFailure(SubmitFailure.NONE);
|
|
938
1168
|
setIsSubmitted(false);
|
|
@@ -954,7 +1184,11 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
954
1184
|
for (const field of carryForward.fields) {
|
|
955
1185
|
reflectToInput(field, emptyValueFor(field));
|
|
956
1186
|
}
|
|
1187
|
+
selectBaselineByTarget.clear();
|
|
1188
|
+
void captureSelectBaselines();
|
|
957
1189
|
wireNativeFormSubmit(refs, submit);
|
|
1190
|
+
void seedAndReflect();
|
|
1191
|
+
watchControlValues(refs, carryForward.fields, seedAndReflect);
|
|
958
1192
|
});
|
|
959
1193
|
}
|
|
960
1194
|
refs.resetButton?.onclick(({ event }) => {
|
|
@@ -982,34 +1216,63 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
982
1216
|
setStatusMessage("");
|
|
983
1217
|
setSubmitFailure(SubmitFailure.NONE);
|
|
984
1218
|
setErrorByTarget({});
|
|
1219
|
+
setInvalidByTarget({});
|
|
1220
|
+
setRuleByTarget({});
|
|
985
1221
|
if (honeypotValue.trim()) {
|
|
986
1222
|
showSubmitted();
|
|
1223
|
+
reportSubmitOutcome({ outcome: "succeeded" });
|
|
987
1224
|
return;
|
|
988
1225
|
}
|
|
989
|
-
|
|
990
|
-
const
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
submitForm,
|
|
1005
|
-
formId: carryForward.formId,
|
|
1006
|
-
submissionValues,
|
|
1007
|
-
presentSubmitResult,
|
|
1008
|
-
navigate: (path) => window.location.assign(path),
|
|
1009
|
-
onNetworkFailure: () => {
|
|
1010
|
-
setStatusMessage(SUBMIT_FAILED_MESSAGE);
|
|
1011
|
-
setSubmitFailure(SubmitFailure.NETWORK);
|
|
1226
|
+
setIsSubmitting(true);
|
|
1227
|
+
const generation = submitFlight.begin();
|
|
1228
|
+
let isHandedOff = false;
|
|
1229
|
+
try {
|
|
1230
|
+
await reconcileNativeInputs();
|
|
1231
|
+
if (!submitFlight.isCurrent(generation))
|
|
1232
|
+
return;
|
|
1233
|
+
await seedAndReflect();
|
|
1234
|
+
if (!submitFlight.isCurrent(generation))
|
|
1235
|
+
return;
|
|
1236
|
+
const submissionValues = readFormValues();
|
|
1237
|
+
const clientFailures = validateVisibleFields(carryForward.fields, submissionValues, (field, value) => checkField(field, value, submissionValues));
|
|
1238
|
+
if (Object.keys(clientFailures).length) {
|
|
1239
|
+
showClientErrors(clientFailures);
|
|
1240
|
+
return;
|
|
1012
1241
|
}
|
|
1242
|
+
isHandedOff = true;
|
|
1243
|
+
await runFormSubmit({
|
|
1244
|
+
flight: submitFlight,
|
|
1245
|
+
generation,
|
|
1246
|
+
setIsSubmitting,
|
|
1247
|
+
ensureSessionFresh,
|
|
1248
|
+
submitForm,
|
|
1249
|
+
formId: carryForward.formId,
|
|
1250
|
+
submissionValues,
|
|
1251
|
+
presentSubmitResult,
|
|
1252
|
+
navigate: (path) => window.location.assign(path),
|
|
1253
|
+
onNetworkFailure: () => {
|
|
1254
|
+
setStatusMessage(SUBMIT_FAILED_MESSAGE);
|
|
1255
|
+
setSubmitFailure(SubmitFailure.NETWORK);
|
|
1256
|
+
reportSubmitOutcome({ outcome: "failed", reason: SubmitFailure.NETWORK });
|
|
1257
|
+
}
|
|
1258
|
+
});
|
|
1259
|
+
} finally {
|
|
1260
|
+
if (!isHandedOff && submitFlight.isCurrent(generation))
|
|
1261
|
+
setIsSubmitting(false);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
function showClientErrors(failures) {
|
|
1265
|
+
const targets = Object.keys(failures);
|
|
1266
|
+
const errors = Object.fromEntries(targets.map((target) => [target, failures[target].message]));
|
|
1267
|
+
setErrorByTarget(errors);
|
|
1268
|
+
setInvalidByTarget(Object.fromEntries(targets.map((target) => [target, true])));
|
|
1269
|
+
setRuleByTarget(Object.fromEntries(targets.map((target) => [target, failures[target].rule])));
|
|
1270
|
+
showFieldErrors(errors);
|
|
1271
|
+
setSubmitFailure(SubmitFailure.VALIDATION);
|
|
1272
|
+
reportSubmitOutcome({
|
|
1273
|
+
outcome: "failed",
|
|
1274
|
+
reason: SubmitFailure.VALIDATION,
|
|
1275
|
+
fieldErrors: errors
|
|
1013
1276
|
});
|
|
1014
1277
|
}
|
|
1015
1278
|
function presentSubmitResult(result) {
|
|
@@ -1017,12 +1280,16 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
1017
1280
|
const rejection = presentSubmitRejection(result, carryForward.fields);
|
|
1018
1281
|
setErrorByTarget(rejection.errorByTarget);
|
|
1019
1282
|
setInvalidByTarget(rejection.invalidByTarget);
|
|
1283
|
+
setRuleByTarget(Object.fromEntries(Object.keys(rejection.errorByTarget).map((target) => [
|
|
1284
|
+
target,
|
|
1285
|
+
FieldRule.SERVER
|
|
1286
|
+
])));
|
|
1020
1287
|
setStatusMessage(rejection.statusMessage);
|
|
1021
1288
|
setSubmitFailure(result.reason ?? SubmitFailure.UNKNOWN);
|
|
1022
1289
|
showFieldErrors(rejection.errorByTarget);
|
|
1290
|
+
reportSubmitRejection(result);
|
|
1023
1291
|
return void 0;
|
|
1024
1292
|
}
|
|
1025
|
-
showSubmitted();
|
|
1026
1293
|
const redirectTo = result.redirectTo;
|
|
1027
1294
|
const redirectPath = toSitePath(redirectTo);
|
|
1028
1295
|
if (redirectTo !== void 0 && !redirectPath) {
|
|
@@ -1030,8 +1297,19 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
1030
1297
|
formId: carryForward.formId
|
|
1031
1298
|
});
|
|
1032
1299
|
}
|
|
1300
|
+
if (!redirectPath)
|
|
1301
|
+
showSubmitted();
|
|
1302
|
+
reportSubmitOutcome(redirectPath === void 0 ? { outcome: "succeeded" } : { outcome: "succeeded", redirectTo: redirectPath });
|
|
1033
1303
|
return redirectPath;
|
|
1034
1304
|
}
|
|
1305
|
+
function reportSubmitRejection(result) {
|
|
1306
|
+
reportSubmitOutcome({
|
|
1307
|
+
outcome: "failed",
|
|
1308
|
+
reason: result.reason ?? SubmitFailure.UNKNOWN,
|
|
1309
|
+
...result.code === void 0 ? {} : { code: result.code },
|
|
1310
|
+
...result.fieldErrors === void 0 ? {} : { fieldErrors: result.fieldErrors }
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1035
1313
|
return {
|
|
1036
1314
|
render: () => buildFormViewState(carryForward.fields, {
|
|
1037
1315
|
isSubmitting: isSubmitting(),
|
|
@@ -1039,6 +1317,7 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
|
|
|
1039
1317
|
loadError: carryForward.loadError ?? "",
|
|
1040
1318
|
errorByTarget: errorByTarget(),
|
|
1041
1319
|
invalidByTarget: invalidByTarget(),
|
|
1320
|
+
ruleByTarget: ruleByTarget(),
|
|
1042
1321
|
valueByTarget: values(),
|
|
1043
1322
|
isSubmitted: isSubmitted(),
|
|
1044
1323
|
submitFailure: submitFailure()
|
|
@@ -1054,16 +1333,28 @@ function wireFieldEvents(refs, fields, handlers) {
|
|
|
1054
1333
|
fieldRef.input?.onblur(() => handlers.onNativeBlur(field));
|
|
1055
1334
|
fieldRef.control?.onclick(({ event }) => handlers.onControl(field, event));
|
|
1056
1335
|
fieldRef.control?.oninput(({ event }) => handlers.onControl(field, event));
|
|
1336
|
+
if (field.inputType === "multiselect") {
|
|
1337
|
+
fieldRef.options?.toggle?.onchange(({ event, viewState }) => handlers.onOptionToggle(field, {
|
|
1338
|
+
optionValue: viewState.value,
|
|
1339
|
+
checked: event.target.checked
|
|
1340
|
+
}));
|
|
1341
|
+
}
|
|
1057
1342
|
}
|
|
1058
1343
|
}
|
|
1059
|
-
function afterRemount(work) {
|
|
1060
|
-
queueMicrotask(work);
|
|
1061
|
-
}
|
|
1062
1344
|
function focusFirstFieldInput(refs, fields, matches) {
|
|
1063
|
-
const field = fields.find((candidate) => !candidate.hidden && matches(candidate) && refs
|
|
1345
|
+
const field = fields.find((candidate) => !candidate.hidden && matches(candidate) && focusableRef(refs, candidate));
|
|
1064
1346
|
if (!field)
|
|
1065
1347
|
return;
|
|
1066
|
-
void refs
|
|
1348
|
+
void focusableRef(refs, field).exec$((element) => element.focus());
|
|
1349
|
+
}
|
|
1350
|
+
function focusableRef(refs, field) {
|
|
1351
|
+
const fieldRef = refs[contractTagName(field.target)];
|
|
1352
|
+
if (field.inputType === "multiselect") {
|
|
1353
|
+
const firstChip = fieldRef?.options?.toggle?.find(() => true);
|
|
1354
|
+
if (firstChip)
|
|
1355
|
+
return firstChip;
|
|
1356
|
+
}
|
|
1357
|
+
return fieldRef?.input;
|
|
1067
1358
|
}
|
|
1068
1359
|
function owningSubmitForm(target) {
|
|
1069
1360
|
const button = target?.closest?.("button");
|
|
@@ -1081,15 +1372,45 @@ function seedControlsIntoStore(refs, fields, dirtyTargets, setValues) {
|
|
|
1081
1372
|
return control.exec$((element) => readControlValue(element)).then((value) => {
|
|
1082
1373
|
seeds.push({
|
|
1083
1374
|
target: field.target,
|
|
1084
|
-
value
|
|
1375
|
+
value,
|
|
1085
1376
|
dirty: dirtyTargets.has(field.target)
|
|
1086
1377
|
});
|
|
1087
1378
|
});
|
|
1088
1379
|
});
|
|
1089
1380
|
return Promise.all(reads).then(() => {
|
|
1090
|
-
|
|
1381
|
+
let changedTargets = [];
|
|
1382
|
+
setValues((current) => {
|
|
1383
|
+
const seeded = seedControlValues(current, seeds);
|
|
1384
|
+
changedTargets = seeded.changedTargets;
|
|
1385
|
+
return seeded.store;
|
|
1386
|
+
});
|
|
1387
|
+
return changedTargets;
|
|
1091
1388
|
});
|
|
1092
1389
|
}
|
|
1390
|
+
const watchedControls = /* @__PURE__ */ new WeakSet();
|
|
1391
|
+
function watchControlValues(refs, fields, onControlValueChange) {
|
|
1392
|
+
if (typeof MutationObserver === "undefined")
|
|
1393
|
+
return;
|
|
1394
|
+
for (const field of fields) {
|
|
1395
|
+
void refs[contractTagName(field.target)]?.control?.exec$((element) => {
|
|
1396
|
+
if (watchedControls.has(element))
|
|
1397
|
+
return;
|
|
1398
|
+
watchedControls.add(element);
|
|
1399
|
+
new MutationObserver(() => void onControlValueChange()).observe(element, {
|
|
1400
|
+
attributeFilter: ["data-value"]
|
|
1401
|
+
});
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
function reconcilableFields(fields) {
|
|
1406
|
+
return fields.filter((field) => !field.hidden && field.inputType !== "checkbox" && field.inputType !== "multiselect");
|
|
1407
|
+
}
|
|
1408
|
+
function readBoundInputValue(refs, field) {
|
|
1409
|
+
const input = refs[contractTagName(field.target)]?.input;
|
|
1410
|
+
if (!input?.exec$)
|
|
1411
|
+
return Promise.resolve(void 0);
|
|
1412
|
+
return input.exec$((element) => element.value);
|
|
1413
|
+
}
|
|
1093
1414
|
function wireHoneypot(refs, record) {
|
|
1094
1415
|
refs.honeypot?.oninput(({ event }) => {
|
|
1095
1416
|
record(event.target.value ?? "");
|