2024-03-26 18:14:32 -05:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
import axios from "axios";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { Button } from "primereact/button";
|
|
|
|
import MenuTab from "@/components/menutab/MenuTab";
|
|
|
|
import { useLocalStorageWithEffect } from "@/hooks/useLocalStorage";
|
2024-03-26 18:54:46 -05:00
|
|
|
import { useNostr } from "@/hooks/useNostr";
|
2024-03-26 18:14:32 -05:00
|
|
|
import ContentList from "@/components/content/lists/ContentList";
|
2024-03-26 18:54:46 -05:00
|
|
|
import { parseEvent } from "@/utils/nostr";
|
|
|
|
import { useToast } from "@/hooks/useToast";
|
2024-03-26 18:14:32 -05:00
|
|
|
|
|
|
|
const UserContent = () => {
|
|
|
|
const [activeIndex, setActiveIndex] = useState(0);
|
2024-03-26 18:54:46 -05:00
|
|
|
const [drafts, setDrafts] = useState([]);
|
2024-03-26 18:14:32 -05:00
|
|
|
const [user, setUser] = useLocalStorageWithEffect('user', {});
|
2024-03-26 18:54:46 -05:00
|
|
|
const { fetchCourses, fetchResources, fetchWorkshops, events } = useNostr();
|
2024-03-26 18:14:32 -05:00
|
|
|
const router = useRouter();
|
2024-03-26 18:54:46 -05:00
|
|
|
const { showToast } = useToast();
|
2024-03-26 18:14:32 -05:00
|
|
|
|
2024-03-26 18:54:46 -05:00
|
|
|
const contentItems = [
|
|
|
|
{ label: 'Published', icon: 'pi pi-verified' },
|
2024-03-26 18:14:32 -05:00
|
|
|
{ label: 'Drafts', icon: 'pi pi-file-edit' },
|
|
|
|
{ label: 'Resources', icon: 'pi pi-book' },
|
|
|
|
{ label: 'Workshops', icon: 'pi pi-video' },
|
|
|
|
{ label: 'Courses', icon: 'pi pi-desktop' }
|
|
|
|
];
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-03-26 18:54:46 -05:00
|
|
|
if (user && user.id) {
|
|
|
|
fetchAllContent();
|
|
|
|
}
|
2024-03-26 18:14:32 -05:00
|
|
|
}, [user]);
|
|
|
|
|
2024-03-26 18:54:46 -05:00
|
|
|
const fetchAllContent = async () => {
|
|
|
|
try {
|
2024-03-27 14:44:54 -05:00
|
|
|
console.log(user.id)
|
2024-03-26 18:54:46 -05:00
|
|
|
// Fetch drafts from the database
|
|
|
|
const draftsResponse = await axios.get(`/api/drafts/all/${user.id}`);
|
|
|
|
const drafts = draftsResponse.data;
|
2024-03-27 14:44:54 -05:00
|
|
|
console.log('drafts:', drafts);
|
2024-03-26 18:54:46 -05:00
|
|
|
|
|
|
|
// Fetch resources, workshops, and courses from Nostr
|
|
|
|
fetchResources();
|
|
|
|
fetchWorkshops();
|
|
|
|
fetchCourses();
|
|
|
|
|
|
|
|
if (drafts.length > 0) {
|
|
|
|
setDrafts(drafts);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
showToast('error', 'Error', 'Failed to fetch content');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getContentByIndex = (index) => {
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return []
|
|
|
|
case 1:
|
|
|
|
return drafts;
|
|
|
|
case 2:
|
|
|
|
return events.resources.map(resource => {
|
|
|
|
const { id, content, title, summary, image, published_at } = parseEvent(resource);
|
|
|
|
return { id, content, title, summary, image, published_at };
|
|
|
|
});
|
|
|
|
case 3:
|
|
|
|
return events.workshops.map(workshop => {
|
|
|
|
const { id, content, title, summary, image, published_at } = parseEvent(workshop);
|
|
|
|
return { id, content, title, summary, image, published_at };
|
|
|
|
})
|
|
|
|
case 4:
|
|
|
|
return events.courses.map(course => {
|
|
|
|
const { id, content, title, summary, image, published_at } = parseEvent(course);
|
|
|
|
return { id, content, title, summary, image, published_at };
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-26 18:14:32 -05:00
|
|
|
return (
|
|
|
|
<div className="w-[90vw] mx-auto max-tab:w-[100vw] max-mob:w-[100vw]">
|
|
|
|
<div className="border-y-2 border-gray-300 mt-12">
|
|
|
|
<h2 className="text-center my-4">Your Content</h2>
|
|
|
|
</div>
|
|
|
|
<div className="flex flex-row w-full justify-between px-4">
|
2024-03-26 18:54:46 -05:00
|
|
|
<MenuTab items={contentItems} activeIndex={activeIndex} onTabChange={setActiveIndex} />
|
2024-03-26 18:14:32 -05:00
|
|
|
<Button onClick={() => router.push('/create')} label="Create" severity="success" outlined className="mt-2" />
|
|
|
|
</div>
|
|
|
|
<div className="w-full mx-auto my-8">
|
2024-03-26 18:54:46 -05:00
|
|
|
<div className="w-full mx-auto my-8">
|
|
|
|
{getContentByIndex(activeIndex).length > 0 && (
|
|
|
|
<ContentList content={getContentByIndex(activeIndex)} />
|
|
|
|
)}
|
|
|
|
</div>
|
2024-03-26 18:14:32 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-26 18:54:46 -05:00
|
|
|
);
|
|
|
|
};
|
2024-03-26 18:14:32 -05:00
|
|
|
|
|
|
|
export default UserContent;
|