40 lines
1.4 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from "react";
2024-08-13 22:59:00 -05:00
import axios from "axios";
import { useRouter } from "next/router";
2024-08-24 18:00:22 -05:00
import CourseForm from "@/components/forms/course/CourseForm";
2024-08-13 22:59:00 -05:00
import { useNDKContext } from "@/context/NDKContext";
import { useToast } from "@/hooks/useToast";
export default function Edit() {
const [draft, setDraft] = useState(null);
const { ndk } = useNDKContext();
2024-08-13 22:59:00 -05:00
const router = useRouter();
const { showToast } = useToast();
useEffect(() => {
if (router.isReady) {
const { slug } = router.query;
const fetchDraft = async () => {
try {
const response = await axios.get(`/api/courses/drafts/${slug}`);
if (response.status === 200) {
setDraft(response.data);
} else {
showToast('error', 'Error', 'Draft not found.');
2024-08-13 22:59:00 -05:00
}
} catch (error) {
console.error('Error fetching draft:', error);
showToast('error', 'Error', 'Failed to fetch draft.');
}
2024-08-13 22:59:00 -05:00
}
fetchDraft();
2024-08-13 22:59:00 -05:00
}
}, [router.isReady, router.query, showToast]);
2024-08-13 22:59:00 -05:00
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 Draft</h2>
2024-08-24 18:00:22 -05:00
{draft && <CourseForm draft={draft} />}
2024-08-13 22:59:00 -05:00
</div>
);
}