2025-04-02 17:47:30 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { useNDKContext } from '@/context/NDKContext';
|
2024-12-03 12:48:17 -06:00
|
|
|
import { useSession } from 'next-auth/react';
|
2025-04-02 17:47:30 -05:00
|
|
|
import { parseCourseEvent } from '@/utils/nostr';
|
2024-12-03 12:48:17 -06:00
|
|
|
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';
|
2024-12-03 12:48:17 -06:00
|
|
|
|
|
|
|
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();
|
2024-12-03 12:48:17 -06:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (!router.isReady || !session) return;
|
2024-12-03 12:48:17 -06:00
|
|
|
|
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] });
|
2024-12-03 12:48:17 -06:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
if (!event) {
|
|
|
|
showToast('error', 'Error', 'Course not found');
|
|
|
|
router.push('/dashboard');
|
|
|
|
return;
|
|
|
|
}
|
2024-12-03 12:48:17 -06:00
|
|
|
|
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;
|
|
|
|
}
|
2024-12-03 12:48:17 -06:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2024-12-03 12:48:17 -06:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
fetchCourse();
|
|
|
|
}, [router.isReady, router.query, ndk, session, showToast, router]);
|
2024-12-03 12:48:17 -06:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
if (loading) {
|
2024-12-03 12:48:17 -06:00
|
|
|
return (
|
2025-04-02 17:47:30 -05:00
|
|
|
<div className="w-full h-full flex items-center justify-center">
|
|
|
|
<ProgressSpinner />
|
|
|
|
</div>
|
2024-12-03 12:48:17 -06:00
|
|
|
);
|
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>
|
|
|
|
);
|
2024-12-03 12:48:17 -06:00
|
|
|
};
|
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
export default EditCourse;
|