@pinhadev/pinha-quiz-builder-pkg 0.1.0 → 0.1.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.js CHANGED
@@ -4074,7 +4074,7 @@ function PresetOrCustomColorField({
4074
4074
  }
4075
4075
 
4076
4076
  // src/v2/QuizV2.tsx
4077
- var import_react13 = require("react");
4077
+ var import_react13 = __toESM(require("react"));
4078
4078
  var import_formik3 = require("formik");
4079
4079
  var import_styled_components9 = require("styled-components");
4080
4080
 
@@ -6196,6 +6196,213 @@ var EmptyState = import_styled_components8.default.div`
6196
6196
 
6197
6197
  // src/v2/QuizV2.tsx
6198
6198
  var import_jsx_runtime54 = require("react/jsx-runtime");
6199
+ var QuizContent = import_react13.default.memo(function QuizContent2({
6200
+ data,
6201
+ pages,
6202
+ currentPageIndex,
6203
+ setCurrentPageIndex,
6204
+ safePageIndex,
6205
+ headerSectionIds,
6206
+ footerSectionIds,
6207
+ rootSectionIds,
6208
+ sectionsById,
6209
+ collectInputFieldNames,
6210
+ withDefaultTracking,
6211
+ onSubmit,
6212
+ layoutMaxWidth,
6213
+ quizTitle,
6214
+ quizDescription
6215
+ }) {
6216
+ const { validateForm, setTouched, submitForm, values } = (0, import_formik3.useFormikContext)();
6217
+ const { trackEvent, trackFormSubmission } = useDefaultFormTracking();
6218
+ const handleAction = (0, import_react13.useCallback)(
6219
+ async (action) => {
6220
+ if (withDefaultTracking) {
6221
+ trackEvent({
6222
+ event_type: "action_trigger",
6223
+ step: currentPageIndex,
6224
+ metadata: {
6225
+ ...values,
6226
+ action_type: action.type,
6227
+ target_page_index: action.targetPageIndex
6228
+ }
6229
+ });
6230
+ }
6231
+ switch (action.type) {
6232
+ case "goToPage":
6233
+ if (action.targetPageIndex != null) {
6234
+ const allSectionIds = [
6235
+ ...headerSectionIds,
6236
+ ...rootSectionIds,
6237
+ ...footerSectionIds
6238
+ ];
6239
+ const currentPageFieldNames = collectInputFieldNames(allSectionIds);
6240
+ const validationErrors = await validateForm();
6241
+ const touchedFields = {};
6242
+ currentPageFieldNames.forEach((fieldName) => {
6243
+ touchedFields[fieldName] = true;
6244
+ });
6245
+ setTouched(touchedFields);
6246
+ const hasErrors = currentPageFieldNames.some(
6247
+ (fieldName) => validationErrors[fieldName]
6248
+ );
6249
+ if (hasErrors) {
6250
+ if (withDefaultTracking) {
6251
+ trackEvent({
6252
+ event_type: "validation_error",
6253
+ step: currentPageIndex,
6254
+ metadata: {
6255
+ ...values,
6256
+ errors: validationErrors
6257
+ }
6258
+ });
6259
+ }
6260
+ setTimeout(() => {
6261
+ const firstErrorField = document.querySelector('[data-error="true"]');
6262
+ if (firstErrorField) {
6263
+ firstErrorField.scrollIntoView({ behavior: "smooth", block: "center" });
6264
+ }
6265
+ }, 100);
6266
+ return;
6267
+ }
6268
+ const targetIndex = Math.max(0, Math.min(action.targetPageIndex, pages.length - 1));
6269
+ if (withDefaultTracking) {
6270
+ trackEvent({
6271
+ event_type: "step_complete",
6272
+ step: currentPageIndex,
6273
+ metadata: values
6274
+ });
6275
+ }
6276
+ setCurrentPageIndex(targetIndex);
6277
+ if (withDefaultTracking) {
6278
+ trackEvent({
6279
+ event_type: "step_view",
6280
+ step: targetIndex,
6281
+ metadata: values
6282
+ });
6283
+ }
6284
+ }
6285
+ break;
6286
+ case "toThankYouPage":
6287
+ const thankYouPageIndex = pages.findIndex((p) => p.type === "thankYou");
6288
+ if (withDefaultTracking) {
6289
+ trackEvent({
6290
+ event_type: "form_submission",
6291
+ step: currentPageIndex,
6292
+ metadata: values
6293
+ });
6294
+ trackFormSubmission(values, data.id);
6295
+ }
6296
+ if (thankYouPageIndex !== -1) {
6297
+ setCurrentPageIndex(thankYouPageIndex);
6298
+ await submitForm();
6299
+ } else {
6300
+ await submitForm();
6301
+ }
6302
+ break;
6303
+ case "toRedirectionPage":
6304
+ const redirectionPageIndex = pages.findIndex((p) => p.type === "redirection");
6305
+ if (withDefaultTracking) {
6306
+ trackEvent({
6307
+ event_type: "redirection_trigger",
6308
+ step: currentPageIndex,
6309
+ metadata: values
6310
+ });
6311
+ }
6312
+ if (redirectionPageIndex !== -1) {
6313
+ setCurrentPageIndex(redirectionPageIndex);
6314
+ if (withDefaultTracking) {
6315
+ trackEvent({
6316
+ event_type: "step_view",
6317
+ step: redirectionPageIndex,
6318
+ metadata: values
6319
+ });
6320
+ }
6321
+ }
6322
+ break;
6323
+ default:
6324
+ break;
6325
+ }
6326
+ },
6327
+ [validateForm, setTouched, submitForm, headerSectionIds, rootSectionIds, footerSectionIds, collectInputFieldNames, pages, setCurrentPageIndex, trackEvent, currentPageIndex, values, withDefaultTracking, data.id, trackFormSubmission]
6328
+ );
6329
+ const handleButtonClick = (0, import_react13.useCallback)(
6330
+ (button) => {
6331
+ if (!button.onClick) return;
6332
+ handleAction(button.onClick);
6333
+ },
6334
+ [handleAction]
6335
+ );
6336
+ const handleSelectAction = (0, import_react13.useCallback)(
6337
+ (action) => {
6338
+ handleAction(action);
6339
+ },
6340
+ [handleAction]
6341
+ );
6342
+ (0, import_react13.useEffect)(() => {
6343
+ const currentPage2 = pages[safePageIndex];
6344
+ if (currentPage2?.type === "thankYou" && onSubmit) {
6345
+ submitForm();
6346
+ }
6347
+ }, [safePageIndex, pages, onSubmit, submitForm]);
6348
+ const theme2 = (0, import_styled_components9.useTheme)();
6349
+ const currentPage = pages[safePageIndex] ?? null;
6350
+ return /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(import_formik3.Form, { children: /* @__PURE__ */ (0, import_jsx_runtime54.jsxs)(QuizContainer, { $maxWidth: layoutMaxWidth === "medium" || !layoutMaxWidth ? "medium" : layoutMaxWidth, children: [
6351
+ (quizTitle || quizDescription) && /* @__PURE__ */ (0, import_jsx_runtime54.jsxs)(Header, { children: [
6352
+ quizTitle && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(Title, { children: quizTitle }),
6353
+ quizDescription && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(Description, { children: quizDescription })
6354
+ ] }),
6355
+ /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(SectionGroup, { id: "header-generator", children: headerSectionIds.map((id) => /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6356
+ SectionTreeRenderer,
6357
+ {
6358
+ sectionId: id,
6359
+ sectionsById,
6360
+ onButtonClick: handleButtonClick,
6361
+ onSelectAction: handleSelectAction,
6362
+ isBuilderMode: false,
6363
+ totalPages: pages.length,
6364
+ currentPageIndex
6365
+ },
6366
+ id
6367
+ )) }),
6368
+ /* @__PURE__ */ (0, import_jsx_runtime54.jsxs)(SectionGroup, { id: "main-content-generator", children: [
6369
+ currentPage?.type === "redirection" && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6370
+ RedirectionPage,
6371
+ {
6372
+ redirectUrl: currentPage.redirectUrl || "https://example.com",
6373
+ countdownSeconds: currentPage.redirectCountdownSeconds
6374
+ }
6375
+ ),
6376
+ currentPage?.type === "thankYou" && rootSectionIds.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(ThankYouPage, {}),
6377
+ rootSectionIds.length === 0 && currentPage?.type !== "redirection" && currentPage?.type !== "thankYou" ? /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(EmptyState, { children: "This page has no content." }) : rootSectionIds.map((id) => /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6378
+ SectionTreeRenderer,
6379
+ {
6380
+ sectionId: id,
6381
+ sectionsById,
6382
+ onButtonClick: handleButtonClick,
6383
+ onSelectAction: handleSelectAction,
6384
+ isBuilderMode: false,
6385
+ totalPages: pages.length,
6386
+ currentPageIndex
6387
+ },
6388
+ id
6389
+ ))
6390
+ ] }),
6391
+ /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(SectionGroup, { id: "footer-generator", children: footerSectionIds.map((id) => /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6392
+ SectionTreeRenderer,
6393
+ {
6394
+ sectionId: id,
6395
+ sectionsById,
6396
+ onButtonClick: handleButtonClick,
6397
+ onSelectAction: handleSelectAction,
6398
+ isBuilderMode: false,
6399
+ totalPages: pages.length,
6400
+ currentPageIndex
6401
+ },
6402
+ id
6403
+ )) })
6404
+ ] }) });
6405
+ });
6199
6406
  function QuizV2Content({
6200
6407
  data,
6201
6408
  onSubmit,
@@ -6205,13 +6412,13 @@ function QuizV2Content({
6205
6412
  }) {
6206
6413
  const [internalPageIndex, setInternalPageIndex] = (0, import_react13.useState)(0);
6207
6414
  const currentPageIndex = controlledPageIndex !== void 0 ? controlledPageIndex : internalPageIndex;
6208
- const setCurrentPageIndex = (index) => {
6415
+ const setCurrentPageIndex = (0, import_react13.useCallback)((index) => {
6209
6416
  if (controlledPageIndex !== void 0 && onPageChange) {
6210
6417
  onPageChange(index);
6211
6418
  } else {
6212
6419
  setInternalPageIndex(index);
6213
6420
  }
6214
- };
6421
+ }, [controlledPageIndex, onPageChange]);
6215
6422
  const pages = data.pages ?? [];
6216
6423
  const sections = data.sections ?? [];
6217
6424
  const layouts = data.layouts ?? [];
@@ -6222,10 +6429,10 @@ function QuizV2Content({
6222
6429
  const safePageIndex = pages.length === 0 ? 0 : Math.min(currentPageIndex, pages.length - 1);
6223
6430
  const currentPage = pages[safePageIndex] ?? null;
6224
6431
  const resolvedLayout = resolveLayoutForPage(layouts, currentPage);
6225
- const headerSectionIds = resolvedLayout?.headerSectionIds ?? [];
6226
- const footerSectionIds = resolvedLayout?.footerSectionIds ?? [];
6227
- const rootSectionIds = currentPage?.sectionIds ?? [];
6228
- const sectionsById = getSectionsById(sections);
6432
+ const headerSectionIds = (0, import_react13.useMemo)(() => resolvedLayout?.headerSectionIds ?? [], [resolvedLayout]);
6433
+ const footerSectionIds = (0, import_react13.useMemo)(() => resolvedLayout?.footerSectionIds ?? [], [resolvedLayout]);
6434
+ const rootSectionIds = (0, import_react13.useMemo)(() => currentPage?.sectionIds ?? [], [currentPage]);
6435
+ const sectionsById = (0, import_react13.useMemo)(() => getSectionsById(sections), [sections]);
6229
6436
  const initialValues = (0, import_react13.useMemo)(() => {
6230
6437
  const values = {};
6231
6438
  sections.forEach((section) => {
@@ -6278,213 +6485,6 @@ function QuizV2Content({
6278
6485
  },
6279
6486
  [sectionsById]
6280
6487
  );
6281
- function QuizContent({
6282
- data: data2,
6283
- pages: pages2,
6284
- currentPageIndex: currentPageIndex2,
6285
- setCurrentPageIndex: setCurrentPageIndex2,
6286
- safePageIndex: safePageIndex2,
6287
- headerSectionIds: headerSectionIds2,
6288
- footerSectionIds: footerSectionIds2,
6289
- rootSectionIds: rootSectionIds2,
6290
- sectionsById: sectionsById2,
6291
- collectInputFieldNames: collectInputFieldNames2,
6292
- withDefaultTracking: withDefaultTracking2,
6293
- onSubmit: onSubmit2,
6294
- layoutMaxWidth: layoutMaxWidth2,
6295
- quizTitle: quizTitle2,
6296
- quizDescription: quizDescription2
6297
- }) {
6298
- const { validateForm, setTouched, submitForm, values } = (0, import_formik3.useFormikContext)();
6299
- const { trackEvent, trackFormSubmission } = useDefaultFormTracking();
6300
- const handleAction = (0, import_react13.useCallback)(
6301
- async (action) => {
6302
- if (withDefaultTracking2) {
6303
- trackEvent({
6304
- event_type: "action_trigger",
6305
- step: currentPageIndex2,
6306
- metadata: {
6307
- ...values,
6308
- action_type: action.type,
6309
- target_page_index: action.targetPageIndex
6310
- }
6311
- });
6312
- }
6313
- switch (action.type) {
6314
- case "goToPage":
6315
- if (action.targetPageIndex != null) {
6316
- const allSectionIds = [
6317
- ...headerSectionIds2,
6318
- ...rootSectionIds2,
6319
- ...footerSectionIds2
6320
- ];
6321
- const currentPageFieldNames = collectInputFieldNames2(allSectionIds);
6322
- const validationErrors = await validateForm();
6323
- const touchedFields = {};
6324
- currentPageFieldNames.forEach((fieldName) => {
6325
- touchedFields[fieldName] = true;
6326
- });
6327
- setTouched(touchedFields);
6328
- const hasErrors = currentPageFieldNames.some(
6329
- (fieldName) => validationErrors[fieldName]
6330
- );
6331
- if (hasErrors) {
6332
- if (withDefaultTracking2) {
6333
- trackEvent({
6334
- event_type: "validation_error",
6335
- step: currentPageIndex2,
6336
- metadata: {
6337
- ...values,
6338
- errors: validationErrors
6339
- }
6340
- });
6341
- }
6342
- setTimeout(() => {
6343
- const firstErrorField = document.querySelector('[data-error="true"]');
6344
- if (firstErrorField) {
6345
- firstErrorField.scrollIntoView({ behavior: "smooth", block: "center" });
6346
- }
6347
- }, 100);
6348
- return;
6349
- }
6350
- const targetIndex = Math.max(0, Math.min(action.targetPageIndex, pages2.length - 1));
6351
- if (withDefaultTracking2) {
6352
- trackEvent({
6353
- event_type: "step_complete",
6354
- step: currentPageIndex2,
6355
- metadata: values
6356
- });
6357
- }
6358
- setCurrentPageIndex2(targetIndex);
6359
- if (withDefaultTracking2) {
6360
- trackEvent({
6361
- event_type: "step_view",
6362
- step: targetIndex,
6363
- metadata: values
6364
- });
6365
- }
6366
- }
6367
- break;
6368
- case "toThankYouPage":
6369
- const thankYouPageIndex = pages2.findIndex((p) => p.type === "thankYou");
6370
- if (withDefaultTracking2) {
6371
- trackEvent({
6372
- event_type: "form_submission",
6373
- step: currentPageIndex2,
6374
- metadata: values
6375
- });
6376
- trackFormSubmission(values, data2.id);
6377
- }
6378
- if (thankYouPageIndex !== -1) {
6379
- setCurrentPageIndex2(thankYouPageIndex);
6380
- await submitForm();
6381
- } else {
6382
- await submitForm();
6383
- }
6384
- break;
6385
- case "toRedirectionPage":
6386
- const redirectionPageIndex = pages2.findIndex((p) => p.type === "redirection");
6387
- if (withDefaultTracking2) {
6388
- trackEvent({
6389
- event_type: "redirection_trigger",
6390
- step: currentPageIndex2,
6391
- metadata: values
6392
- });
6393
- }
6394
- if (redirectionPageIndex !== -1) {
6395
- setCurrentPageIndex2(redirectionPageIndex);
6396
- if (withDefaultTracking2) {
6397
- trackEvent({
6398
- event_type: "step_view",
6399
- step: redirectionPageIndex,
6400
- metadata: values
6401
- });
6402
- }
6403
- }
6404
- break;
6405
- default:
6406
- break;
6407
- }
6408
- },
6409
- [validateForm, setTouched, submitForm, headerSectionIds2, rootSectionIds2, footerSectionIds2, collectInputFieldNames2, pages2, setCurrentPageIndex2, trackEvent, currentPageIndex2, values, withDefaultTracking2, data2.id, trackFormSubmission]
6410
- );
6411
- const handleButtonClick = (0, import_react13.useCallback)(
6412
- (button) => {
6413
- if (!button.onClick) return;
6414
- handleAction(button.onClick);
6415
- },
6416
- [handleAction]
6417
- );
6418
- const handleSelectAction = (0, import_react13.useCallback)(
6419
- (action) => {
6420
- handleAction(action);
6421
- },
6422
- [handleAction]
6423
- );
6424
- (0, import_react13.useEffect)(() => {
6425
- const currentPage3 = pages2[safePageIndex2];
6426
- if (currentPage3?.type === "thankYou" && onSubmit2) {
6427
- submitForm();
6428
- }
6429
- }, [safePageIndex2, pages2, onSubmit2, submitForm]);
6430
- const theme2 = (0, import_styled_components9.useTheme)();
6431
- const currentPage2 = pages2[safePageIndex2] ?? null;
6432
- return /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(import_formik3.Form, { children: /* @__PURE__ */ (0, import_jsx_runtime54.jsxs)(QuizContainer, { $maxWidth: layoutMaxWidth2 === "medium" || !layoutMaxWidth2 ? "medium" : layoutMaxWidth2, children: [
6433
- (quizTitle2 || quizDescription2) && /* @__PURE__ */ (0, import_jsx_runtime54.jsxs)(Header, { children: [
6434
- quizTitle2 && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(Title, { children: quizTitle2 }),
6435
- quizDescription2 && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(Description, { children: quizDescription2 })
6436
- ] }),
6437
- /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(SectionGroup, { id: "header-generator", children: headerSectionIds2.map((id) => /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6438
- SectionTreeRenderer,
6439
- {
6440
- sectionId: id,
6441
- sectionsById: sectionsById2,
6442
- onButtonClick: handleButtonClick,
6443
- onSelectAction: handleSelectAction,
6444
- isBuilderMode: false,
6445
- totalPages: pages2.length,
6446
- currentPageIndex: currentPageIndex2
6447
- },
6448
- id
6449
- )) }),
6450
- /* @__PURE__ */ (0, import_jsx_runtime54.jsxs)(SectionGroup, { id: "main-content-generator", children: [
6451
- currentPage2?.type === "redirection" && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6452
- RedirectionPage,
6453
- {
6454
- redirectUrl: currentPage2.redirectUrl || "https://example.com",
6455
- countdownSeconds: currentPage2.redirectCountdownSeconds
6456
- }
6457
- ),
6458
- currentPage2?.type === "thankYou" && rootSectionIds2.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(ThankYouPage, {}),
6459
- rootSectionIds2.length === 0 && currentPage2?.type !== "redirection" && currentPage2?.type !== "thankYou" ? /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(EmptyState, { children: "This page has no content." }) : rootSectionIds2.map((id) => /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6460
- SectionTreeRenderer,
6461
- {
6462
- sectionId: id,
6463
- sectionsById: sectionsById2,
6464
- onButtonClick: handleButtonClick,
6465
- onSelectAction: handleSelectAction,
6466
- isBuilderMode: false,
6467
- totalPages: pages2.length,
6468
- currentPageIndex: currentPageIndex2
6469
- },
6470
- id
6471
- ))
6472
- ] }),
6473
- /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(SectionGroup, { id: "footer-generator", children: footerSectionIds2.map((id) => /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(
6474
- SectionTreeRenderer,
6475
- {
6476
- sectionId: id,
6477
- sectionsById: sectionsById2,
6478
- onButtonClick: handleButtonClick,
6479
- onSelectAction: handleSelectAction,
6480
- isBuilderMode: false,
6481
- totalPages: pages2.length,
6482
- currentPageIndex: currentPageIndex2
6483
- },
6484
- id
6485
- )) })
6486
- ] }) });
6487
- }
6488
6488
  if (pages.length === 0) {
6489
6489
  return /* @__PURE__ */ (0, import_jsx_runtime54.jsx)(EmptyState, { children: "No pages yet." });
6490
6490
  }