/** * "Quiz": answers score points, right answers one point each, or answers count toward types * (personality quiz), and visitors see the result page they reach after the contact step. Shown in * the Settings workspace; the builder keeps the points calculation and the paths to the results in * step (src/quiz.ts). All questions and their scoring are listed here, so a quiz is set up in one place. */ import React from 'react'; import type { FormDocument } from '@funnelforms/form-schema'; import { findNode, nodeName } from '../document'; import { addQuizCategory, addQuizResult, choiceFields, counts, moveQuizCategory, quizMax, quizQuestions, quizResults, removeQuizCategory, removeQuizResult, renameQuizCategory, resultText, setAnswerPoints, setAnswerScore, setQuizKind, setQuizResultMin, setRightAnswer, startQuiz, stopQuiz, type QuizKind, type QuizQuestion, } from '../quiz'; import type { Recipe } from '../store'; import { useEnv } from './context'; import { AskAiButton } from './AiForm'; import { fieldTitle } from './describe'; import { __, sprintf } from './i18n'; import { Button, icons, Notice, Select, Toggle } from './ui'; function useChange(): ( recipe: Recipe, key?: string ) => void { return useEnv().store.getState().change; } const KINDS = (): Array<{ value: QuizKind; title: string; text: string }> => [ { value: 'points', title: __( 'Points per answer', 'funnelforms-pro' ), text: __( 'Every answer is worth points, such as a lead check or a self-test.', 'funnelforms-pro' ) }, { value: 'knowledge', title: __( 'Right and wrong answers', 'funnelforms-pro' ), text: __( 'One point per right answer, such as a knowledge test.', 'funnelforms-pro' ) }, { value: 'personality', title: __( 'Personality types', 'funnelforms-pro' ), text: __( 'Answers count toward types; the type with the most points is the result.', 'funnelforms-pro' ) }, ]; export function QuizSection( { doc }: { doc: FormDocument } ) { const { readOnly, ctx, store } = useEnv(); const change = useChange(); const quiz = doc.settings.quiz; const start = ( kind: QuizKind ): void => change( ( d ) => { startQuiz( d, kind, 'personality' === kind ? __( 'Quiz type', 'funnelforms-pro' ) : __( 'Quiz points', 'funnelforms-pro' ) ); if ( 'personality' === kind ) { addQuizCategory( d, ctx, sprintf( __( 'Type %d', 'funnelforms-pro' ), 1 ) ); addQuizCategory( d, ctx, sprintf( __( 'Type %d', 'funnelforms-pro' ), 2 ) ); } } ); if ( ! quiz ) { return (

{ __( 'Quiz', 'funnelforms-pro' ) }

{ __( 'Make the form a quiz: answers score points, and after the contact step visitors see the result their points reach.', 'funnelforms-pro' ) }

{ KINDS().map( ( k ) => ( ) ) }
); } const questions = quizQuestions( doc ); const max = quizMax( doc ); const results = quizResults( doc ); const contact = doc.nodes.some( ( n ) => 'contact' === n.type ); const knowledge = 'knowledge' === quiz.kind; const personality = 'personality' === quiz.kind; const pageName = ( id: string ): string => { const node = findNode( doc, id ); return ( node ? nodeName( node, ctx.locale ) : '' ) || __( 'Without a title', 'funnelforms-pro' ); }; return (

{ __( 'Quiz', 'funnelforms-pro' ) }

change( ( d ) => renameQuizCategory( d, c.id, e.target.value ), 'quiz-type:' + c.id ) } /> { result ? ( ) : ( { __( 'No result page', 'funnelforms-pro' ) } ) } ) : null } { personality ? null : ( <>

{ __( 'Results', 'funnelforms-pro' ) }

{ contact ? (

{ __( 'After sending, visitors see the result with the most points they reach. Use {{calc:…}} and {{quiz:max}} in its text for their points.', 'funnelforms-pro' ).replace( '{{calc:…}}', `{{calc:${ quiz.calculation }}}` ) }

) : ( { __( 'Results show after the contact step. Add a contact step first.', 'funnelforms-pro' ) } ) } ) }
{ personality ? null : ( ) }
); } /** Every choice question with its answers and what they score, to set a quiz up in one place. */ function QuestionTable( { doc, kind }: { doc: FormDocument; kind: QuizKind } ) { const { readOnly, ctx, store } = useEnv(); const change = useChange(); const questions = choiceFields( doc ); const categories = doc.settings.quiz?.categories ?? []; const optionLabel = ( q: QuizQuestion, i: number ): string => { const o = q.options[ i ]; return ( o ? o.label[ ctx.locale ] ?? Object.values( o.label )[ 0 ] ?? o.value : '' ).replace( /<[^>]*>/g, '' ) || sprintf( __( 'Answer %d', 'funnelforms-pro' ), i + 1 ); }; const stepOf = ( fieldId: string ): string | undefined => doc.nodes.find( ( n ) => 'step' === n.type && n.fields.some( ( f ) => f.id === fieldId ) )?.id; if ( ! questions.length ) { return ( <>

{ __( 'Questions', 'funnelforms-pro' ) }

{ __( 'Add steps with choices or dropdowns; their answers score here.', 'funnelforms-pro' ) }

); } return ( <>

{ __( 'Questions', 'funnelforms-pro' ) }

{ questions.map( ( q ) => { const title = fieldTitle( q, ctx.locale ); return (
{ 'personality' === kind ? ( categories.map( ( c ) => ( ) ) ) : ( ) } { q.options.map( ( o, i ) => ( { 'personality' === kind ? ( categories.map( ( c ) => ( ) ) ) : 'knowledge' === kind ? ( ) : ( ) } ) ) }
{ __( 'Answer', 'funnelforms-pro' ) } { c.label } { 'knowledge' === kind ? __( 'Right', 'funnelforms-pro' ) : __( 'Points', 'funnelforms-pro' ) }
{ optionLabel( q, i ) } change( ( d ) => setAnswerScore( d, q.id, o.id, c.id, Number( e.target.value ) ), `quiz-score:${ o.id }:${ c.id }` ) } /> change( ( d ) => setRightAnswer( d, q.id, o.id, q.props.multiple ? e.target.checked : true ) ) } /> change( ( d ) => setAnswerPoints( d, q.id, o.id, '' === e.target.value ? undefined : Number( e.target.value ) ), 'calc-value:' + o.id ) } />
); } ) }
); }