import React, { useState } from "react"; import axios from "axios"; import { InputText } from "primereact/inputtext"; import { InputNumber } from "primereact/inputnumber"; import { InputSwitch } from "primereact/inputswitch"; import { Editor } from "primereact/editor"; import { Button } from "primereact/button"; import { nip04, verifyEvent, nip19 } from "nostr-tools"; import { useNostr } from "@/hooks/useNostr"; import { v4 as uuidv4 } from 'uuid'; import { useLocalStorageWithEffect } from "@/hooks/useLocalStorage"; import 'primeicons/primeicons.css'; const ResourceForm = () => { const [title, setTitle] = useState(''); const [summary, setSummary] = useState(''); const [checked, setChecked] = useState(false); const [price, setPrice] = useState(0); const [text, setText] = useState(''); const [topics, setTopics] = useState(['']); // Initialize with an empty string to show one input by default const [user] = useLocalStorageWithEffect('user', {}); const { publishAll } = useNostr(); const handleSubmit = (e) => { e.preventDefault(); // Prevents the default form submission mechanism const payload = { title, summary, isPaidResource: checked, price: checked ? price : null, content: text, topics: topics.map(topic => topic.trim().toLowerCase()) // Process topics as they are }; buildPaidResource(payload); }; // For images, whether included in the markdown content or not, clients SHOULD use image tags as described in NIP-58. This allows clients to display images in carousel format more easily. const buildPaidResource = async (payload) => { // encrypt the content with NEXT_PUBLIC_APP_PRIV_KEY to NEXT_PUBLIC_APP_PUBLIC_KEY const encryptedContent = await nip04.encrypt(process.env.NEXT_PUBLIC_APP_PRIV_KEY ,process.env.NEXT_PUBLIC_APP_PUBLIC_KEY, payload.content); const newresourceId = uuidv4(); const event = { kind: 30402, content: encryptedContent, created_at: Math.floor(Date.now() / 1000), tags: [ ['title', payload.title], ['summary', payload.summary], ['t', ...topics], ['image', ''], ['d', newresourceId], ['location', `https://plebdevs.com/resource/${newresourceId}`], ['published_at', Math.floor(Date.now() / 1000).toString()], ['price', payload.price] ] }; console.log('event:', event); // Will need to add d tag from db id // will need to add url/id as location tag // first sign the event const signedEvent = await window.nostr.signEvent(event); const eventVerification = await verifyEvent(signedEvent); console.log('eventVerification:', eventVerification); console.log('signedEvent:', signedEvent); const nAddress = nip19.naddrEncode({ pubkey: signedEvent.pubkey, kind: signedEvent.kind, identifier: newresourceId, }) console.log('nAddress:', nAddress); const userResponse = await axios.get(`/api/users/${user.pubkey}`) console.log('userResponse:', userResponse); const resourcePayload = { id: newresourceId, userId: userResponse.data.id, price: payload.price || 0, noteId: nAddress, } const response = await axios.post(`/api/resources`, resourcePayload); console.log('response:', response); const publishResponse = await publishAll(signedEvent); console.log('publishResponse:', publishResponse); } const handleTopicChange = (index, value) => { const updatedTopics = topics.map((topic, i) => i === index ? value : topic); setTopics(updatedTopics); }; const addTopic = () => { setTopics([...topics, '']); // Add an empty string to the topics array }; const removeTopic = (index) => { const updatedTopics = topics.filter((_, i) => i !== index); setTopics(updatedTopics); }; return (
); } export default ResourceForm;