2024-01-05 17:00:30 +00:00
import { Box , Typography , Button , useMediaQuery , TextField , Link , InputAdornment , useTheme } from "@mui/material" ;
2023-12-17 13:22:21 +00:00
import NameIcon from "@icons/ContactFormIcon/NameIcon" ;
import EmailIcon from "@icons/ContactFormIcon/EmailIcon" ;
import PhoneIcon from "@icons/ContactFormIcon/PhoneIcon" ;
import TextIcon from "@icons/ContactFormIcon/TextIcon" ;
import AddressIcon from "@icons/ContactFormIcon/AddressIcon" ;
2023-12-17 21:28:57 +00:00
import { useDebouncedCallback } from "use-debounce" ;
2023-12-16 14:55:56 +00:00
2023-12-17 13:22:21 +00:00
import CustomCheckbox from "@ui_kit/CustomCheckbox" ;
2023-12-17 21:28:57 +00:00
import { useEffect , useRef , useState } from "react" ;
2023-12-17 13:22:21 +00:00
import { useQuestionsStore } from "@root/quizData/store" ;
2023-12-16 14:55:56 +00:00
2023-12-23 18:03:34 +00:00
import { checkEmptyData } from "./tools/checkEmptyData" ;
2023-12-16 14:55:56 +00:00
import type { AnyTypedQuizQuestion } from "../../model/questionTypes/shared" ;
2023-12-17 21:28:57 +00:00
import { enqueueSnackbar } from "notistack" ;
import { sendFC } from "@api/quizRelase" ;
2023-12-21 19:22:06 +00:00
import { NameplateLogo } from "@icons/NameplateLogo" ;
2023-12-31 00:04:35 +00:00
import { modes } from "../../utils/themes/Publication/themePublication" ;
import { QuizQuestionResult } from "@model/questionTypes/result" ;
import { ApologyPage } from "./ApologyPage" ;
2023-12-16 14:55:56 +00:00
2024-01-05 17:00:30 +00:00
const EMAIL_REGEXP = / ^ ( ( [ ^ < > ( ) [ \ ] . , ; : \ s @ " ] + ( \ . [ ^ < > ( ) [ \ ] . , ; : \ s @ " ] + ) * ) | ( " . + " ) ) @ ( ( [ ^ < > ( ) [ \ ] . , ; : \ s @ " ] + \ . ) + [ ^ < > ( ) [ \ ] . , ; : \ s @ " ] { 2 , } ) $ / i u ;
2023-12-16 14:55:56 +00:00
type ContactFormProps = {
2023-12-29 00:58:19 +00:00
currentQuestion : any ;
2023-12-16 14:55:56 +00:00
showResultForm : boolean ;
setShowContactForm : ( show : boolean ) = > void ;
setShowResultForm : ( show : boolean ) = > void ;
} ;
2023-12-17 13:22:21 +00:00
const icons = [
2023-12-17 21:28:57 +00:00
{ type : "name" , icon : NameIcon , defaultText : "Введите имя" , defaultTitle : "имя" , backendName : "name" } ,
{ type : "email" , icon : EmailIcon , defaultText : "Введите Email" , defaultTitle : "Email" , backendName : "email" } ,
{ type : "phone" , icon : PhoneIcon , defaultText : "Введите номер телефона" , defaultTitle : "номер телефона" , backendName : "phone" } ,
{ type : "text" , icon : TextIcon , defaultText : "Введите фамилию" , defaultTitle : "фамилию" , backendName : "adress" } ,
{ type : "address" , icon : AddressIcon , defaultText : "Введите адрес" , defaultTitle : "адрес" , backendName : "adress" } ,
2023-12-17 13:22:21 +00:00
]
2023-12-16 14:55:56 +00:00
export const ContactForm = ( {
currentQuestion ,
showResultForm ,
setShowContactForm ,
setShowResultForm ,
} : ContactFormProps ) = > {
2024-01-05 17:00:30 +00:00
const theme = useTheme ( ) ;
2023-12-23 18:03:34 +00:00
const { settings , items } = useQuestionsStore ( )
2023-12-17 13:22:21 +00:00
const [ ready , setReady ] = useState ( false )
2023-12-24 11:52:09 +00:00
const [ name , setName ] = useState ( "" )
const [ email , setEmail ] = useState ( "" )
const [ phone , setPhone ] = useState ( "" )
const [ text , setText ] = useState ( "" )
const [ adress , setAdress ] = useState ( "" )
2023-12-16 14:55:56 +00:00
2023-12-31 00:04:35 +00:00
const fireOnce = useRef ( true )
const [ fire , setFire ] = useState ( false )
2024-01-06 23:56:13 +00:00
const isMobile = useMediaQuery ( theme . breakpoints . down ( 850 ) ) ;
2023-12-31 00:04:35 +00:00
2023-12-16 14:55:56 +00:00
const followNextForm = ( ) = > {
setShowContactForm ( false ) ;
setShowResultForm ( true ) ;
} ;
2023-12-29 00:58:19 +00:00
const mode = modes ;
2023-12-31 00:04:35 +00:00
//@ts-ignore
const resultQuestion : QuizQuestionResult = items . find ( ( question ) = > {
2023-12-23 18:03:34 +00:00
if ( settings ? . cfg . haveRoot ) { //ветвимся
return (
question . type === "result" &&
//@ts-ignore
question . content . rule . parentId === currentQuestion . content . id
)
} else { // не ветвимся
return (
question . type === "result" &&
question . content . rule . parentId === "line"
)
}
}
) ;
2023-12-24 11:52:09 +00:00
const inputHC = async ( ) = > {
const body = { }
//@ts-ignore
if ( name . length > 0 ) body . name = name
//@ts-ignore
if ( email . length > 0 ) body . email = email
//@ts-ignore
if ( phone . length > 0 ) body . phone = phone
//@ts-ignore
if ( text . length > 0 ) body . text = text
//@ts-ignore
if ( adress . length > 0 ) body . adress = adress
if ( Object . keys ( body ) . length > 0 ) {
try {
await sendFC ( {
questionId : resultQuestion?.id ,
body : body ,
//@ts-ignore
qid : settings.qid
} )
} catch ( e ) {
enqueueSnackbar ( "ответ не был засчитан" )
}
}
}
2024-01-06 23:56:13 +00:00
//@ts-ignore
let FCcopy : any = settings ? . cfg . formContact . fields || settings ? . cfg . formContact ;
let filteredFC : any = { }
for ( let i in FCcopy ) {
let field = FCcopy [ i ]
console . log ( filteredFC )
if ( field . used ) {
filteredFC [ i ] = field
}
}
let isWide = Object . keys ( filteredFC ) . length > 2
console . log ( isWide )
2023-12-24 11:52:09 +00:00
2023-12-31 00:04:35 +00:00
if ( ! resultQuestion ) return < ApologyPage message = "не получилось найти результат для этой ветки :(" / >
2023-12-16 14:55:56 +00:00
return (
2023-12-17 13:22:21 +00:00
< Box
sx = { {
display : "flex" ,
alignItems : "center" ,
justifyContent : "center" ,
2023-12-29 00:58:19 +00:00
backgroundColor : theme.palette.background.default ,
2024-01-06 23:56:13 +00:00
height : "100vh" ,
overflow : "auto" ,
2023-12-17 13:22:21 +00:00
} }
>
< Box
sx = { {
2024-01-06 23:56:13 +00:00
width : isWide && ! isMobile ? "100%" : ( isMobile ? undefined : "530px" ) ,
2023-12-29 21:31:21 +00:00
borderRadius : "4px" ,
2024-01-05 17:00:30 +00:00
height : "90vh" ,
2024-01-06 23:56:13 +00:00
display : isWide && ! isMobile ? "flex" : undefined
2023-12-17 13:22:21 +00:00
} }
>
2024-01-06 23:56:13 +00:00
< Box
sx = { {
width : isWide && ! isMobile ? "100%" : undefined ,
display : "flex" ,
flexDirection : "column" ,
alignItems : "center" ,
justifyContent : "center" ,
borderRight : isWide && ! isMobile ? "1px solid gray" : undefined
} }
>
2023-12-17 13:22:21 +00:00
< Typography
sx = { {
textAlign : "center" ,
m : "20px 0" ,
2023-12-29 00:58:19 +00:00
fontSize : "28px" ,
2023-12-31 00:04:35 +00:00
color : theme.palette.text.primary
2023-12-17 13:22:21 +00:00
} }
>
2023-12-17 18:15:59 +00:00
{ settings ? . cfg . formContact . title || "Заполните форму, чтобы получить результаты теста" }
2023-12-17 21:28:57 +00:00
2023-12-17 13:22:21 +00:00
< / Typography >
{
2023-12-17 18:15:59 +00:00
settings ? . cfg . formContact . desc &&
2023-12-17 21:28:57 +00:00
< Typography
sx = { {
2024-01-05 17:00:30 +00:00
color : theme.palette.text.primary ,
2023-12-17 21:28:57 +00:00
textAlign : "center" ,
m : "20px 0" ,
fontSize : "18px"
} }
>
{ settings ? . cfg . formContact . desc }
< / Typography >
2023-12-17 13:22:21 +00:00
}
< / Box >
2023-12-29 21:31:21 +00:00
< Box
2023-12-17 13:22:21 +00:00
sx = { {
display : "flex" ,
alignItems : "center" ,
justifyContent : "center" ,
flexDirection : "column" ,
2023-12-31 00:04:35 +00:00
backgroundColor : theme.palette.background.default ,
2023-12-17 13:22:21 +00:00
p : "30px"
} } >
< Box
sx = { {
display : "flex" ,
flexDirection : "column" ,
my : "20px"
} }
>
2023-12-29 13:35:25 +00:00
< Inputs
name = { name } setName = { setName }
email = { email } setEmail = { setEmail }
phone = { phone } setPhone = { setPhone }
text = { text } setText = { setText }
adress = { adress } setAdress = { setAdress }
/ >
2023-12-17 13:22:21 +00:00
< / Box >
{
2023-12-29 00:58:19 +00:00
// resultQuestion &&
// settings?.cfg.resultInfo.when === "after" &&
2023-12-17 13:22:21 +00:00
(
< Button
2023-12-31 00:04:35 +00:00
disabled = { ! ( ready && ! fire ) }
2024-01-05 17:00:30 +00:00
variant = "contained"
onClick = { async ( ) = > {
2024-01-05 19:18:57 +00:00
//@ts-ignore
const FC : any = settings ? . cfg . formContact . fields || settings ? . cfg . formContact
if ( FC [ "email" ] . used === EMAIL_REGEXP . test ( email ) ) { //почта валидна
2024-01-05 17:00:30 +00:00
setFire ( true )
if ( fireOnce . current ) {
if (
name . length > 0 ||
email . length > 0 ||
phone . length > 0 ||
text . length > 0 ||
adress . length > 0
) {
try {
await inputHC ( )
fireOnce . current = false
enqueueSnackbar ( "Данные успешно отправлены" )
} catch ( e ) {
enqueueSnackbar ( "повторите попытку позже" )
}
2024-01-05 17:39:25 +00:00
if ( ( settings ? . cfg . resultInfo . when === "after" || settings ? . cfg . resultInfo . when === "email" ) && ! checkEmptyData ( { resultData : resultQuestion } ) ) {
2024-01-05 17:00:30 +00:00
setShowContactForm ( false )
setShowResultForm ( true )
}
} else {
enqueueSnackbar ( "Пожалуйста, заполните поля" )
2023-12-31 00:04:35 +00:00
}
2023-12-24 11:52:09 +00:00
}
2024-01-05 17:00:30 +00:00
setFire ( false )
} else {
enqueueSnackbar ( "введена некорректная почта" )
}
2023-12-29 00:58:19 +00:00
} }
2023-12-31 00:04:35 +00:00
>
2023-12-29 00:58:19 +00:00
{ settings ? . cfg . formContact ? . button || "Получить результаты" }
2023-12-17 13:22:21 +00:00
< / Button >
) }
< Box
sx = { {
display : "flex" ,
mt : "20px" ,
2024-01-05 17:00:30 +00:00
width : isMobile ? "300px" : "450px" ,
2023-12-17 13:22:21 +00:00
} }
>
2023-12-31 00:04:35 +00:00
< CustomCheckbox label = "" handleChange = { ( { target } ) = > { setReady ( target . checked ) } } checked = { ready } colorIcon = { theme . palette . primary . main } / >
2024-01-05 17:00:30 +00:00
< Typography sx = { { color : theme.palette.text.primary } } >
2023-12-29 00:58:19 +00:00
С & ensp ;
2023-12-31 00:04:35 +00:00
< Link > П о л о ж е н и е м о б о б р а б о т к е п е р с о н а л ь н ы х д а н н ы х < / Link >
& ensp ; и & ensp ;
< Link > П о л и т и к о й к о н ф и д е н ц и а л ь н о с т и < / Link >
& ensp ; о з н а к о м л е н
2023-12-17 13:22:21 +00:00
< / Typography >
< / Box >
2023-12-21 19:22:06 +00:00
< Box
sx = { {
display : "flex" ,
alignItems : "center" ,
2023-12-29 00:58:19 +00:00
mt : "20px" ,
2023-12-31 00:04:35 +00:00
gap : "15px"
2023-12-21 19:22:06 +00:00
} }
>
2023-12-31 00:04:35 +00:00
< NameplateLogo style = { {
fontSize : "34px" ,
//@ts-ignore
color : mode [ settings . cfg . theme ] ? "#151515" : "#FFFFFF"
} } / >
< Typography sx = { {
fontSize : "20px" ,
//@ts-ignore
color : mode [ settings . cfg . theme ] ? "#4D4D4D" : "#F5F7FF" , whiteSpace : "nowrap"
} } >
2024-01-05 17:00:30 +00:00
С д е л а н о н а PenaQuiz
2023-12-29 00:58:19 +00:00
< / Typography >
2023-12-21 19:22:06 +00:00
< / Box >
2023-12-29 21:31:21 +00:00
< / Box >
2023-12-17 13:22:21 +00:00
< / B o x >
< / B o x >
2023-12-16 14:55:56 +00:00
) ;
} ;
2023-12-17 13:22:21 +00:00
2023-12-24 11:52:09 +00:00
const Inputs = ( {
name , setName ,
email , setEmail ,
phone , setPhone ,
text , setText ,
adress , setAdress
} : any ) = > {
2023-12-17 21:28:57 +00:00
const { settings , items } = useQuestionsStore ( )
2023-12-17 13:22:21 +00:00
2024-01-05 17:00:30 +00:00
console . log ( "______________________EMAIL_REGEXP.test(email)" )
console . log ( EMAIL_REGEXP . test ( email ) )
2023-12-17 13:22:21 +00:00
2023-12-23 19:53:39 +00:00
//@ts-ignore
const FC : any = settings ? . cfg . formContact . fields || settings ? . cfg . formContact
2023-12-17 21:28:57 +00:00
//@ts-ignore
const Name = < CustomInput onChange = { ( { target } ) = > setName ( target . value ) } id = { name } title = { FC [ "name" ] . innerText || "Введите имя" } desc = { FC [ "name" ] . text || "имя" } Icon = { NameIcon } / >
//@ts-ignore
2024-01-05 17:00:30 +00:00
const Email = < CustomInput
error = { ! EMAIL_REGEXP . test ( email ) }
label = { ! EMAIL_REGEXP . test ( email ) ? "" : "Некорректная почта" }
//@ts-ignore
onChange = { ( { target } ) = > setEmail ( target . value ) } id = { email } title = { FC [ "email" ] . innerText || "Введите Email" } desc = { FC [ "email" ] . text || "Email" } Icon = { EmailIcon } / >
2023-12-17 21:28:57 +00:00
//@ts-ignore
const Phone = < CustomInput onChange = { ( { target } ) = > setPhone ( target . value ) } id = { phone } title = { FC [ "phone" ] . innerText || "Введите номер телефона" } desc = { FC [ "phone" ] . text || "номер телефона" } Icon = { PhoneIcon } / >
//@ts-ignore
const Text = < CustomInput onChange = { ( { target } ) = > setText ( target . value ) } id = { text } title = { FC [ "text" ] . innerText || "Введите фамилию" } desc = { FC [ "text" ] . text || "фамилию" } Icon = { TextIcon } / >
//@ts-ignore
const Adress = < CustomInput onChange = { ( { target } ) = > setAdress ( target . value ) } id = { adress } title = { FC [ "address" ] . innerText || "Введите адрес" } desc = { FC [ "address" ] . text || "адрес" } Icon = { AddressIcon } / >
//@ts-ignore
2023-12-24 07:57:30 +00:00
if ( Object . values ( FC ) . some ( ( data ) = > data . used ) ) {
2023-12-17 21:28:57 +00:00
return < >
{ FC [ "name" ] . used ? Name : < > < / > }
{ FC [ "email" ] . used ? Email : < > < / > }
{ FC [ "phone" ] . used ? Phone : < > < / > }
{ FC [ "text" ] . used ? Text : < > < / > }
{ FC [ "address" ] . used ? Adress : < > < / > }
< / >
2023-12-17 13:22:21 +00:00
} else {
return < >
2023-12-17 21:28:57 +00:00
{ Name }
{ Email }
{ Phone }
2023-12-17 13:22:21 +00:00
< / >
}
}
2023-12-17 21:28:57 +00:00
const CustomInput = ( { title , desc , Icon , onChange } : any ) = > {
2024-01-05 17:00:30 +00:00
const theme = useTheme ( ) ;
const isMobile = useMediaQuery ( theme . breakpoints . down ( 600 ) ) ;
2023-12-21 19:22:06 +00:00
//@ts-ignore
2023-12-17 13:22:21 +00:00
return < Box m = "15px 0" >
2024-01-05 17:00:30 +00:00
< Typography mb = "7px" color = { theme . palette . text . primary } > { title } < / Typography >
2023-12-24 11:52:09 +00:00
2023-12-17 13:22:21 +00:00
< TextField
2023-12-17 21:28:57 +00:00
onChange = { onChange }
2023-12-17 13:22:21 +00:00
sx = { {
2024-01-05 17:39:25 +00:00
width : isMobile ? "300px" : "350px" ,
2023-12-17 13:22:21 +00:00
} }
placeholder = { desc }
InputProps = { {
startAdornment : < InputAdornment position = "start" > < Icon color = "gray" / > < / InputAdornment > ,
} }
/ >
< / Box >
}