import { useTranslation } from "react-i18next"; import MainButton from "../MainButton"; import Title from "../Title"; import styles from "./styles.module.css"; import { useCallback, useEffect, useRef, useState } from "react"; import NameInput from "./nameInput"; import DatePicker from "./DatePicker"; import { IDate, getDateAsString } from "@/services/date"; import { AICompatCategories, useApi, useApiCall } from "@/api"; import { useNavigate } from "react-router-dom"; import routes from "@/routes"; import { useDispatch, useSelector } from "react-redux"; import { actions, selectors } from "@/store"; import { EPathsFromHome } from "@/store/siteConfig"; import { getCategoryIdByZodiacSign, getZodiacSignByDate, } from "@/services/zodiac-sign"; import { Asset } from "@/api/resources/Assets"; import { getRandomArbitrary } from "@/services/random-value"; import Onboarding, { EDirectionOnboarding } from "../Onboarding"; import TextWithFinger from "../TextWithFinger"; function CompatibilityPage(): JSX.Element { const { t, i18n } = useTranslation(); const navigate = useNavigate(); const dispatch = useDispatch(); const [isDisabled, setIsDisabled] = useState(true); const [isDisabledName, setIsDisabledName] = useState(true); const [isDisabledDate, setIsDisabledDate] = useState(true); const [isChangeDate, setIsChangeDate] = useState(false); const [currentOnboarding, setCurrentOnboarding] = useState(0); const [name, setName] = useState(""); const [selectedDate, setSelectedDate] = useState(""); const [compatCategory, setCompatCategory] = useState(1); const homeConfig = useSelector(selectors.selectHome); const showNavbarFooter = homeConfig.isShowNavbar; const birthdate = useSelector(selectors.selectBirthdate); const onboardingCompatibility = useSelector( selectors.selectOnboardingCompatibility ); const zodiacSign = getZodiacSignByDate(birthdate); const [asset, setAsset] = useState(); const api = useApi(); const inputRef = useRef(null); const dateRef = useRef(null); const categoriesRef = useRef(null); const mainButtonRef = useRef(null); const locale = i18n.language; const assetsData = useCallback(async () => { const { asset_categories } = await api.getAssetCategories({ locale }); const categoryId = getCategoryIdByZodiacSign(zodiacSign, asset_categories); const { assets } = await api.getAssets({ category: String(categoryId || "1"), }); return assets; }, [api, locale, zodiacSign]); const { data: assets, // isPending } = useApiCall(assetsData); useEffect(() => { if (assets) { setAsset(assets[getRandomArbitrary(0, assets?.length || 0)]); } }, [assets]); const handleNext = () => { if (isDisabled) return; dispatch( actions.onboardingConfig.update({ compatibility: { isShown: true, }, }) ); dispatch( actions.compatibility.update({ rightUser: { name, birthDate: selectedDate, }, categoryId: compatCategory, }) ); navigate(routes.client.compatibilityResult()); }; const handleCross = () => { if (homeConfig.pathFromHome === EPathsFromHome.compatibility) { dispatch( actions.siteConfig.update({ home: { pathFromHome: EPathsFromHome.compatibility, isShowNavbar: false, }, }) ); return navigate(routes.client.breath()); } if (homeConfig.pathFromHome === EPathsFromHome.breath) { dispatch( actions.siteConfig.update({ home: { pathFromHome: EPathsFromHome.compatibility, isShowNavbar: true, }, }) ); return navigate(routes.client.home()); } return navigate(routes.client.home()); }; const loadData = useCallback(() => { return api .getAiCompatCategories({ locale }) .then((resp: AICompatCategories.Response) => resp.compat_categories); }, [api, locale]); const { data } = useApiCall(loadData); const handleValidName = (name: string) => { setIsDisabledName(!name.length); setName(name); checkAllDisabled(); }; const handleValidDate = (date: string | IDate) => { setIsDisabledDate(date === ""); setIsChangeDate(date !== getDateAsString(new Date())); setSelectedDate(date); checkAllDisabled(); }; const checkAllDisabled = () => { setIsDisabled(isDisabledName || isDisabledDate); }; const changeCompatCategory = (event: React.ChangeEvent) => { setCompatCategory(parseInt(event.target.value)); }; return (
{!showNavbarFooter && (
)} {/* {t("compatibility")} */}
{t("iAm")} +
{!onboardingCompatibility.isShown && <> {currentOnboarding === 0 && ( setCurrentOnboarding(1)} /> )} {currentOnboarding === 1 && ( <> )} {currentOnboarding === 3 && ( )} }
{ if (!name.length) return; if (e.key === "Enter") { setCurrentOnboarding(1); (e.target as HTMLInputElement).blur(); dateRef.current?.scrollIntoView({ behavior: "smooth" }); } }} onValid={handleValidName} onInvalid={() => setIsDisabledName(true)} />
{currentOnboarding === 2 && !onboardingCompatibility.isShown && ( setCurrentOnboarding(3)} /> )} {data && data.length && (
{data.map((item, index) => (
setCurrentOnboarding(3)} >
))}
)}
{t("check")}
); } export default CompatibilityPage;