import { useDispatch, useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; import { useNavigate, useParams } 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 { useEffect, useState } from "react"; 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"; import { EPlacementKeys, IPaywallProduct } from "@/api/resources/Paywall"; import { usePaywall } from "@/hooks/paywall/usePaywall"; const currency = Currency.USD; const locale = getClientLocale() as Locale; const getPrice = (product: IPaywallProduct | null) => { if (!product?.trialPrice) { return 0; } return (product.trialPrice === 100 ? 99 : product.trialPrice || 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 { subPlan } = useParams(); const birthday = useSelector(selectors.selectBirthday); console.log(nameError); const { products } = usePaywall({ placementKey: EPlacementKeys["aura.placement.main"], }); const activeProductFromStore = useSelector(selectors.selectActiveProduct); const [activeProduct, setActiveProduct] = useState( activeProductFromStore ); useEffect(() => { if (subPlan) { const targetProduct = products.find( (product) => String( product?.trialPrice ? Math.floor((product?.trialPrice + 1) / 100) : product.key.replace(".", "") ) === subPlan ); if (targetProduct) { setActiveProduct(targetProduct); } } }, [products, subPlan]); const paymentItems = [ { title: activeProduct?.name || "Per 7-Day Trial For", price: getPrice(activeProduct), description: activeProduct?.description?.length ? activeProduct.description : 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)); } if (name) { dispatch( actions.user.update({ username: name, }) ); } dispatch(actions.status.update("registred")); dispatch( actions.payment.update({ activeProduct, }) ); 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 handleClick = async () => { setIsSubmit(true); if ( !isValidEmail(email) // || !isValidName(name) ) { return; } await authorization(); }; // 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 ${getPrice(activeProduct || null)}
<> {t("auweb.agree.text1")} {t("subscription_text", { policyLink })}
); } export default SubscriptionPage;