diff --git a/src/pages/Activate.jsx b/src/pages/Activate.jsx new file mode 100644 index 0000000..24cb319 --- /dev/null +++ b/src/pages/Activate.jsx @@ -0,0 +1,198 @@ +import React, { useState, useEffect } from "react"; +import { motion } from "framer-motion"; +import { base44 } from "@/api/base44Client"; +import { createPageUrl } from "@/utils"; +import { ShieldCheck, Loader2, AlertCircle } from "lucide-react"; +import { Input } from "@/components/ui/input"; +import { Button } from "@/components/ui/button"; +import { Label } from "@/components/ui/label"; +import ActivationSuccessModal from "@/components/onboarding/ActivationSuccessModal"; +import DemoControlsPanel from "@/components/demo/DemoControlsPanel"; +import { getDemoControls } from "@/components/demo/useDemoControls"; + +export default function Activate() { + const urlParams = new URLSearchParams(window.location.search); + const userId = urlParams.get("uid"); + const codeFromUrl = urlParams.get("code") || ""; + + const [code, setCode] = useState(codeFromUrl); + const [user, setUser] = useState(null); + const [loading, setLoading] = useState(true); + const [submitting, setSubmitting] = useState(false); + const [error, setError] = useState(""); + const [showSuccess, setShowSuccess] = useState(false); + + useEffect(() => { + const loadUser = async () => { + if (!userId) { + setError("Invalid activation link. Please check your email and try again."); + setLoading(false); + return; + } + const results = await base44.entities.BoUser.filter({ id: userId }); + if (!results || results.length === 0) { + setError("Account not found. Please contact support."); + setLoading(false); + return; + } + const foundUser = results[0]; + if (foundUser.is_activated) { + // Already activated — send to dashboard + restoreSessionAndRedirect(foundUser); + return; + } + setUser(foundUser); + setLoading(false); + }; + loadUser(); + }, [userId]); + + const restoreSessionAndRedirect = (u) => { + sessionStorage.setItem("demo_user", JSON.stringify({ + email: u.email, + store_id: u.store_id, + store_name: u.store_name, + store_address: u.store_address, + full_name: u.full_name, + first_login_completed: u.first_login_completed + })); + window.location.href = createPageUrl("Dashboard") + "?first_login=true"; + }; + + const handleSubmit = async (e) => { + e.preventDefault(); + setError(""); + if (!code.trim()) { + setError("Please enter the verification code from your email."); + return; + } + // Demo: simulate invalid code + const demoControls = getDemoControls(); + if (!demoControls.activationCodeValid) { + setError("The verification code you entered is invalid. Please check your email and try again."); + return; + } + if (code.trim().toUpperCase() !== user.activation_code) { + setError("That code doesn't match. Please check your email and try again."); + return; + } + setSubmitting(true); + await base44.entities.BoUser.update(user.id, { + is_activated: true, + activation_completed_at: new Date().toISOString() + }); + setSubmitting(false); + setShowSuccess(true); + }; + + const handleSuccessConfirm = () => { + restoreSessionAndRedirect(user); + }; + + if (loading) { + return ( +
+ Enter the code from your email
+{error}
++ We sent a verification code to{" "} + {user?.email}. + Enter it below to complete your account activation. +
+ + + ++ Need help? Contact{" "} + + support@packagehub360.com + +
+ > + )} +