diff --git a/widget/IpfsImageUpload.jsx b/widget/IpfsImageUpload.jsx new file mode 100644 index 0000000..4952eea --- /dev/null +++ b/widget/IpfsImageUpload.jsx @@ -0,0 +1,104 @@ +const IPFSImageUpload = ({ + key, + name, + className, + img, + setImg, + msg, + setMsg, + onError, + accepts, + multiple, + clickable, + maxFiles, + maxFileSize, + minFileSize, + dragActiveClassName, +}) => { + const attributes = { + key, + name, + className, + img, + setImg, + msg, + setMsg, + onError, + accepts, + multiple, + clickable, + maxFiles, + maxFileSize, + minFileSize, + dragActiveClassName, + }; + const ipfsUrl = (cid) => `https://ipfs.near.social/ipfs/${cid}`; + const uploadFile = (files) => { + setMsg("Uploading..."); + + const file = files[0]; + + const uploadPromise = asyncFetch("https://ipfs.near.social/add", { + method: "POST", + headers: { + "Content-Type": file.type, + }, + body: file, + }) + .then((response) => { + if (!response.ok) { + setMsg("Upload failed!"); + return Promise.reject(new Error("Upload failed")); + } + return response.body; + }) + .then((data) => { + console.log(data); + setImg(data); + }) + .catch((error) => { + console.error("Upload error:", error); + setMsg("Upload failed!"); + }) + .finally(() => { + setMsg("Replace Image"); + }); + + uploadPromise + .then(() => { + console.log("Upload successful!"); + }) + .catch((error) => { + console.error("Upload failed:", error); + }); + }; + return ( +
+ {img?.cid && ( +
+ upload preview +
+ )} + + {msg} + +
+ ); +}; + +return { IPFSImageUpload };