import { useState } from 'react'
import { Button } from '@/components/ui/button.jsx'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card.jsx'
import { Input } from '@/components/ui/input.jsx'
import { Label } from '@/components/ui/label.jsx'
import { Textarea } from '@/components/ui/textarea.jsx'
import { Checkbox } from '@/components/ui/checkbox.jsx'
import { Badge } from '@/components/ui/badge.jsx'
import { Globe, Mail, User, MessageSquare, CheckCircle, AlertCircle, DollarSign } from 'lucide-react'
import { Link } from 'react-router-dom'
import { loadStripe } from '@stripe/stripe-js'
// Make sure to call `loadStripe` outside of a component’s render to avoid
// recreating the Stripe object on every render.
// This is your publishable key.
const stripePromise = loadStripe('pk_test_TYooMQauvdEDq5XKxTMn5jxK') // Replace with your actual publishable key
function DirectBuy() {
const [currentStep, setCurrentStep] = useState(1)
const [formData, setFormData] = useState({
name: '',
email: '',
country: '',
uniqueNamePart1: '',
uniqueNamePart2: '',
experience: '',
whyInterested: '',
tipsForInfo: '',
newsletter: false,
terms: false
})
const [isSubmitting, setIsSubmitting] = useState(false)
const [submitStatus, setSubmitStatus] = useState(null) // 'success', 'error', or null
const [uniqueNameError, setUniqueNameError] = useState('')
const [uniqueNameValid, setUniqueNameValid] = useState(false)
const handleInputChange = (e) => {
const { id, value, type, checked } = e.target
setFormData(prev => ({
...prev,
[id]: type === 'checkbox' ? checked : value
}))
}
const validateUniqueNamePart = (part) => {
const regex = /^[a-z]{6,}$/
return regex.test(part)
}
const validateStep1 = () => {
const { name, email, uniqueNamePart1, uniqueNamePart2 } = formData
if (!name || !email) {
setSubmitStatus('error')
setUniqueNameValid(false) // Set to false if other required fields are missing
return false
}
let isValid = true
if (!validateUniqueNamePart(uniqueNamePart1)) {
setUniqueNameError('First part must be at least 6 lowercase letters.')
isValid = false
} else if (!validateUniqueNamePart(uniqueNamePart2)) {
setUniqueNameError('Second part must be at least 6 lowercase letters.')
isValid = false
} else {
setUniqueNameError('')
}
setUniqueNameValid(isValid)
if (!isValid) {
setSubmitStatus('error')
return false
}
setSubmitStatus(null)
return true
}
const validateStep2 = () => {
const { terms } = formData
if (!terms) {
setSubmitStatus('error')
return false
}
setSubmitStatus(null)
return true
}
const handleNext = () => {
setSubmitStatus(null) // Clear previous status
if (currentStep === 1 && !validateStep1()) {
return
}
if (currentStep === 2 && !validateStep2()) {
return
}
setCurrentStep(prev => prev + 1)
}
const handlePrev = () => {
setSubmitStatus(null) // Clear previous status
setCurrentStep(prev => prev - 1)
}
const handleBuy = async (e) => {
e.preventDefault()
setIsSubmitting(true)
setSubmitStatus(null)
try {
// Create a Checkout Session on your backend
// Replace with your actual API endpoint
const response = await fetch('https://your-backend.com/create-checkout-session', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: formData.name,
email: formData.email,
country: formData.country,
uniqueName: `${formData.uniqueNamePart1}.${formData.uniqueNamePart2}`,
experience: formData.experience,
whyInterested: formData.whyInterested,
tipsForInfo: formData.tipsForInfo,
newsletter: formData.newsletter,
priceId: 'price_12345', // Replace with your actual Stripe Price ID for $20/month
}),
})
if (!response.ok) {
throw new Error('Failed to create Stripe Checkout Session')
}
const { sessionId } = await response.json()
const stripe = await stripePromise
const { error } = await stripe.redirectToCheckout({
sessionId,
})
if (error) {
console.error('Stripe checkout error:', error)
setSubmitStatus('error')
}
} catch (error) {
console.error('Purchase error:', error)
setSubmitStatus('error')
} finally {
setIsSubmitting(false)
}
}
const renderStepContent = () => {
switch (currentStep) {
case 1:
return (
<>
Please fill in all required fields and correct any errors. This will be your unique identifier (e.g., firstpart.secondpart). Each part must be 7 lowercase letters. {uniqueNameError} Please fill in all required fields and agree to the terms. Redirecting to secure Stripe checkout... There was an error processing your purchase. Please try again or contact support.
You are about to purchase a membership to ThreeFold Galaxy Coop for $20 USD per month.
Click "Proceed to Payment" to be redirected to Stripe's secure checkout page.
Validation Error
Validation Error
Purchase Initiated!
Purchase Failed
Join ThreeFold Galaxy Coop's sovereign digital freezone for $20 USD per month.