import { useDispatch, useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { actions, selectors } from "@/store"; import MainButton from "../MainButton"; import Policy from "../Policy"; import PaymentTable, { Currency, Locale } from "../PaymentTable"; import CallToAction from "../CallToAction"; import routes from "@/routes"; import styles from "./styles.module.css"; import Header from "../Header"; import SpecialWelcomeOffer from "../SpecialWelcomeOffer"; import { useState } from "react"; import { ITrial } from "@/api/resources/SubscriptionPlans"; import { ApiError, extractErrorMessage, useApi } from "@/api"; import { useAuth } from "@/auth"; import { getClientLocale, getClientTimezone } from "@/locales"; import Loader from "../Loader"; import Title from "../Title"; import ErrorText from "../ErrorText"; const currency = Currency.USD; const locale = getClientLocale() as Locale; const getPriceFromTrial = (trial: ITrial | null) => { if (!trial) { return 0; } return (trial.price_cents || 0) / 100; }; function SubscriptionPage(): JSX.Element { const api = useApi(); const timezone = getClientTimezone(); const { signUp } = useAuth(); const { t } = useTranslation(); const navigate = useNavigate(); const dispatch = useDispatch(); const [isOpenModal, setIsOpenModal] = useState(false); const [email, setEmail] = useState(""); const [emailError, setEmailError] = useState( "Email is invalid" ); const [name, setName] = useState(""); const [nameError, setNameError] = useState("Name is invalid"); const [isSubmit, setIsSubmit] = useState(false); const [isAuth, setIsAuth] = useState(false); const [isLoading, setIsLoading] = useState(false); const [apiError, setApiError] = useState(null); const [error, setError] = useState(false); const activeSubPlan = useSelector(selectors.selectActiveSubPlan); const birthday = useSelector(selectors.selectBirthday); const paymentItems = [ { title: activeSubPlan?.name || "Per 7-Day Trial For", price: getPriceFromTrial(activeSubPlan?.trial || null), description: activeSubPlan?.desc.length ? activeSubPlan?.desc : t("au.2week_plan.web"), }, ]; const authorization = async () => { try { setIsLoading(true); const auth = await api.auth({ email, timezone, locale }); const { auth: { token, user }, } = auth; signUp(token, user); const payload = { user: { profile_attributes: { birthday } }, token, }; const updatedUser = await api.updateUser(payload).catch((error) => { console.log("Error: ", error); }); if (updatedUser?.user) { dispatch(actions.user.update(updatedUser.user)); } dispatch(actions.status.update("registred")); setIsLoading(false); setIsAuth(true); setTimeout(() => { navigate(routes.client.paymentMethod()); }, 1000); } catch (error) { console.error(error); if (error instanceof ApiError) { setApiError(error as ApiError); } else { setError(true); } setIsLoading(false); } }; // const email = useSelector(selectors.selectEmail); const handleClick = async () => { setIsSubmit(true); if (!isValidEmail(email) || !isValidName(name)) { return; } await authorization(); console.log("sdvdvsdv"); // navigate(routes.client.paymentMethod()) }; const handleCross = () => setIsOpenModal(true); const policyLink = ( {t("subscription_policy")} ); const isValidEmail = (email: string) => { return /\S+@\S+\.\S+/.test(email); }; const isValidName = (name: string) => { return !!(name.length > 0 && name.length < 30); }; const handleChangeEmail = (event: React.ChangeEvent) => { const email = event.target.value; if (!isValidEmail(email)) { setEmailError("Email is invalid"); } else { setEmailError(null); } setEmail(email); }; const handleChangeName = (event: React.ChangeEvent) => { const name = event.target.value; if (!isValidName(name)) { setNameError("Name is invalid"); } else { setNameError(null); } setName(name); }; return ( <>
{/* */}
{/* */}
{isSubmit && !!nameError && ( {nameError} )}
{isSubmit && !!emailError && ( {emailError} )}
{isLoading && isSubmit && isValidEmail(email) && isValidName(name) && } {(error || apiError) && ( Something went wrong )} {apiError && ( )} {!apiError && !error && !isLoading && isAuth && ( Success Icon )}
Start ${getPriceFromTrial(activeSubPlan?.trial || null)}
<> {t("auweb.agree.text1")} {t("subscription_text", { policyLink })}
); } export default SubscriptionPage;