-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample1.tsx
More file actions
100 lines (88 loc) · 2.98 KB
/
example1.tsx
File metadata and controls
100 lines (88 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"use client";
import React from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Profanity } from "profanity-validator";
import { useState } from "react";
import { toast } from "sonner";
const profanity = new Profanity({
customWords: ["badword", "inappropriate", "submit"],
heat: 0.5,
});
export default function Example1() {
const [title, setTitle] = useState("Can you hit the 'submit' button");
const [description, setDescription] = useState(
"I'm so f**king tired of this s**t.",
);
const [errors, setErrors] = useState<Record<string, string>>({});
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsSubmitting(true);
setErrors({});
try {
const titleResult = await profanity.validateField(title);
const descriptionResult = await profanity.validateField(description);
const newErrors: Record<string, string> = {};
if (!titleResult.isValid) {
newErrors.title =
titleResult.message || "Inappropriate content detected";
}
if (!descriptionResult.isValid) {
newErrors.description =
descriptionResult.message || "Inappropriate content detected";
}
if (Object.keys(newErrors).length > 0) {
setErrors(newErrors);
return;
}
// If no profanity detected, proceed with form submission
console.log("Form submitted:", { title, description });
toast("Form submitted check console log!");
} catch (error) {
console.error("Validation error:", error);
toast.error("Validation failed. Check form errors.");
} finally {
setIsSubmitting(false);
}
};
return (
<form
onSubmit={handleSubmit}
className="space-y-6 w-full max-w-md mx-auto p-6 rounded-md"
>
<div className="space-y-2">
<Input
type="text"
placeholder="Enter title"
value={title}
onChange={(e) => setTitle(e.target.value)}
className={errors.title ? "border-red-500" : ""}
/>
{errors.title && (
<Alert variant="destructive">
<AlertDescription>{errors.title}</AlertDescription>
</Alert>
)}
</div>
<div className="space-y-2">
<Textarea
placeholder="Enter description"
value={description}
onChange={(e) => setDescription(e.target.value)}
className={errors.description ? "border-red-500" : ""}
/>
{errors.description && (
<Alert variant="destructive">
<AlertDescription>{errors.description}</AlertDescription>
</Alert>
)}
</div>
<Button type="submit" disabled={isSubmitting} className="w-full">
{isSubmitting ? "Checking..." : "Submit"}
</Button>
</form>
);
}