@anker-in/ui 0.1.19 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1670,6 +1670,61 @@ var QuestionnaireForm = ({
1670
1670
  const [agreementError, setAgreementError] = useState("");
1671
1671
  const [phoneCountryCodes, setPhoneCountryCodes] = useState({});
1672
1672
  const [otherInputs, setOtherInputs] = useState({});
1673
+ const prevHasBookedBeforeRef = useRef(false);
1674
+ const hasBookedBeforeInitialized = useRef(false);
1675
+ const hasBookedBeforeQuestion = questions.find(
1676
+ (q) => (q.key || q.id) === "has_booked_before"
1677
+ );
1678
+ const hasBookedBeforeQuestionId = hasBookedBeforeQuestion ? hasBookedBeforeQuestion.key || hasBookedBeforeQuestion.id || "" : "has_booked_before";
1679
+ const hasBookedBefore = responses[hasBookedBeforeQuestionId] === "option2";
1680
+ const hiddenQuestionIds = useMemo(() => {
1681
+ if (!hasBookedBefore) return [];
1682
+ return questions.filter((q) => q.is_default === false).map((q) => q.key || q.id || "");
1683
+ }, [hasBookedBefore, questions]);
1684
+ const visibleQuestions = useMemo(() => {
1685
+ return questions.filter((question) => {
1686
+ if (hasBookedBefore && question.is_default === false) {
1687
+ return false;
1688
+ }
1689
+ return true;
1690
+ });
1691
+ }, [questions, hasBookedBefore]);
1692
+ useEffect(() => {
1693
+ if (hasBookedBeforeInitialized.current) return;
1694
+ if (hasBookedBeforeQuestion && hasBookedBeforeQuestionId) {
1695
+ setResponses((prev) => {
1696
+ if (!prev[hasBookedBeforeQuestionId]) {
1697
+ return {
1698
+ ...prev,
1699
+ [hasBookedBeforeQuestionId]: "option1"
1700
+ };
1701
+ }
1702
+ return prev;
1703
+ });
1704
+ hasBookedBeforeInitialized.current = true;
1705
+ }
1706
+ }, [hasBookedBeforeQuestion, hasBookedBeforeQuestionId]);
1707
+ useEffect(() => {
1708
+ if (hasBookedBefore && !prevHasBookedBeforeRef.current) {
1709
+ if (hiddenQuestionIds.length > 0) {
1710
+ setResponses((prev) => {
1711
+ const newResponses = { ...prev };
1712
+ hiddenQuestionIds.forEach((questionId) => {
1713
+ delete newResponses[questionId];
1714
+ });
1715
+ return newResponses;
1716
+ });
1717
+ setErrors((prev) => {
1718
+ const newErrors = { ...prev };
1719
+ hiddenQuestionIds.forEach((questionId) => {
1720
+ delete newErrors[questionId];
1721
+ });
1722
+ return newErrors;
1723
+ });
1724
+ }
1725
+ }
1726
+ prevHasBookedBeforeRef.current = hasBookedBefore;
1727
+ }, [hasBookedBefore, hiddenQuestionIds]);
1673
1728
  const defaultCountryCodes = [
1674
1729
  { code: "+1", country: "US/CA" },
1675
1730
  { code: "+86", country: "CN" },
@@ -1685,7 +1740,7 @@ var QuestionnaireForm = ({
1685
1740
  ];
1686
1741
  const countryCodes = pageData?.countryCodes || defaultCountryCodes;
1687
1742
  const getQuestionId = (question) => {
1688
- return question.id || question.key || "";
1743
+ return question.key || question.id || "";
1689
1744
  };
1690
1745
  const handleChange = (questionId, value) => {
1691
1746
  setResponses((prev) => ({
@@ -1739,7 +1794,7 @@ var QuestionnaireForm = ({
1739
1794
  };
1740
1795
  const validateForm = () => {
1741
1796
  const newErrors = {};
1742
- questions.forEach((question) => {
1797
+ visibleQuestions.forEach((question) => {
1743
1798
  if (question.required) {
1744
1799
  const questionId = getQuestionId(question);
1745
1800
  const value = responses[questionId];
@@ -2052,7 +2107,7 @@ var QuestionnaireForm = ({
2052
2107
  }
2053
2108
  };
2054
2109
  return /* @__PURE__ */ jsxs("div", { className: "pb-[80px] light", children: [
2055
- questions && questions.length > 0 && /* @__PURE__ */ jsx("div", { className: "bg-white rounded-[12px] border border-[#E4E5E6] p-8 dark:bg-white dark:border-[#E4E5E6] l:p-4", children: /* @__PURE__ */ jsx("form", { onSubmit: handleSubmit, className: cn("space-y-6", className), children: questions.map((question) => {
2110
+ visibleQuestions && visibleQuestions.length > 0 && /* @__PURE__ */ jsx("div", { className: "bg-white rounded-[12px] border border-[#E4E5E6] p-8 dark:bg-white dark:border-[#E4E5E6] l:p-4", children: /* @__PURE__ */ jsx("form", { onSubmit: handleSubmit, className: cn("space-y-6", className), children: visibleQuestions.map((question) => {
2056
2111
  const questionId = getQuestionId(question);
2057
2112
  const hasError = !!errors[questionId];
2058
2113
  return /* @__PURE__ */ jsxs(
@@ -2732,6 +2787,7 @@ var DateTimePicker = ({
2732
2787
  const customerEmail = responses.email || responses.customer_email || "";
2733
2788
  const customerPhone = responses.phone || responses.customer_phone_number || "";
2734
2789
  const customerPhoneCode = responses.phone_code || responses.customer_phone_code || "+1";
2790
+ const hasBookedBefore = responses.has_booked_before === "option2" ? 1 : 0;
2735
2791
  if (selectedTimeslotId) {
2736
2792
  const result = await createBooking({
2737
2793
  demoroom_code: demoRoomCode,
@@ -2741,7 +2797,8 @@ var DateTimePicker = ({
2741
2797
  customer_name: customerName,
2742
2798
  customer_email: customerEmail,
2743
2799
  customer_phone_code: customerPhoneCode,
2744
- customer_phone_number: customerPhone
2800
+ customer_phone_number: customerPhone,
2801
+ has_booked_before: hasBookedBefore
2745
2802
  });
2746
2803
  if (result && onBookingSuccess) {
2747
2804
  onBookingSuccess(result?.cancel_token);