2023-12-17 13:22:21 +00:00
import { Box , Typography , Button , Paper , TextField , Link , InputAdornment } from "@mui/material" ;
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
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-16 14:55:56 +00:00
type ContactFormProps = {
currentQuestion : AnyTypedQuizQuestion ;
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 ) = > {
2023-12-17 13:22:21 +00:00
const { settings } = useQuestionsStore ( )
const [ ready , setReady ] = useState ( false )
2023-12-16 14:55:56 +00:00
const followNextForm = ( ) = > {
setShowContactForm ( false ) ;
setShowResultForm ( true ) ;
} ;
return (
2023-12-17 13:22:21 +00:00
< Box
sx = { {
display : "flex" ,
alignItems : "center" ,
justifyContent : "center" ,
height : "100vh"
} }
>
< Box
sx = { {
width : "800px"
} }
>
< Box >
< Typography
sx = { {
textAlign : "center" ,
m : "20px 0" ,
fontSize : "28px"
} }
>
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 = { {
textAlign : "center" ,
m : "20px 0" ,
fontSize : "18px"
} }
>
{ settings ? . cfg . formContact . desc }
< / Typography >
2023-12-17 13:22:21 +00:00
}
< / Box >
< Paper
sx = { {
display : "flex" ,
alignItems : "center" ,
justifyContent : "center" ,
flexDirection : "column" ,
p : "30px"
} } >
< Box
sx = { {
display : "flex" ,
flexDirection : "column" ,
my : "20px"
} }
>
2023-12-17 21:28:57 +00:00
< Inputs currentQuestion = { currentQuestion } / >
2023-12-17 13:22:21 +00:00
< / Box >
{
(
< Button
disabled = { ! ready }
variant = "contained" onClick = { ( ) = > {
2023-12-17 18:15:59 +00:00
if ( settings ? . cfg . resultInfo . when === "after" ) followNextForm ( )
2023-12-17 13:22:21 +00:00
} } >
П о л у ч и т ь р е з у л ь т а т ы
< / Button >
) }
< Box
sx = { {
display : "flex" ,
mt : "20px" ,
width : "450px" ,
} }
>
< CustomCheckbox label = "" handleChange = { ( { target } ) = > { setReady ( target . checked ) } } checked = { ready } / >
< Typography >
С
< Link > П о л о ж е н и е м о б о б р а б о т к е п е р с о н а л ь н ы х д а н н ы х < / Link >
и
< Link > П о л и т и к о й к о н ф и д е н ц и а л ь н о с т и < / Link >
о з н а к о м л е н
< / Typography >
< / Box >
2023-12-21 19:22:06 +00:00
< Box
sx = { {
display : "flex" ,
alignItems : "center" ,
mt : "20px"
} }
>
< NameplateLogo style = { { fontSize : "34px" } } / >
< Typography sx = { { fontSize : "20px" , color : "#4D4D4D" , whiteSpace : "nowrap" } } > С д е л а н о н а PenaQuiz < / Typography >
< / Box >
2023-12-17 13:22:21 +00:00
< / Paper >
< / B o x >
< / B o x >
2023-12-16 14:55:56 +00:00
) ;
} ;
2023-12-17 13:22:21 +00:00
2023-12-17 21:28:57 +00:00
const Inputs = ( currentQuestion : any ) = > {
const { settings , items } = useQuestionsStore ( )
2023-12-17 13:22:21 +00:00
2023-12-17 21:28:57 +00:00
const [ name , setName ] = useState ( "" )
const [ email , setEmail ] = useState ( "" )
const [ phone , setPhone ] = useState ( "" )
const [ text , setText ] = useState ( "" )
const [ adress , setAdress ] = useState ( "" )
const inputHC = useDebouncedCallback ( async ( ) = > {
console . log ( "start input" )
2023-12-17 13:22:21 +00:00
2023-12-17 21:28:57 +00:00
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
console . log ( body )
if ( Object . keys ( body ) . length > 0 ) {
try {
await sendFC ( {
questionId : currentQuestion.id ,
body : body ,
//@ts-ignore
qid : settings.qid
} )
} catch ( e ) {
console . log ( e )
enqueueSnackbar ( "ответ не был засчитан" )
}
}
} , 1000 ) ;
useEffect ( ( ) = > {
inputHC ( )
} , [ name , email , phone , text , adress ] )
//@ts-ignore
const FC : any = settings ? . cfg . formContact
//@ts-ignore
const Name = < CustomInput onChange = { ( { target } ) = > setName ( target . value ) } id = { name } title = { FC [ "name" ] . innerText || "Введите имя" } desc = { FC [ "name" ] . text || "имя" } Icon = { NameIcon } / >
//@ts-ignore
const Email = < CustomInput onChange = { ( { target } ) = > setEmail ( target . value ) } id = { email } title = { FC [ "email" ] . innerText || "Введите Email" } desc = { FC [ "email" ] . text || "Email" } Icon = { EmailIcon } / >
//@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
if ( items . some ( ( data ) = > data . used ) ) {
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 ) = > {
2023-12-21 19:22:06 +00:00
//@ts-ignore
2023-12-17 13:22:21 +00:00
return < Box m = "15px 0" >
< Typography mb = "7px" > { title } < / Typography >
2023-12-21 19:22:06 +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 = { {
width : "350px" ,
} }
placeholder = { desc }
InputProps = { {
startAdornment : < InputAdornment position = "start" > < Icon color = "gray" / > < / InputAdornment > ,
} }
/ >
< / Box >
}