import { useState } from "react";
import { Navigate, useSearchParams } from "react-router-dom";
import { api } from "../api";
import { useEffectOnce } from "../useEffectOnce";

export function VerifyRegistrationPage() {
  const [params] = useSearchParams();
  const [isVerified, setIsVerified] = useState(false);

  useEffectOnce(() => {
    async function verify() {
      try {
        await api.post("accounts/verify-registration/", {
          user_id: params.get("user_id"),
          timestamp: params.get("timestamp"),
          signature: params.get("signature"),
        });
      } finally {
        setIsVerified(true);
      }
    }

    verify();
  }, []);

  if (!isVerified) {
    return <div>Verifying...</div>;
  }
  return <Navigate to="/" />;
}
