74 lines
2.1 KiB
JavaScript
Raw Normal View History

2025-04-02 17:47:30 -05:00
import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { useNDKContext } from '@/context/NDKContext';
import { useSession } from 'next-auth/react';
2025-04-02 17:47:30 -05:00
import { parseCourseEvent } from '@/utils/nostr';
import { ProgressSpinner } from 'primereact/progressspinner';
2025-04-02 17:47:30 -05:00
import PublishedCourseForm from '@/components/forms/course/PublishedCourseForm';
import { useToast } from '@/hooks/useToast';
const EditCourse = () => {
2025-04-02 17:47:30 -05:00
const [course, setCourse] = useState(null);
const [loading, setLoading] = useState(true);
const router = useRouter();
const { ndk } = useNDKContext();
const { data: session } = useSession();
const { showToast } = useToast();
2025-04-02 17:47:30 -05:00
useEffect(() => {
if (!router.isReady || !session) return;
2025-04-02 17:47:30 -05:00
const fetchCourse = async () => {
try {
const { slug } = router.query;
await ndk.connect();
const event = await ndk.fetchEvent({ '#d': [slug] });
2025-04-02 17:47:30 -05:00
if (!event) {
showToast('error', 'Error', 'Course not found');
router.push('/dashboard');
return;
}
2025-04-02 17:47:30 -05:00
// Check if user is the author
if (event.pubkey !== session.user.pubkey) {
showToast('error', 'Error', 'Unauthorized');
router.push('/dashboard');
return;
}
2025-04-02 17:47:30 -05:00
const parsedCourse = parseCourseEvent(event);
setCourse(parsedCourse);
} catch (error) {
console.error('Error fetching course:', error);
showToast('error', 'Error', 'Failed to fetch course');
} finally {
setLoading(false);
}
};
2025-04-02 17:47:30 -05:00
fetchCourse();
}, [router.isReady, router.query, ndk, session, showToast, router]);
2025-04-02 17:47:30 -05:00
if (loading) {
return (
2025-04-02 17:47:30 -05:00
<div className="w-full h-full flex items-center justify-center">
<ProgressSpinner />
</div>
);
2025-04-02 17:47:30 -05:00
}
if (!course) {
return null;
}
return (
<div className="w-[80vw] max-w-[80vw] mx-auto my-8 flex flex-col justify-center">
<h2 className="text-center mb-8">Edit Course</h2>
<PublishedCourseForm course={course} />
</div>
);
};
2025-04-02 17:47:30 -05:00
export default EditCourse;