@jay-framework/wix-bookings 0.26.1 → 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.
Files changed (2) hide show
  1. package/dist/index.client.js +349 -68
  2. package/package.json +14 -14
@@ -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, createEffect, useReactive } from "@jay-framework/component";
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;
@@ -508,11 +508,87 @@ var BookingFailure = /* @__PURE__ */ ((BookingFailure2) => {
508
508
  const listServices = createActionCaller("wixBookings.listServices", "GET");
509
509
  const listSlots = createActionCaller("wixBookings.listSlots", "GET");
510
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
+ }
511
573
  makeJayInit().withClient(async () => {
574
+ provideWixFormsContext();
512
575
  console.log("[wix-forms] Client ready");
513
576
  });
514
577
  const getFormSummary = createActionCaller("wixForms.getFormSummary", "GET");
515
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 || {});
516
592
  function validateFormSummaryField(field, value) {
517
593
  const trimmed = (value ?? "").trim();
518
594
  if (field.required && !trimmed) {
@@ -535,46 +611,124 @@ function validateFormSummaryField(field, value) {
535
611
  }
536
612
  return "";
537
613
  }
538
- function validateFormField(field, value) {
614
+ function checkFormField(field, value) {
539
615
  if (field.inputType === "checkbox") {
540
- return field.required && value !== true ? `${field.label} is required.` : "";
616
+ return field.required && value !== true ? requiredFailure(field) : PASSED$1;
541
617
  }
542
618
  if (field.inputType === "multiselect") {
543
619
  const empty = !Array.isArray(value) || value.length === 0;
544
- return field.required && empty ? `${field.label} is required.` : "";
620
+ return field.required && empty ? requiredFailure(field) : PASSED$1;
545
621
  }
546
622
  const trimmed = (typeof value === "string" ? value : "").trim();
547
623
  if (field.required && !trimmed) {
548
- return `${field.label} is required.`;
624
+ return requiredFailure(field);
549
625
  }
550
626
  if (!trimmed) {
551
- return "";
627
+ return PASSED$1;
552
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) {
553
636
  if (field.minLength !== void 0 && trimmed.length < field.minLength) {
554
- return `${field.label} must be at least ${field.minLength} characters.`;
637
+ return {
638
+ rule: FieldRule.MIN_LENGTH,
639
+ message: `${field.label} must be at least ${field.minLength} characters.`
640
+ };
555
641
  }
556
642
  if (field.maxLength !== void 0 && trimmed.length > field.maxLength) {
557
- return `${field.label} must be at most ${field.maxLength} characters.`;
643
+ return {
644
+ rule: FieldRule.MAX_LENGTH,
645
+ message: `${field.label} must be at most ${field.maxLength} characters.`
646
+ };
558
647
  }
559
648
  if (field.inputType === "email" && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed)) {
560
- return "Please enter a valid email address.";
649
+ return { rule: FieldRule.EMAIL, message: "Please enter a valid email address." };
561
650
  }
562
651
  if (field.inputType === "url" && !/^https?:\/\/.+/.test(trimmed)) {
563
- return "Please enter a valid URL.";
652
+ return { rule: FieldRule.URL, message: "Please enter a valid URL." };
564
653
  }
565
654
  if (field.inputType === "tel" && !/^[+()\-\s\d]{7,}$/.test(trimmed)) {
566
- return "Please enter a valid phone number.";
655
+ return { rule: FieldRule.PHONE, message: "Please enter a valid phone number." };
656
+ }
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
+ }
567
685
  }
568
- if (field.pattern) {
686
+ function runCheck(check, value, values) {
569
687
  try {
570
- if (!new RegExp(field.pattern).test(trimmed)) {
571
- return `${field.label} is not in the expected format.`;
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
+ );
572
697
  }
573
- } catch {
574
- return "";
698
+ return void 0;
575
699
  }
576
700
  }
577
- 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
+ };
578
732
  }
579
733
  const SUBMIT_FAILED_MESSAGE = "We could not complete your request. Please try again.";
