2024-08-06 19:52:06 -05:00
|
|
|
import React, { useState } from "react";
|
2024-04-24 12:57:46 -05:00
|
|
|
import axios from "axios";
|
2024-03-20 14:20:45 -05:00
|
|
|
import { InputText } from "primereact/inputtext";
|
|
|
|
import { InputNumber } from "primereact/inputnumber";
|
|
|
|
import { InputSwitch } from "primereact/inputswitch";
|
|
|
|
import { Button } from "primereact/button";
|
2024-04-24 12:57:46 -05:00
|
|
|
import { Dropdown } from "primereact/dropdown";
|
2024-08-06 19:52:06 -05:00
|
|
|
import { ProgressSpinner } from "primereact/progressspinner";
|
2024-04-28 17:00:58 -05:00
|
|
|
import { v4 as uuidv4, v4 } from 'uuid';
|
2024-04-24 12:57:46 -05:00
|
|
|
import { useLocalStorageWithEffect } from "@/hooks/useLocalStorage";
|
2024-08-06 19:52:06 -05:00
|
|
|
import { useNDKContext } from "@/context/NDKContext";
|
2024-07-21 19:56:55 -05:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useToast } from "@/hooks/useToast";
|
2024-08-06 19:52:06 -05:00
|
|
|
import { useWorkshopsQuery } from "@/hooks/nostrQueries/content/useWorkshopsQuery";
|
|
|
|
import { useResourcesQuery } from "@/hooks/nostrQueries/content/useResourcesQuery";
|
|
|
|
import { useDraftsQuery } from "@/hooks/apiQueries/useDraftsQuery";
|
|
|
|
import { NDKEvent } from "@nostr-dev-kit/ndk";
|
2024-04-24 12:57:46 -05:00
|
|
|
import { parseEvent } from "@/utils/nostr";
|
|
|
|
import ContentDropdownItem from "@/components/content/dropdowns/ContentDropdownItem";
|
2024-03-20 14:20:45 -05:00
|
|
|
import 'primeicons/primeicons.css';
|
|
|
|
|
|
|
|
const CourseForm = () => {
|
|
|
|
const [title, setTitle] = useState('');
|
|
|
|
const [summary, setSummary] = useState('');
|
|
|
|
const [checked, setChecked] = useState(false);
|
|
|
|
const [price, setPrice] = useState(0);
|
2024-03-25 10:23:42 -05:00
|
|
|
const [coverImage, setCoverImage] = useState('');
|
2024-04-24 12:57:46 -05:00
|
|
|
const [lessons, setLessons] = useState([{ id: uuidv4(), title: 'Select a lesson' }]);
|
|
|
|
const [selectedLessons, setSelectedLessons] = useState([]);
|
2024-03-21 12:54:06 -05:00
|
|
|
const [topics, setTopics] = useState(['']);
|
2024-03-20 14:20:45 -05:00
|
|
|
|
2024-08-06 19:52:06 -05:00
|
|
|
const { resources, resourcesLoading, resourcesError } = useResourcesQuery();
|
|
|
|
const { workshops, workshopsLoading, workshopsError } = useWorkshopsQuery();
|
|
|
|
const { drafts, draftsLoading, draftsError } = useDraftsQuery();
|
|
|
|
const [user, setUser] = useLocalStorageWithEffect('user', {});
|
|
|
|
const ndk = useNDKContext();
|
2024-07-21 19:56:55 -05:00
|
|
|
const router = useRouter();
|
|
|
|
const { showToast } = useToast();
|
|
|
|
|
2024-08-06 19:52:06 -05:00
|
|
|
/**
|
|
|
|
* Course Creation Flow:
|
|
|
|
* 1. Generate a new course ID
|
|
|
|
* 2. Process each lesson:
|
|
|
|
* - If unpublished: create event, publish to Nostr, save to DB, delete draft
|
|
|
|
* - If published: use existing data
|
|
|
|
* 3. Create and publish course event to Nostr
|
|
|
|
* 4. Save course to database
|
|
|
|
* 5. Show success message and redirect to course page
|
|
|
|
*/
|
2024-07-22 16:11:46 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const handleSubmit = async (e) => {
|
2024-03-20 14:20:45 -05:00
|
|
|
e.preventDefault();
|
2024-04-28 17:00:58 -05:00
|
|
|
|
2024-07-22 16:11:46 -05:00
|
|
|
const newCourseId = uuidv4();
|
|
|
|
const processedLessons = [];
|
2024-04-28 17:00:58 -05:00
|
|
|
|
2024-07-22 16:11:46 -05:00
|
|
|
try {
|
|
|
|
// Step 1: Process lessons
|
|
|
|
for (const lesson of selectedLessons) {
|
|
|
|
let noteId = lesson.noteId;
|
|
|
|
|
|
|
|
if (!lesson.published_at) {
|
|
|
|
// Publish unpublished lesson
|
|
|
|
const event = createLessonEvent(lesson);
|
2024-08-06 19:52:06 -05:00
|
|
|
const published = await event.publish();
|
2024-07-22 16:11:46 -05:00
|
|
|
|
|
|
|
if (!published) {
|
|
|
|
throw new Error(`Failed to publish lesson: ${lesson.title}`);
|
|
|
|
}
|
2024-04-28 17:00:58 -05:00
|
|
|
|
2024-08-06 19:52:06 -05:00
|
|
|
noteId = event.id;
|
2024-07-22 16:11:46 -05:00
|
|
|
|
|
|
|
// Save to db and delete draft
|
|
|
|
await Promise.all([
|
|
|
|
axios.post('/api/resources', {
|
|
|
|
id: lesson.id,
|
|
|
|
noteId: noteId,
|
|
|
|
userId: user.id,
|
|
|
|
price: lesson.price || 0,
|
|
|
|
}),
|
|
|
|
axios.delete(`/api/drafts/${lesson.id}`)
|
|
|
|
]);
|
|
|
|
}
|
2024-07-29 17:26:49 -05:00
|
|
|
// if the lesson was already published we will have d tag, otherwise we will have id
|
|
|
|
// if the lesson was already published we will have kind tag, otherwise we will use price tag to determine the kind
|
|
|
|
// if the lesson was already published we will have pubkey tag, otherwise we will use user.pubkey
|
|
|
|
processedLessons.push({
|
|
|
|
d: lesson?.d || lesson.id,
|
|
|
|
kind: lesson.kind ?? (lesson.price ? 30402 : 30023),
|
|
|
|
pubkey: lesson.pubkey || user.pubkey
|
|
|
|
});
|
2024-07-22 16:11:46 -05:00
|
|
|
}
|
2024-04-28 17:00:58 -05:00
|
|
|
|
2024-07-22 16:11:46 -05:00
|
|
|
// Step 2: Create and publish course
|
2024-07-29 17:26:49 -05:00
|
|
|
const courseEvent = createCourseEvent(newCourseId, title, summary, coverImage, processedLessons);
|
2024-08-06 19:52:06 -05:00
|
|
|
const published = await courseEvent.publish();
|
|
|
|
|
|
|
|
console.log('published', published);
|
2024-07-20 10:51:16 -05:00
|
|
|
|
2024-07-22 16:11:46 -05:00
|
|
|
if (!published) {
|
|
|
|
throw new Error('Failed to publish course');
|
2024-07-20 10:51:16 -05:00
|
|
|
}
|
2024-07-22 16:11:46 -05:00
|
|
|
|
|
|
|
// Step 3: Save course to db
|
|
|
|
console.log('processedLessons:', processedLessons);
|
|
|
|
await axios.post('/api/courses', {
|
|
|
|
id: newCourseId,
|
|
|
|
resources: {
|
2024-07-29 17:26:49 -05:00
|
|
|
connect: processedLessons.map(lesson => ({ id: lesson?.d }))
|
2024-07-22 16:11:46 -05:00
|
|
|
},
|
2024-08-06 19:52:06 -05:00
|
|
|
noteId: courseEvent.id,
|
2024-07-22 16:11:46 -05:00
|
|
|
user: {
|
|
|
|
connect: { id: user.id }
|
|
|
|
},
|
|
|
|
price: price || 0
|
|
|
|
});
|
|
|
|
|
2024-07-29 17:26:49 -05:00
|
|
|
// step 4: Update all resources to have the course id
|
|
|
|
await Promise.all(processedLessons.map(lesson => axios.put(`/api/resources/${lesson?.d}`, { courseId: newCourseId })));
|
|
|
|
|
|
|
|
// Step 5: Show success message and redirect
|
2024-07-22 16:11:46 -05:00
|
|
|
showToast('success', 'Course created successfully');
|
2024-08-06 19:52:06 -05:00
|
|
|
router.push(`/course/${courseEvent.id}`);
|
2024-07-22 16:11:46 -05:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error creating course:', error);
|
|
|
|
showToast('error', error.message || 'Failed to create course. Please try again.');
|
2024-04-28 17:00:58 -05:00
|
|
|
}
|
2024-04-24 12:57:46 -05:00
|
|
|
};
|
2024-03-20 14:20:45 -05:00
|
|
|
|
2024-08-06 19:52:06 -05:00
|
|
|
const createLessonEvent = (lesson) => {
|
|
|
|
const event = new NDKEvent(ndk);
|
|
|
|
event.kind = lesson.price ? 30402 : 30023;
|
|
|
|
event.content = lesson.content;
|
|
|
|
event.tags = [
|
2024-07-22 16:11:46 -05:00
|
|
|
['d', lesson.id],
|
|
|
|
['title', lesson.title],
|
|
|
|
['summary', lesson.summary],
|
|
|
|
['image', lesson.image],
|
|
|
|
...lesson.topics.map(topic => ['t', topic]),
|
|
|
|
['published_at', Math.floor(Date.now() / 1000).toString()],
|
2024-08-06 19:52:06 -05:00
|
|
|
];
|
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createCourseEvent = (courseId, title, summary, coverImage, lessons) => {
|
|
|
|
const event = new NDKEvent(ndk);
|
|
|
|
event.kind = 30004;
|
|
|
|
event.content = "";
|
|
|
|
event.tags = [
|
2024-07-22 16:11:46 -05:00
|
|
|
['d', courseId],
|
|
|
|
['name', title],
|
|
|
|
['picture', coverImage],
|
|
|
|
['image', coverImage],
|
|
|
|
['description', summary],
|
|
|
|
['l', "Education"],
|
|
|
|
...lessons.map((lesson) => ['a', `${lesson.kind}:${lesson.pubkey}:${lesson.d}`]),
|
2024-08-06 19:52:06 -05:00
|
|
|
];
|
|
|
|
return event;
|
|
|
|
};
|
2024-07-22 16:11:46 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const handleLessonChange = (e, index) => {
|
|
|
|
const selectedLessonId = e.value;
|
|
|
|
const selectedLesson = getContentOptions(index).flatMap(group => group.items).find(lesson => lesson.value === selectedLessonId);
|
2024-04-24 14:30:40 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const updatedLessons = lessons.map((lesson, i) =>
|
|
|
|
i === index ? { ...lesson, id: selectedLessonId, title: selectedLesson.label.props.content.title } : lesson
|
|
|
|
);
|
|
|
|
setLessons(updatedLessons);
|
2024-03-21 12:54:06 -05:00
|
|
|
};
|
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const handleLessonSelect = (content) => {
|
|
|
|
setSelectedLessons([...selectedLessons, content]);
|
|
|
|
addLesson();
|
2024-03-21 12:54:06 -05:00
|
|
|
};
|
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const addLesson = () => {
|
|
|
|
setLessons([...lessons, { id: uuidv4(), title: 'Select a lesson' }]);
|
2024-03-21 12:54:06 -05:00
|
|
|
};
|
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const removeLesson = (index) => {
|
|
|
|
const updatedLessons = lessons.filter((_, i) => i !== index);
|
|
|
|
const updatedSelectedLessons = selectedLessons.filter((_, i) => i !== index);
|
2024-04-24 14:30:40 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
if (updatedLessons.length === 0) {
|
|
|
|
updatedLessons.push({ id: uuidv4(), title: 'Select a lesson' });
|
|
|
|
}
|
2024-04-24 14:30:40 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
setLessons(updatedLessons);
|
|
|
|
setSelectedLessons(updatedSelectedLessons);
|
2024-03-21 12:54:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const addTopic = () => {
|
2024-04-24 12:57:46 -05:00
|
|
|
setTopics([...topics, '']);
|
2024-03-21 12:54:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const removeTopic = (index) => {
|
|
|
|
const updatedTopics = topics.filter((_, i) => i !== index);
|
|
|
|
setTopics(updatedTopics);
|
|
|
|
};
|
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const handleTopicChange = (index, value) => {
|
|
|
|
const updatedTopics = topics.map((topic, i) => i === index ? value : topic);
|
|
|
|
setTopics(updatedTopics);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getContentOptions = (index) => {
|
2024-08-06 19:52:06 -05:00
|
|
|
if (resourcesLoading || !resources || workshopsLoading || !workshops || draftsLoading || !drafts) {
|
|
|
|
return [];
|
|
|
|
}
|
2024-04-24 12:57:46 -05:00
|
|
|
const draftOptions = drafts.map(draft => ({
|
|
|
|
label: <ContentDropdownItem content={draft} onSelect={(content) => handleLessonSelect(content, index)} selected={lessons[index] && lessons[index].id === draft.id} />,
|
|
|
|
value: draft.id
|
|
|
|
}));
|
2024-04-24 14:30:40 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const resourceOptions = resources.map(resource => {
|
2024-07-22 16:11:46 -05:00
|
|
|
const { id, kind, pubkey, content, title, summary, image, published_at, d, topics } = parseEvent(resource);
|
2024-04-24 12:57:46 -05:00
|
|
|
return {
|
2024-07-22 16:11:46 -05:00
|
|
|
label: <ContentDropdownItem content={{ id, kind, pubkey, content, title, summary, image, published_at, d, topics }} onSelect={(content) => handleLessonSelect(content, index)} selected={lessons[index] && lessons[index].id === id} />,
|
2024-04-24 12:57:46 -05:00
|
|
|
value: id
|
|
|
|
};
|
|
|
|
});
|
2024-04-24 14:30:40 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
const workshopOptions = workshops.map(workshop => {
|
2024-07-22 16:11:46 -05:00
|
|
|
const { id, kind, pubkey, content, title, summary, image, published_at, d, topics } = parseEvent(workshop);
|
2024-04-24 12:57:46 -05:00
|
|
|
return {
|
2024-07-22 16:11:46 -05:00
|
|
|
label: <ContentDropdownItem content={{ id, kind, pubkey, content, title, summary, image, published_at, d, topics }} onSelect={(content) => handleLessonSelect(content, index)} selected={lessons[index] && lessons[index].id === id} />,
|
2024-04-24 12:57:46 -05:00
|
|
|
value: id
|
|
|
|
};
|
|
|
|
});
|
2024-04-24 14:30:40 -05:00
|
|
|
|
2024-04-24 12:57:46 -05:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
label: 'Drafts',
|
|
|
|
items: draftOptions
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Resources',
|
|
|
|
items: resourceOptions
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Workshops',
|
|
|
|
items: workshopOptions
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-08-06 19:52:06 -05:00
|
|
|
// const lessonOptions = getContentOptions();
|
|
|
|
if (resourcesLoading || workshopsLoading || draftsLoading) {
|
|
|
|
return <ProgressSpinner />;
|
|
|
|
}
|
2024-04-24 12:57:46 -05:00
|
|
|
|
2024-03-20 14:20:45 -05:00
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className="p-inputgroup flex-1">
|
|
|
|
<InputText value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Title" />
|
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
<div className="p-inputgroup flex-1 mt-4">
|
2024-03-20 14:20:45 -05:00
|
|
|
<InputText value={summary} onChange={(e) => setSummary(e.target.value)} placeholder="Summary" />
|
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
<div className="p-inputgroup flex-1 mt-4">
|
2024-03-25 10:23:42 -05:00
|
|
|
<InputText value={coverImage} onChange={(e) => setCoverImage(e.target.value)} placeholder="Cover Image URL" />
|
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
<div className="p-inputgroup flex-1 mt-4 flex-col">
|
2024-03-20 14:20:45 -05:00
|
|
|
<p className="py-2">Paid Course</p>
|
|
|
|
<InputSwitch checked={checked} onChange={(e) => setChecked(e.value)} />
|
2024-04-24 12:57:46 -05:00
|
|
|
{checked && (
|
|
|
|
<div className="p-inputgroup flex-1 py-4">
|
|
|
|
<InputNumber value={price} onValueChange={(e) => setPrice(e.value)} placeholder="Price (sats)" />
|
|
|
|
</div>
|
|
|
|
)}
|
2024-03-20 14:20:45 -05:00
|
|
|
</div>
|
2024-03-21 12:54:06 -05:00
|
|
|
<div className="mt-8 flex-col w-full">
|
2024-07-21 17:02:40 -05:00
|
|
|
<div className="mt-4 flex-col w-full">
|
2024-04-24 12:57:46 -05:00
|
|
|
{selectedLessons.map((lesson, index) => (
|
2024-07-21 17:02:40 -05:00
|
|
|
<div key={lesson.id} className="p-inputgroup flex-1 mt-4">
|
2024-04-24 12:57:46 -05:00
|
|
|
<ContentDropdownItem content={lesson} selected={true} />
|
|
|
|
<Button icon="pi pi-times" className="p-button-danger" onClick={() => removeLesson(index)} />
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
{lessons.map((lesson, index) => (
|
2024-07-21 17:02:40 -05:00
|
|
|
<div key={lesson.id} className="p-inputgroup flex-1 mt-4">
|
2024-04-24 12:57:46 -05:00
|
|
|
<Dropdown
|
|
|
|
value={lesson.title}
|
|
|
|
options={getContentOptions(index)}
|
|
|
|
onChange={(e) => handleLessonChange(e, index)}
|
|
|
|
placeholder="Select a Lesson"
|
|
|
|
itemTemplate={(option) => option.label}
|
|
|
|
optionLabel="label"
|
|
|
|
optionGroupLabel="label"
|
|
|
|
optionGroupChildren="items"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))}
|
2024-03-20 14:20:45 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
<div className="mt-4 flex-col w-full">
|
2024-03-21 12:54:06 -05:00
|
|
|
{topics.map((topic, index) => (
|
2024-07-21 17:02:40 -05:00
|
|
|
<div key={index} className="p-inputgroup flex-1 mt-4">
|
2024-04-24 12:57:46 -05:00
|
|
|
<InputText value={topic} onChange={(e) => handleTopicChange(index, e.target.value)} placeholder={`Topic #${index + 1}`} className="w-full" />
|
2024-03-21 12:54:06 -05:00
|
|
|
{index > 0 && (
|
|
|
|
<Button icon="pi pi-times" className="p-button-danger mt-2" onClick={() => removeTopic(index)} />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
2024-04-24 12:57:46 -05:00
|
|
|
<Button type="button" icon="pi pi-plus" onClick={addTopic} className="p-button-outlined mt-2" />
|
2024-03-21 12:54:06 -05:00
|
|
|
</div>
|
2024-03-20 14:20:45 -05:00
|
|
|
<div className="flex justify-center mt-8">
|
2024-04-24 12:57:46 -05:00
|
|
|
<Button type="submit" label="Submit" className="p-button-raised p-button-success" />
|
2024-03-20 14:20:45 -05:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-22 16:11:46 -05:00
|
|
|
export default CourseForm;
|