AdForm.tsx
AdForm.tsx
if (locations.length === 0) {
toast({
title: "Location selection error",
description: "Please select at least one country for your ad.",
variant: "destructive",
})
return
}
if (!recaptchaValue) {
toast({
title: "reCAPTCHA verification required",
description: "Please complete the reCAPTCHA verification.",
variant: "destructive",
})
return
}
// Post the ad
postAd()
}
const newAd = {
title,
description,
price: Number.parseFloat(price),
category,
locations,
imageUrl,
userId: user!.id,
targetAudience,
}
toast({
title: "Ad posted successfully",
description: "Your ad has been posted and shared on all social media
platforms.",
})
router.push("/dashboard")
} catch (error) {
console.error("Error posting ad:", error)
toast({
title: "Error",
description: "Failed to post your ad. Please try again.",
variant: "destructive",
})
}
}
return (
<form onSubmit={handleSubmit} className="space-y-4 max-w-xl mx-auto">
<div>
<Label htmlFor="title">Ad Title (Be descriptive for better
visibility)</Label>
<Input id="title" value={title} onChange={(e) => setTitle(e.target.value)}
required />
</div>
<div>
<Label htmlFor="description">Description (Include relevant details to help
your ad appear in searches)</Label>
<Textarea id="description" value={description} onChange={(e) =>
setDescription(e.target.value)} required />
</div>
<div>
<Label htmlFor="price">Price</Label>
<Input id="price" type="number" value={price} onChange={(e) =>
setPrice(e.target.value)} required />
</div>
<div>
<Label htmlFor="category">Category</Label>
<Select onValueChange={setCategory} required>
<SelectTrigger>
<SelectValue placeholder="Select a category" />
</SelectTrigger>
<SelectContent>
{categories.map((cat) => (
<SelectItem key={cat} value={cat}>
{cat}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div>
<Label htmlFor="locations">Countries (select all that apply)</Label>
<Select
onValueChange={(value) => {
if (locations.includes(value)) {
setLocations(locations.filter((loc) => loc !== value))
} else {
setLocations([...locations, value])
}
}}
>
<SelectTrigger>
<SelectValue placeholder="Select countries" />
</SelectTrigger>
<SelectContent>
{countries.map((country) => (
<SelectItem key={country} value={country}>
<div className="flex items-center">
<Checkbox checked={locations.includes(country)} className="mr-
2" />
{country}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
<div className="mt-2">Selected countries: {locations.join(", ")}</div>
</div>
<div>
<Label htmlFor="image">Upload Image</Label>
<Input
id="image"
type="file"
accept="image/*"
onChange={(e) => setImage(e.target.files?.[0] || null)}
required
/>
</div>
<div>
<Label htmlFor="targetAudience">Target Audience</Label>
<Input
id="targetAudience"
value={targetAudience}
onChange={(e) => setTargetAudience(e.target.value)}
placeholder="Describe your target audience"
/>
</div>
<div>
<ReCAPTCHA
sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY || ""}
onChange={(value) => setRecaptchaValue(value)}
/>
</div>
<div>
<p className="text-sm text-gray-500">
Your ad will be automatically shared on Facebook, Twitter, Instagram,
TikTok, Pinterest, and LinkedIn.
</p>
</div>
<Button type="submit" className="w-full">
Post Ad
</Button>
</form>
)
}