580
734
  var SubmitState = /* @__PURE__ */ ((SubmitState2) => {
@@ -652,27 +806,28 @@ function toggleSelection(field, { current, optionValue, checked }) {
652
806
  }
653
807
  function seedControlValues(store, seeds) {
654
808
  const seeded = { ...store };
809
+ const changedTargets = [];
655
810
  for (const { target, value, dirty } of seeds) {
656
- if (dirty || target in seeded || !hasValue(value)) {
657
- continue;
658
- }
811
+ if (dirty || value === void 0) continue;
812
+ if (!sameFieldValue(seeded[target], value)) changedTargets.push(target);
659
813
  seeded[target] = value;
660
814
  }
661
- return seeded;
815
+ return { store: seeded, changedTargets };
662
816
  }
663
- function hasValue(value) {
664
- if (typeof value === "string") return value.length > 0;
665
- if (Array.isArray(value)) return value.length > 0;
666
- return value === true;
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;
667
822
  }
668
- function validateVisibleFields(fields, values) {
669
- const errors = {};
823
+ function validateVisibleFields(fields, values, check = checkFormField) {
824
+ const failures = {};
670
825
  for (const field of fields) {
671
826
  if (field.hidden) continue;
672
- const message = validateFormField(field, values[field.target] ?? emptyValueFor(field));
673
- if (message) errors[field.target] = message;
827
+ const result = check(field, values[field.target] ?? emptyValueFor(field));
828
+ if (result.rule !== FieldRule.NONE) failures[field.target] = result;
674
829
  }
675
- return errors;
830
+ return failures;
676
831
  }
677
832
  var __assign = function() {
678
833
  __assign = Object.assign || function __assign2(t) {
@@ -771,7 +926,8 @@ function fieldToViewState(field, state) {
771
926
  label: field.label,
772
927
  required: field.required,
773
928
  error: state.error,
774
- invalid: state.invalid
929
+ invalid: state.invalid,
930
+ errorRule: state.errorRule
775
931
  };
776
932
  if (field.inputType === "checkbox") {
777
933
  viewState.value = state.value === true;
@@ -793,12 +949,13 @@ function fieldToViewState(field, state) {
793
949
  }
794
950
  return viewState;
795
951
  }
796
- function buildFieldsViewState(fields, errorByTarget = {}, invalidByTarget = {}, valueByTarget = {}) {
952
+ function buildFieldsViewState(fields, errorByTarget = {}, invalidByTarget = {}, valueByTarget = {}, ruleByTarget = {}) {
797
953
  const viewState = {};
798
954
  for (const field of fields) {
799
955
  viewState[contractTagName(field.target)] = fieldToViewState(field, {
800
956
  error: errorByTarget[field.target] ?? "",
801
957
  invalid: invalidByTarget[field.target] ?? false,
958
+ errorRule: ruleByTarget[field.target] ?? FieldRule.NONE,
802
959
  value: valueByTarget[field.target] ?? ""
803
960
  });
804
961
  }
@@ -810,7 +967,8 @@ function buildFormViewState(fields, overrides) {
810
967
  fields,
811
968
  overrides.errorByTarget,
812
969
  overrides.invalidByTarget,
813
- overrides.valueByTarget
970
+ overrides.valueByTarget,
971
+ overrides.ruleByTarget
814
972
  ),
815
973
  isLoading: fields.length === 0 && !overrides.loadError,
816
974
  loadError: overrides.loadError,
@@ -842,9 +1000,7 @@ function createSubmitFlight() {
842
1000
  };
843
1001
  }
844
1002
  async function runFormSubmit(params) {
845
- if (params.isSubmitting()) return;
846
- const generation = params.flight.begin();
847
- params.setIsSubmitting(true);
1003
+ const { generation } = params;
848
1004
  let result;
849
1005
  try {
850
1006
  await params.ensureSessionFresh();
@@ -880,26 +1036,36 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
880
1036
  const [statusMessage, setStatusMessage] = createSignal("");
881
1037
  const [errorByTarget, setErrorByTarget] = createSignal({});
882
1038
  const [invalidByTarget, setInvalidByTarget] = createSignal({});
1039
+ const [ruleByTarget, setRuleByTarget] = createSignal({});
883
1040
  const [isSubmitted, setIsSubmitted] = createSignal(false);
884
1041
  const [submitFailure, setSubmitFailure] = createSignal(SubmitFailure.NONE);
885
1042
  const [values, setValues] = createSignal({});
886
1043
  const dirtyTargets = /* @__PURE__ */ new Set();
887
1044
  let honeypotValue = "";
888
1045
  const submitFlight = createSubmitFlight();
1046
+ const selectBaselineByTarget = /* @__PURE__ */ new Map();
1047
+ const runPageChecks = createPageCheckRunner(carryForward.formId, carryForward.fields);
1048
+ const reportSubmitOutcome = createSubmitOutcomeReporter(carryForward.formId);
889
1049
  function setFieldValue(target, value) {
890
1050
  setValues((current) => ({ ...current, [target]: value }));
891
1051
  }
892
1052
  function readFormValues() {
893
1053
  return projectSubmissionValues(carryForward.fields, values());
894
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
+ }
895
1059
  function validateField(field) {
896
- const message = validateFormField(field, values()[field.target] ?? emptyValueFor(field));
1060
+ const { rule, message } = checkField(field);
897
1061
  setErrorByTarget((current) => ({ ...current, [field.target]: message }));
898
1062
  setInvalidByTarget((current) => ({ ...current, [field.target]: Boolean(message) }));
1063
+ setRuleByTarget((current) => ({ ...current, [field.target]: rule }));
899
1064
  }
900
1065
  function clearFieldError(field) {
901
1066
  setErrorByTarget((current) => ({ ...current, [field.target]: "" }));
902
1067
  setInvalidByTarget((current) => ({ ...current, [field.target]: false }));
1068
+ setRuleByTarget((current) => ({ ...current, [field.target]: FieldRule.NONE }));
903
1069
  }
904
1070
  function reflectToInput(field, value) {
905
1071
  const fieldRef = refs[contractTagName(field.target)];
@@ -922,10 +1088,16 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
922
1088
  }));
923
1089
  }
924
1090
  }
1091
+ function handleNativeBlur(field) {
1092
+ if (dirtyTargets.has(field.target) || errorByTarget()[field.target])
1093
+ validateField(field);
1094
+ }
925
1095
  function handleNativeInput(field, event) {
926
1096
  dirtyTargets.add(field.target);
927
1097
  setFieldValue(field.target, readNativeValue(field, event.target));
928
- clearFieldError(field);
1098
+ if (errorByTarget()[field.target] && checkField(field).rule === FieldRule.NONE) {
1099
+ clearFieldError(field);
1100
+ }
929
1101
  }
930
1102
  function handleControl(field, event) {
931
1103
  dirtyTargets.add(field.target);
@@ -941,26 +1113,56 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
941
1113
  }
942
1114
  wireFieldEvents(refs, carryForward.fields, {
943
1115
  onNativeInput: handleNativeInput,
944
- onNativeBlur: validateField,
1116
+ onNativeBlur: handleNativeBlur,
945
1117
  onControl: handleControl,
946
1118
  onOptionToggle: handleOptionToggle
947
1119
  });
948
1120
  wireHoneypot(refs, (value) => {
949
1121
  honeypotValue = value;
950
1122
  });
951
- createEffect(() => {
952
- void seedControlsIntoStore(refs, carryForward.fields, dirtyTargets, setValues);
953
- });
954
1123
  const reactive = useReactive();
955
1124
  function afterRemount(work) {
956
1125
  void reactive.toBeClean().then(work);
957
1126
  }
958
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
+ }
959
1160
  function resetForm() {
960
1161
  submitFlight.invalidate();
961
1162
  setValues(emptyStoreFor(carryForward.fields));
962
1163
  setErrorByTarget({});
963
1164
  setInvalidByTarget({});
1165
+ setRuleByTarget({});
964
1166
  setStatusMessage("");
965
1167
  setSubmitFailure(SubmitFailure.NONE);
966
1168
  setIsSubmitted(false);
@@ -982,7 +1184,11 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
982
1184
  for (const field of carryForward.fields) {
983
1185
  reflectToInput(field, emptyValueFor(field));
984
1186
  }
1187
+ selectBaselineByTarget.clear();
1188
+ void captureSelectBaselines();
985
1189
  wireNativeFormSubmit(refs, submit);
1190
+ void seedAndReflect();
1191
+ watchControlValues(refs, carryForward.fields, seedAndReflect);
986
1192
  });
987
1193
  }
988
1194
  refs.resetButton?.onclick(({ event }) => {
@@ -1010,34 +1216,63 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
1010
1216
  setStatusMessage("");
1011
1217
  setSubmitFailure(SubmitFailure.NONE);
1012
1218
  setErrorByTarget({});
1219
+ setInvalidByTarget({});
1220
+ setRuleByTarget({});
1013
1221
  if (honeypotValue.trim()) {
1014
1222
  showSubmitted();
1223
+ reportSubmitOutcome({ outcome: "succeeded" });
1015
1224
  return;
1016
1225
  }
1017
- await seedControlsIntoStore(refs, carryForward.fields, dirtyTargets, setValues);
1018
- const submissionValues = readFormValues();
1019
- const clientErrors = validateVisibleFields(carryForward.fields, submissionValues);
1020
- if (Object.keys(clientErrors).length) {
1021
- setErrorByTarget(clientErrors);
1022
- setInvalidByTarget(Object.fromEntries(Object.keys(clientErrors).map((target) => [target, true])));
1023
- showFieldErrors(clientErrors);
1024
- setSubmitFailure(SubmitFailure.VALIDATION);
1025
- return;
1026
- }
1027
- await runFormSubmit({
1028
- flight: submitFlight,
1029
- isSubmitting,
1030
- setIsSubmitting,
1031
- ensureSessionFresh,
1032
- submitForm,
1033
- formId: carryForward.formId,
1034
- submissionValues,
1035
- presentSubmitResult,
1036
- navigate: (path) => window.location.assign(path),
1037
- onNetworkFailure: () => {
1038
- setStatusMessage(SUBMIT_FAILED_MESSAGE);
1039
- 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;
1040
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
1041
1276
  });
1042
1277
  }
1043
1278
  function presentSubmitResult(result) {
@@ -1045,12 +1280,16 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
1045
1280
  const rejection = presentSubmitRejection(result, carryForward.fields);
1046
1281
  setErrorByTarget(rejection.errorByTarget);
1047
1282
  setInvalidByTarget(rejection.invalidByTarget);
1283
+ setRuleByTarget(Object.fromEntries(Object.keys(rejection.errorByTarget).map((target) => [
1284
+ target,
1285
+ FieldRule.SERVER
1286
+ ])));
1048
1287
  setStatusMessage(rejection.statusMessage);
1049
1288
  setSubmitFailure(result.reason ?? SubmitFailure.UNKNOWN);
1050
1289
  showFieldErrors(rejection.errorByTarget);
1290
+ reportSubmitRejection(result);
1051
1291
  return void 0;
1052
1292
  }
1053
- showSubmitted();
1054
1293
  const redirectTo = result.redirectTo;
1055
1294
  const redirectPath = toSitePath(redirectTo);
1056
1295
  if (redirectTo !== void 0 && !redirectPath) {
@@ -1058,8 +1297,19 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
1058
1297
  formId: carryForward.formId
1059
1298
  });
1060
1299
  }
1300
+ if (!redirectPath)
1301
+ showSubmitted();
1302
+ reportSubmitOutcome(redirectPath === void 0 ? { outcome: "succeeded" } : { outcome: "succeeded", redirectTo: redirectPath });
1061
1303
  return redirectPath;
1062
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
+ }
1063
1313
  return {
1064
1314
  render: () => buildFormViewState(carryForward.fields, {
1065
1315
  isSubmitting: isSubmitting(),
@@ -1067,6 +1317,7 @@ function WixFormInteractive(_props, refs, _viewStateSignals, carryForward) {
1067
1317
  loadError: carryForward.loadError ?? "",
1068
1318
  errorByTarget: errorByTarget(),
1069
1319
  invalidByTarget: invalidByTarget(),
1320
+ ruleByTarget: ruleByTarget(),
1070
1321
  valueByTarget: values(),
1071
1322
  isSubmitted: isSubmitted(),
1072
1323
  submitFailure: submitFailure()
@@ -1121,15 +1372,45 @@ function seedControlsIntoStore(refs, fields, dirtyTargets, setValues) {
1121
1372
  return control.exec$((element) => readControlValue(element)).then((value) => {
1122
1373
  seeds.push({
1123
1374
  target: field.target,
1124
- value: value ?? "",
1375
+ value,
1125
1376
  dirty: dirtyTargets.has(field.target)
1126
1377
  });
1127
1378
  });
1128
1379
  });
1129
1380
  return Promise.all(reads).then(() => {
1130
- setValues((current) => seedControlValues(current, seeds));
1381
+ let changedTargets = [];
1382
+ setValues((current) => {
1383
+ const seeded = seedControlValues(current, seeds);
1384
+ changedTargets = seeded.changedTargets;
1385
+ return seeded.store;
1386
+ });
1387
+ return changedTargets;
1131
1388
  });
1132
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
+ }
1133
1414
  function wireHoneypot(refs, record) {
1134
1415
  refs.honeypot?.oninput(({ event }) => {
1135
1416
  record(event.target.value ?? "");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jay-framework/wix-bookings",
3
- "version": "0.26.1",
3
+ "version": "0.26.3",
4
4
  "type": "module",
5
5
  "description": "Wix Bookings package for Jay Framework — browse services, pick slots, submit participant details, complete checkout",
6
6
  "license": "Apache-2.0",
@@ -43,15 +43,15 @@
43
43
  "test": "vitest run"
44
44
  },
45
45
  "dependencies": {
46
- "@jay-framework/component": "^0.26.1",
47
- "@jay-framework/fullstack-component": "^0.26.1",
48
- "@jay-framework/reactive": "^0.26.1",
49
- "@jay-framework/runtime": "^0.26.1",
50
- "@jay-framework/secure": "^0.26.1",
51
- "@jay-framework/stack-client-runtime": "^0.26.1",
52
- "@jay-framework/stack-server-runtime": "^0.26.1",
53
- "@jay-framework/wix-forms": "^0.26.1",
54
- "@jay-framework/wix-server-client": "^0.26.1",
46
+ "@jay-framework/component": "^0.26.3",
47
+ "@jay-framework/fullstack-component": "^0.26.3",
48
+ "@jay-framework/reactive": "^0.26.3",
49
+ "@jay-framework/runtime": "^0.26.3",
50
+ "@jay-framework/secure": "^0.26.3",
51
+ "@jay-framework/stack-client-runtime": "^0.26.3",
52
+ "@jay-framework/stack-server-runtime": "^0.26.3",
53
+ "@jay-framework/wix-forms": "^0.26.3",
54
+ "@jay-framework/wix-server-client": "^0.26.3",
55
55
  "@wix/sdk": "^1.21.5",
56
56
  "js-yaml": "^4.1.0"
57
57
  },
@@ -59,10 +59,10 @@
59
59
  "@babel/core": "^7.23.7",
60
60
  "@babel/preset-env": "^7.23.8",
61
61
  "@babel/preset-typescript": "^7.23.3",
62
- "@jay-framework/compiler-jay-stack": "^0.26.1",
63
- "@jay-framework/jay-cli": "^0.26.1",
64
- "@jay-framework/jay-stack-cli": "^0.26.1",
65
- "@jay-framework/vite-plugin": "^0.26.1",
62
+ "@jay-framework/compiler-jay-stack": "^0.26.3",
63
+ "@jay-framework/jay-cli": "^0.26.3",
64
+ "@jay-framework/jay-stack-cli": "^0.26.3",
65
+ "@jay-framework/vite-plugin": "^0.26.3",
66
66
  "@jay-framework/wix-dev-environment": "^0.6.12",
67
67
  "nodemon": "^3.0.3",
68
68
  "rimraf": "^5.0.5",