2024-07-21 14:12:19 -05:00
|
|
|
import React, { useState, useEffect, useCallback } from "react";
|
2024-03-20 19:42:28 -05:00
|
|
|
import axios from "axios";
|
2024-03-20 13:00:05 -05:00
|
|
|
import { InputText } from "primereact/inputtext";
|
|
|
|
import { InputNumber } from "primereact/inputnumber";
|
|
|
|
import { InputSwitch } from "primereact/inputswitch";
|
|
|
|
import { Button } from "primereact/button";
|
2024-08-06 19:52:06 -05:00
|
|
|
import { useRouter } from "next/router";;
|
2024-03-20 19:42:28 -05:00
|
|
|
import { useLocalStorageWithEffect } from "@/hooks/useLocalStorage";
|
2024-03-24 14:16:55 -05:00
|
|
|
import { useToast } from "@/hooks/useToast";
|
2024-07-21 14:12:19 -05:00
|
|
|
import dynamic from 'next/dynamic';
|
|
|
|
const MDEditor = dynamic(
|
|
|
|
() => import("@uiw/react-md-editor"),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
}
|
|
|
|
);
|
2024-03-20 13:00:05 -05:00
|
|
|
import 'primeicons/primeicons.css';
|
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
const ResourceForm = ({ draft = null }) => {
|
|
|
|
const [title, setTitle] = useState(draft?.title || '');
|
|
|
|
const [summary, setSummary] = useState(draft?.summary || '');
|
|
|
|
const [isPaidResource, setIsPaidResource] = useState(draft?.price ? true : false);
|
|
|
|
const [price, setPrice] = useState(draft?.price || 0);
|
|
|
|
const [coverImage, setCoverImage] = useState(draft?.image || '');
|
|
|
|
const [topics, setTopics] = useState(draft?.topics || ['']);
|
2024-07-21 14:12:19 -05:00
|
|
|
const [content, setContent] = useState(draft?.content || '');
|
2024-03-20 19:42:28 -05:00
|
|
|
|
|
|
|
const [user] = useLocalStorageWithEffect('user', {});
|
2024-03-24 14:16:55 -05:00
|
|
|
const { showToast } = useToast();
|
2024-03-25 13:39:32 -05:00
|
|
|
const router = useRouter();
|
2024-03-24 14:16:55 -05:00
|
|
|
|
2024-07-21 14:12:19 -05:00
|
|
|
const handleContentChange = useCallback((value) => {
|
|
|
|
setContent(value || '');
|
|
|
|
}, []);
|
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (draft) {
|
|
|
|
setTitle(draft.title);
|
|
|
|
setSummary(draft.summary);
|
|
|
|
setIsPaidResource(draft.price ? true : false);
|
|
|
|
setPrice(draft.price || 0);
|
2024-07-22 16:11:46 -05:00
|
|
|
setContent(draft.content);
|
2024-07-20 12:40:53 -05:00
|
|
|
setCoverImage(draft.image);
|
|
|
|
setTopics(draft.topics || []);
|
|
|
|
}
|
|
|
|
}, [draft]);
|
|
|
|
|
2024-03-25 13:39:32 -05:00
|
|
|
const handleSubmit = async (e) => {
|
|
|
|
e.preventDefault();
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-03-25 13:39:32 -05:00
|
|
|
const userResponse = await axios.get(`/api/users/${user.pubkey}`);
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-03-24 14:16:55 -05:00
|
|
|
if (!userResponse.data) {
|
|
|
|
showToast('error', 'Error', 'User not found', 'Please try again.');
|
|
|
|
return;
|
|
|
|
}
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-03-25 13:39:32 -05:00
|
|
|
const payload = {
|
|
|
|
title,
|
|
|
|
summary,
|
|
|
|
type: 'resource',
|
|
|
|
price: isPaidResource ? price : null,
|
2024-07-21 14:12:19 -05:00
|
|
|
content,
|
2024-03-25 13:39:32 -05:00
|
|
|
image: coverImage,
|
2024-03-27 14:44:54 -05:00
|
|
|
topics: [...topics.map(topic => topic.trim().toLowerCase()), 'plebdevs', 'resource']
|
2024-03-20 19:42:28 -05:00
|
|
|
};
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
if (!draft) {
|
|
|
|
// Only include user when creating a new draft
|
|
|
|
payload.user = userResponse.data.id;
|
|
|
|
}
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
if (payload) {
|
|
|
|
const url = draft ? `/api/drafts/${draft.id}` : '/api/drafts';
|
|
|
|
const method = draft ? 'put' : 'post';
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
axios[method](url, payload)
|
2024-03-25 13:39:32 -05:00
|
|
|
.then(response => {
|
2024-07-20 12:40:53 -05:00
|
|
|
if (response.status === 200 || response.status === 201) {
|
|
|
|
showToast('success', 'Success', draft ? 'Resource updated successfully.' : 'Resource saved as draft.');
|
2024-07-21 14:12:19 -05:00
|
|
|
|
2024-03-25 13:39:32 -05:00
|
|
|
if (response.data?.id) {
|
|
|
|
router.push(`/draft/${response.data.id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2024-07-20 12:40:53 -05:00
|
|
|
showToast('error', 'Error', 'Failed to save resource. Please try again.');
|
2024-03-25 13:39:32 -05:00
|
|
|
});
|
2024-03-24 14:16:55 -05:00
|
|
|
}
|
2024-03-25 13:39:32 -05:00
|
|
|
};
|
2024-03-24 14:16:55 -05:00
|
|
|
|
2024-03-20 19:42:28 -05:00
|
|
|
const handleTopicChange = (index, value) => {
|
|
|
|
const updatedTopics = topics.map((topic, i) => i === index ? value : topic);
|
|
|
|
setTopics(updatedTopics);
|
|
|
|
};
|
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
const addTopic = (e) => {
|
|
|
|
e.preventDefault();
|
2024-03-20 19:42:28 -05:00
|
|
|
setTopics([...topics, '']); // Add an empty string to the topics array
|
|
|
|
};
|
|
|
|
|
2024-07-20 12:40:53 -05:00
|
|
|
const removeTopic = (e, index) => {
|
|
|
|
e.preventDefault();
|
2024-03-20 19:42:28 -05:00
|
|
|
const updatedTopics = topics.filter((_, i) => i !== index);
|
|
|
|
setTopics(updatedTopics);
|
|
|
|
};
|
|
|
|
|
2024-03-20 13:00:05 -05:00
|
|
|
return (
|
2024-07-21 17:02:40 -05:00
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className="p-inputgroup flex-1">
|
|
|
|
<InputText value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Title" />
|
|
|
|
</div>
|
|
|
|
<div className="p-inputgroup flex-1 mt-4">
|
|
|
|
<InputText value={summary} onChange={(e) => setSummary(e.target.value)} placeholder="Summary" />
|
|
|
|
</div>
|
|
|
|
<div className="p-inputgroup flex-1 mt-4">
|
|
|
|
<InputText value={coverImage} onChange={(e) => setCoverImage(e.target.value)} placeholder="Cover Image URL" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="p-inputgroup flex-1 mt-8 flex-col">
|
|
|
|
<p className="py-2">Paid Resource</p>
|
|
|
|
<InputSwitch checked={isPaidResource} onChange={(e) => setIsPaidResource(e.value)} />
|
|
|
|
{isPaidResource && (
|
|
|
|
<div className="p-inputgroup flex-1 py-4">
|
|
|
|
<InputNumber value={price} onValueChange={(e) => setPrice(e.value)} placeholder="Price (sats)" />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="p-inputgroup flex-1 flex-col mt-4">
|
|
|
|
<span>Content</span>
|
|
|
|
<div data-color-mode="dark">
|
2024-07-21 14:12:19 -05:00
|
|
|
<MDEditor
|
|
|
|
value={content}
|
|
|
|
onChange={handleContentChange}
|
2024-07-21 17:02:40 -05:00
|
|
|
height={350}
|
2024-07-21 14:12:19 -05:00
|
|
|
/>
|
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
</div>
|
|
|
|
<div className="mt-8 flex-col w-full">
|
|
|
|
{topics.map((topic, index) => (
|
|
|
|
<div className="p-inputgroup flex-1" key={index}>
|
|
|
|
<InputText value={topic} onChange={(e) => handleTopicChange(index, e.target.value)} placeholder="Topic" className="w-full mt-2" />
|
|
|
|
{index > 0 && (
|
|
|
|
<Button icon="pi pi-times" className="p-button-danger mt-2" onClick={(e) => removeTopic(e, index)} />
|
|
|
|
)}
|
2024-03-20 19:42:28 -05:00
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
))}
|
|
|
|
<div className="w-full flex flex-row items-end justify-end py-2">
|
|
|
|
<Button icon="pi pi-plus" onClick={addTopic} />
|
2024-03-20 19:42:28 -05:00
|
|
|
</div>
|
2024-07-21 17:02:40 -05:00
|
|
|
</div>
|
|
|
|
<div className="flex justify-center mt-8">
|
|
|
|
<Button type="submit" severity="success" outlined label={draft ? "Update" : "Submit"} />
|
|
|
|
</div>
|
|
|
|
</form>
|
2024-03-20 13:00:05 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ResourceForm;
|