This commit is contained in:
Shubhankar45
2026-11-13 12:14:55 +00:00
commit 53dbfa976b
191 changed files with 12074 additions and 0 deletions
+187
View File
@@ -0,0 +1,187 @@
import { useState } from 'react'
import { CheckCircle2 } from 'lucide-react'
import api, { errorMessage, fieldErrors } from '../services/api'
import { Field, inputCls, Spinner } from './ui'
const PHONE_RE = /^\+?[0-9\s\-()]{7,20}$/
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/
function useForm(initial) {
const [values, setValues] = useState(initial)
const [errors, setErrors] = useState({})
const set = (k) => (e) => {
setValues((v) => ({ ...v, [k]: e.target.value }))
if (errors[k]) setErrors((er) => ({ ...er, [k]: undefined }))
}
return { values, setValues, errors, setErrors, set }
}
function Success({ title, text, onClose }) {
return (
<div className="flex flex-col items-center py-6 text-center animate-fade-in" role="status">
<CheckCircle2 className="size-14 text-green" strokeWidth={1.75} />
<h3 className="mt-4 text-[24px] font-extrabold text-navy">{title}</h3>
<p className="mt-2 max-w-sm text-[16px] leading-[24px] text-slate">{text}</p>
<button type="button" onClick={onClose} className="mt-8 rounded-full bg-brand px-8 py-3 font-semibold text-white transition hover:bg-[#023a5a]">
Close
</button>
</div>
)
}
function SubmitButton({ busy, children }) {
return (
<button
type="submit"
disabled={busy}
className="inline-flex h-[54px] w-full items-center justify-center gap-2 rounded-full bg-yellow-cta px-8 text-[16px] font-semibold text-black transition hover:brightness-105 disabled:opacity-60"
>
{busy && <Spinner className="size-4" />}
{busy ? 'Sending…' : children}
</button>
)
}
/** "Get a consultation" — saves an enquiry (POST /api/enquiries). */
export function EnquiryForm({ onDone, defaults = {} }) {
const f = useForm({ name: '', phone: '', email: '', city: '', property_type: 'residential', monthly_bill: '', message: '', website: '', ...defaults })
const [busy, setBusy] = useState(false)
const [serverError, setServerError] = useState('')
const [done, setDone] = useState(false)
const validate = () => {
const v = f.values
const e = {}
if (v.name.trim().length < 2) e.name = 'Please enter your name'
if (!PHONE_RE.test(v.phone.trim()) || v.phone.replace(/\D/g, '').length < 7) e.phone = 'Enter a valid phone number'
if (v.email && !EMAIL_RE.test(v.email.trim())) e.email = 'Enter a valid email address'
if (v.monthly_bill && (Number.isNaN(Number(v.monthly_bill)) || Number(v.monthly_bill) < 0)) e.monthly_bill = 'Enter an amount in ₹'
f.setErrors(e)
return Object.keys(e).length === 0
}
const submit = async (ev) => {
ev.preventDefault()
setServerError('')
if (!validate()) return
setBusy(true)
try {
const v = f.values
await api.post('/enquiries', {
...v,
monthly_bill: v.monthly_bill === '' ? null : Number(v.monthly_bill),
})
setDone(true)
} catch (err) {
f.setErrors(fieldErrors(err))
setServerError(errorMessage(err))
} finally {
setBusy(false)
}
}
if (done) return <Success title="Thank you!" text="Our team will contact you shortly to discuss your solar requirements." onClose={onDone} />
return (
<form onSubmit={submit} noValidate className="grid gap-4 sm:grid-cols-2">
<div className="sm:col-span-2">
<Field label="Full name" required error={f.errors.name}>
<input className={inputCls} value={f.values.name} onChange={f.set('name')} autoComplete="name" aria-invalid={!!f.errors.name} />
</Field>
</div>
<Field label="Phone" required error={f.errors.phone}>
<input className={inputCls} type="tel" value={f.values.phone} onChange={f.set('phone')} autoComplete="tel" placeholder="+91 98765 43210" aria-invalid={!!f.errors.phone} />
</Field>
<Field label="Email" error={f.errors.email}>
<input className={inputCls} type="email" value={f.values.email} onChange={f.set('email')} autoComplete="email" aria-invalid={!!f.errors.email} />
</Field>
<Field label="City" error={f.errors.city}>
<input className={inputCls} value={f.values.city} onChange={f.set('city')} autoComplete="address-level2" placeholder="Bhubaneswar" />
</Field>
<Field label="Property type">
<select className={inputCls} value={f.values.property_type} onChange={f.set('property_type')}>
<option value="residential">Residential</option>
<option value="commercial">Commercial</option>
<option value="industrial">Industrial</option>
<option value="other">Agriculture / Other</option>
</select>
</Field>
<div className="sm:col-span-2">
<Field label="Average monthly electricity bill (₹)" error={f.errors.monthly_bill}>
<input className={inputCls} inputMode="numeric" value={f.values.monthly_bill} onChange={f.set('monthly_bill')} placeholder="e.g. 3500" aria-invalid={!!f.errors.monthly_bill} />
</Field>
</div>
<div className="sm:col-span-2">
<Field label="Message" error={f.errors.message}>
<textarea className={`${inputCls} min-h-24`} value={f.values.message} onChange={f.set('message')} maxLength={2000} placeholder="Tell us about your requirement" />
</Field>
</div>
{/* honeypot */}
<input type="text" name="website" value={f.values.website} onChange={f.set('website')} tabIndex={-1} autoComplete="off" className="hidden" aria-hidden />
{serverError && <p className="rounded-lg bg-[#FEF3F2] px-4 py-3 text-[14px] text-[#B42318] sm:col-span-2" role="alert">{serverError}</p>}
<div className="sm:col-span-2">
<SubmitButton busy={busy}>Request consultation</SubmitButton>
</div>
</form>
)
}
/** "Support" — contact message (POST /api/contact). */
export function ContactForm({ onDone }) {
const f = useForm({ name: '', email: '', phone: '', subject: '', message: '', website: '' })
const [busy, setBusy] = useState(false)
const [serverError, setServerError] = useState('')
const [done, setDone] = useState(false)
const submit = async (ev) => {
ev.preventDefault()
setServerError('')
const v = f.values
const e = {}
if (v.name.trim().length < 2) e.name = 'Please enter your name'
if (!EMAIL_RE.test(v.email.trim())) e.email = 'Enter a valid email address'
if (v.phone && !PHONE_RE.test(v.phone.trim())) e.phone = 'Enter a valid phone number'
if (v.message.trim().length < 5) e.message = 'Please write a short message'
f.setErrors(e)
if (Object.keys(e).length) return
setBusy(true)
try {
await api.post('/contact', v)
setDone(true)
} catch (err) {
f.setErrors(fieldErrors(err))
setServerError(errorMessage(err))
} finally {
setBusy(false)
}
}
if (done) return <Success title="Message sent" text="Thank you for reaching out. We'll get back to you soon." onClose={onDone} />
return (
<form onSubmit={submit} noValidate className="grid gap-4 sm:grid-cols-2">
<Field label="Name" required error={f.errors.name}>
<input className={inputCls} value={f.values.name} onChange={f.set('name')} autoComplete="name" aria-invalid={!!f.errors.name} />
</Field>
<Field label="Email" required error={f.errors.email}>
<input className={inputCls} type="email" value={f.values.email} onChange={f.set('email')} autoComplete="email" aria-invalid={!!f.errors.email} />
</Field>
<Field label="Phone" error={f.errors.phone}>
<input className={inputCls} type="tel" value={f.values.phone} onChange={f.set('phone')} autoComplete="tel" aria-invalid={!!f.errors.phone} />
</Field>
<Field label="Subject">
<input className={inputCls} value={f.values.subject} onChange={f.set('subject')} maxLength={200} />
</Field>
<div className="sm:col-span-2">
<Field label="Message" required error={f.errors.message}>
<textarea className={`${inputCls} min-h-28`} value={f.values.message} onChange={f.set('message')} maxLength={5000} aria-invalid={!!f.errors.message} />
</Field>
</div>
<input type="text" name="website" value={f.values.website} onChange={f.set('website')} tabIndex={-1} autoComplete="off" className="hidden" aria-hidden />
{serverError && <p className="rounded-lg bg-[#FEF3F2] px-4 py-3 text-[14px] text-[#B42318] sm:col-span-2" role="alert">{serverError}</p>}
<div className="sm:col-span-2">
<SubmitButton busy={busy}>Send message</SubmitButton>
</div>
</form>
)
}