announcement
This commit is contained in:
parent
4f63014500
commit
79cd8172e3
@ -72,6 +72,21 @@ export async function POST(req: NextRequest) {
|
|||||||
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
|
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify user exists in database
|
||||||
|
console.log("Verifying user ID:", session.user.id);
|
||||||
|
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: { id: session.user.id }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
console.error("Author not found in database:", session.user.id);
|
||||||
|
return NextResponse.json({
|
||||||
|
error: "Author not found",
|
||||||
|
details: "The user account does not exist in the database"
|
||||||
|
}, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
// Create new announcement
|
// Create new announcement
|
||||||
const announcement = await prisma.announcement.create({
|
const announcement = await prisma.announcement.create({
|
||||||
data: {
|
data: {
|
||||||
@ -85,6 +100,11 @@ export async function POST(req: NextRequest) {
|
|||||||
return NextResponse.json(announcement, { status: 201 });
|
return NextResponse.json(announcement, { status: 201 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating announcement:", error);
|
console.error("Error creating announcement:", error);
|
||||||
return NextResponse.json({ error: "Failed to create announcement" }, { status: 500 });
|
// Return more detailed error information
|
||||||
|
return NextResponse.json({
|
||||||
|
error: "Failed to create announcement",
|
||||||
|
details: error.message || "Unknown error",
|
||||||
|
code: error.code
|
||||||
|
}, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -30,9 +30,10 @@ import {
|
|||||||
CardHeader,
|
CardHeader,
|
||||||
CardTitle
|
CardTitle
|
||||||
} from "@/components/ui/card";
|
} from "@/components/ui/card";
|
||||||
import { CheckIcon, Loader2 } from "lucide-react";
|
import { CheckIcon, Loader2, AlertCircle } from "lucide-react";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { useToast } from "@/components/ui/use-toast";
|
import { useToast } from "@/components/ui/use-toast";
|
||||||
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
||||||
|
|
||||||
// Form schema
|
// Form schema
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
@ -49,6 +50,7 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
|
|||||||
const [selectedRoles, setSelectedRoles] = useState<string[]>([]);
|
const [selectedRoles, setSelectedRoles] = useState<string[]>([]);
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
const [isSuccess, setIsSuccess] = useState(false);
|
const [isSuccess, setIsSuccess] = useState(false);
|
||||||
|
const [errorMsg, setErrorMsg] = useState<string | null>(null);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
// Initialize form
|
// Initialize form
|
||||||
@ -99,6 +101,7 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
|
|||||||
// Form submission
|
// Form submission
|
||||||
const onSubmit = async (data: z.infer<typeof formSchema>) => {
|
const onSubmit = async (data: z.infer<typeof formSchema>) => {
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
|
setErrorMsg(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send the data to the API
|
// Send the data to the API
|
||||||
@ -110,8 +113,17 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
|
|||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Parse response, even if it's an error
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to create announcement');
|
let errorMessage = result.error || 'Failed to create announcement';
|
||||||
|
if (result.details) {
|
||||||
|
errorMessage += `: ${result.details}`;
|
||||||
|
}
|
||||||
|
console.error("API Error:", result);
|
||||||
|
setErrorMsg(errorMessage);
|
||||||
|
throw new Error(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset form and show success message
|
// Reset form and show success message
|
||||||
@ -130,7 +142,7 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
|
|||||||
console.error("Error submitting announcement:", error);
|
console.error("Error submitting announcement:", error);
|
||||||
toast({
|
toast({
|
||||||
title: "Error",
|
title: "Error",
|
||||||
description: "Failed to create the announcement. Please try again.",
|
description: error instanceof Error ? error.message : "Failed to create the announcement. Please try again.",
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
@ -147,6 +159,14 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
|
|||||||
</CardDescription>
|
</CardDescription>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
|
{errorMsg && (
|
||||||
|
<Alert variant="destructive" className="mb-6">
|
||||||
|
<AlertCircle className="h-4 w-4" />
|
||||||
|
<AlertTitle>Error</AlertTitle>
|
||||||
|
<AlertDescription>{errorMsg}</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
||||||
<FormField
|
<FormField
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user