-
-
Save Daltonic/c0a393251a129b49e3f768ff65006a30 to your computer and use it in GitHub Desktop.
Dapp Auction CreateNFT.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios' | |
| import { useState } from 'react' | |
| import { toast } from 'react-toastify' | |
| import { FaTimes } from 'react-icons/fa' | |
| import picture6 from '../assets/images/picture6.png' | |
| import { setGlobalState, useGlobalState } from '../store' | |
| import { createNftItem } from '../services/blockchain' | |
| const CreateNFT = () => { | |
| const [boxModal] = useGlobalState('boxModal') | |
| const [name, setName] = useState('') | |
| const [description, setDescription] = useState('') | |
| const [price, setPrice] = useState('') | |
| const [fileUrl, setFileUrl] = useState('') | |
| const [imgBase64, setImgBase64] = useState(null) | |
| const handleSubmit = async (e) => { | |
| e.preventDefault() | |
| if (!name || !price || !description || !fileUrl) return | |
| const formData = new FormData() | |
| formData.append('name', name) | |
| formData.append('price', price) | |
| formData.append('description', description) | |
| formData.append('image', fileUrl) | |
| await toast.promise( | |
| new Promise(async (resolve, reject) => { | |
| await axios | |
| .post('http://localhost:9000/process', formData) | |
| .then(async (res) => { | |
| await createNftItem(res.data) | |
| .then(async () => { | |
| closeModal() | |
| resolve() | |
| }) | |
| .catch(() => reject()) | |
| reject() | |
| }) | |
| .catch(() => reject()) | |
| }), | |
| { | |
| pending: 'Minting & saving data to chain...', | |
| success: 'Minting completed, will reflect within 30sec 👌', | |
| error: 'Encountered error 🤯', | |
| }, | |
| ) | |
| } | |
| const changeImage = async (e) => { | |
| const reader = new FileReader() | |
| if (e.target.files[0]) reader.readAsDataURL(e.target.files[0]) | |
| reader.onload = (readerEvent) => { | |
| const file = readerEvent.target.result | |
| setImgBase64(file) | |
| setFileUrl(e.target.files[0]) | |
| } | |
| } | |
| const closeModal = () => { | |
| setGlobalState('boxModal', 'scale-0') | |
| resetForm() | |
| } | |
| const resetForm = () => { | |
| setFileUrl('') | |
| setImgBase64(null) | |
| setName('') | |
| setPrice('') | |
| setDescription('') | |
| } | |
| return ( | |
| <div | |
| className={`fixed top-0 left-0 w-screen h-screen flex items-center | |
| justify-center bg-black bg-opacity-50 transform | |
| transition-transform duration-300 ${boxModal}`} | |
| > | |
| <div className="bg-[#151c25] shadow-xl shadow-[#25bd9c] rounded-xl w-11/12 md:w-2/5 h-7/12 p-6"> | |
| <form onSubmit={handleSubmit} className="flex flex-col"> | |
| <div className="flex flex-row justify-between items-center"> | |
| <p className="font-semibold text-gray-400 italic">Add NFT</p> | |
| <button | |
| type="button" | |
| onClick={closeModal} | |
| className="border-0 bg-transparent focus:outline-none" | |
| > | |
| <FaTimes className="text-gray-400" /> | |
| </button> | |
| </div> | |
| <div className="flex flex-row justify-center items-center rounded-xl mt-5"> | |
| <div className="shrink-0 rounded-xl overflow-hidden h-20 w-20"> | |
| <img | |
| alt="NFT" | |
| className="h-full w-full object-cover cursor-pointer" | |
| src={imgBase64 || picture6} | |
| /> | |
| </div> | |
| </div> | |
| <div className="flex flex-row justify-between items-center bg-gray-800 rounded-xl mt-5"> | |
| <label className="block"> | |
| <span className="sr-only">Choose profile photo</span> | |
| <input | |
| type="file" | |
| accept="image/png, image/gif, image/jpeg, image/webp" | |
| className="block w-full text-sm text-slate-500 | |
| file:mr-4 file:py-2 file:px-4 | |
| file:rounded-full file:border-0 | |
| file:text-sm file:font-semibold | |
| file:bg-[#19212c] file:text-gray-300 | |
| hover:file:bg-[#1d2631] | |
| cursor-pointer focus:ring-0 focus:outline-none" | |
| onChange={changeImage} | |
| required | |
| /> | |
| </label> | |
| </div> | |
| <div className="flex flex-row justify-between items-center bg-gray-800 rounded-xl mt-5"> | |
| <input | |
| className="block w-full text-sm | |
| text-slate-500 bg-transparent border-0 | |
| focus:outline-none focus:ring-0 px-4 py-2" | |
| type="text" | |
| name="name" | |
| placeholder="Title" | |
| onChange={(e) => setName(e.target.value)} | |
| value={name} | |
| required | |
| /> | |
| </div> | |
| <div className="flex flex-row justify-between items-center bg-gray-800 rounded-xl mt-5"> | |
| <input | |
| className="block w-full text-sm | |
| text-slate-500 bg-transparent border-0 | |
| focus:outline-none focus:ring-0 px-4 py-2" | |
| type="number" | |
| name="price" | |
| step={0.01} | |
| min={0.01} | |
| placeholder="Price (Eth)" | |
| onChange={(e) => setPrice(e.target.value)} | |
| value={price} | |
| required | |
| /> | |
| </div> | |
| <div className="flex flex-row justify-between items-center bg-gray-800 rounded-xl mt-5"> | |
| <textarea | |
| className="block w-full text-sm resize-none | |
| text-slate-500 bg-transparent border-0 | |
| focus:outline-none focus:ring-0 h-18 py-2 px-4" | |
| type="text" | |
| name="description" | |
| placeholder="Description" | |
| onChange={(e) => setDescription(e.target.value)} | |
| value={description} | |
| required | |
| ></textarea> | |
| </div> | |
| <button | |
| type="submit" | |
| className="flex flex-row justify-center items-center | |
| w-full text-white text-md bg-[#25bd9c] | |
| py-2 px-5 rounded-full | |
| drop-shadow-xl border border-transparent | |
| hover:bg-transparent hover:text-[#ffffff] | |
| hover:border hover:border-[#25bd9c] | |
| focus:outline-none focus:ring mt-5" | |
| > | |
| Mint Now | |
| </button> | |
| </form> | |
| </div> | |
| </div> | |
| ) | |
| } | |
| export default CreateNFT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment