import React, { useContext, useEffect, useRef, useState } from "react"; import './mainPart.css'; import { MainPartHeader } from "./mainPartHeader"; import { ContentContainer } from "./contentContainer"; import { MainPartFooter } from "./main-part-footer"; import { OrLineContainer } from "./orLineContainer"; import { CSSTransition, SwitchTransition, TransitionGroup } from "react-transition-group"; import { CalendarComponent } from "./calendar/calendar"; import { PlanContentContainer } from "./planContentContainer"; import { TimeSelector } from "./timeSelector/timeSelector"; import { Form } from "./form/form"; import { LangDict } from "../langDict"; import { ContextLang } from "../ContextLang"; import { ContextWs } from "../../ContextWs"; type TProps = { createSess: () => void, connectSess: () => void } type FormData = { phone: string email: string firstName: string } export const MainPart: React.FC = React.memo((props) => { const {lang, setLang} = useContext(ContextLang); const changeContent = useRef(0); const [showBackground, setShowBackground] = useState(false); const [planContent, setPlanContent] = useState(); const defaultCurrentContent = onClickStartDemo()} textButton={LangDict.startDemoButton} /> const [currentContent, setCurrentContent] = useState(defaultCurrentContent); const lastRender = currentContent; const history = useRef>(new Array()); const isRemoveLastElementHistry = useRef(false); useEffect(() => { console.log(lang) // onClickStartDemo() setCurrentContent(lastRender) }, [lang]); useEffect(() => { if(planContent && !isRemoveLastElementHistry.current) { history.current.push(planContent); isRemoveLastElementHistry.current = false; console.log(history.current.length, 'добавили 1') } }, [planContent]) function changeLang(lang: 'ru' | 'en') { setLang(lang); document.querySelector('html').lang = lang; } function changeContentFunc() { changeContent.current += 1 } function prevPlan() { changeContentFunc(); if(history.current.length - 1 <= 0) { onClickStartDemo(); history.current = new Array(); setPlanContent(null); disableBackground(); isRemoveLastElementHistry.current = false; } else { setPlanContent(history.current[history.current.length - 2]); history.current.splice(history.current.length - 1, 1); isRemoveLastElementHistry.current = true; console.log(history.current.length, 'убрали 1') } } function onClickStartDemo() { changeContentFunc(); setCurrentContent( <> onClickCreateNewDemo()} textButton={LangDict.startNewDemoButton} /> onClickPlaneDemo()} textButton={LangDict.enrollNewDemoButton} /> ) } function onClickCreateNewDemo() { props.createSess(); changeContentFunc(); setCurrentContent( onClickConnect()} textButton={LangDict.connectToDemoButton} isAccessCode={true} /> ) } function onClickPlaneDemo() { // changeContentFunc() isRemoveLastElementHistry.current = false; enableBackground(); setPlanContent( onSelectDay(date)} />} isLegend={true} toPrevState={prevPlan} showPrevButton={true} /> ); setCurrentContent(<>); } function onClickConnect() { props.connectSess() // ws.send('{ "message" : "NEW_SESS" }'); } function onSelectDay(date: {ru: string, en: string}) { // history.current.push(planContent); isRemoveLastElementHistry.current = false; changeContentFunc() setPlanContent( onSelectTime(date, time)} />} isLegend={true} toPrevState={prevPlan} showPrevButton={true} /> ); } function onSelectTime(date: {ru: string, en: string}, time: string) { // history.current.push(planContent); isRemoveLastElementHistry.current = false; changeContentFunc() let dateTime = `${date} ${lang === 'ru' ? 'в' : 'in'} ${time}`; let dateObj = { ru: `${date.ru} в ${time}`, en: `${date.en} in ${time}` } setPlanContent( onSubmit(data, date)} dateInfo={dateTime} /> } isLegend={false} toPrevState={prevPlan} showPrevButton={true} /> ) console.log(dateTime) } function onSubmit(data: FormData, date: string) { changeContentFunc(); setPlanContent(null); setCurrentContent( toMain()} /> ) history.current = new Array(); // console.log(data, date) } function toMain() { changeContentFunc(); setCurrentContent(defaultCurrentContent); setPlanContent(null); disableBackground(); history.current = new Array(); } function enableBackground() { setShowBackground(true); } function disableBackground() { setShowBackground(false); } return
changeLang(lang)} onClickLogo={toMain}/> node.addEventListener("transitionend", done, false)} classNames={'change-main-part-content'} >
{currentContent}
{ planContent ? node.addEventListener("transitionend", done, false)} classNames={'change-main-part-content'} >
{planContent}
: null }
})