Skip to content

Commit

Permalink
updated next example
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Sep 11, 2024
1 parent 6f9bc63 commit 151855f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
21 changes: 21 additions & 0 deletions examples/next-starter/app/api/files/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextResponse, NextRequest } from "next/server";
import { pinata } from "@/utils/config";

export const config = {
api: {
bodyParser: false,
},
};

export async function POST(request: NextRequest) {
try {
const data = await request.formData();
const file: File | null = data.get("file") as unknown as File;
const uploadData = await pinata.upload.file(file);
const url = await pinata.gateways.convert(uploadData.IpfsHash);
return NextResponse.json(url, { status: 200 });
} catch (e) {
console.log(e);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}
76 changes: 38 additions & 38 deletions examples/next-starter/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
"use client";

import { useState, useRef } from "react";
import { pinata } from "@/utils/config";
import { useState } from "react";

export default function Home() {
const [file, setFile]: any = useState();
const [cid, setCid] = useState("");
const [uploading, setUploading] = useState(false);
const [file, setFile] = useState<File>();
const [url, setUrl] = useState("");
const [uploading, setUploading] = useState(false);

const inputFile = useRef(null);
const uploadFile = async () => {
try {
setUploading(true);
if (!file) {
alert("No file selected");
return;
}
const data = new FormData();
data.set("file", file);
const uploadRequest = await fetch("/api/files", {
method: "POST",
body: data,
});
const url = await uploadRequest.json();
setUrl(url);
setUploading(false);
} catch (e) {
console.log(e);
setUploading(false);
alert("Trouble uploading file");
}
};

const uploadFile = async () => {
try {
setUploading(true);
const keyRequest = await fetch("/api/key");
const keyData = await keyRequest.json();
const upload = await pinata.upload.file(file).key(keyData.JWT);
setCid(upload.IpfsHash);
setUploading(false);
} catch (e) {
console.log(e);
setUploading(false);
alert("Trouble uploading file");
}
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setFile(e.target?.files?.[0]);
};

const handleChange = (e) => {
setFile(e.target.files[0]);
};

return (
<main className="w-full min-h-screen m-auto flex flex-col justify-center items-center">
<input type="file" id="file" ref={inputFile} onChange={handleChange} />
<button disabled={uploading} onClick={uploadFile}>
{uploading ? "Uploading..." : "Upload"}
</button>
{cid && (
<img
src={`https://${process.env.NEXT_PUBLIC_GATEWAY_URL}/ipfs/${cid}`}
alt="Image from IPFS"
/>
)}
</main>
);
return (
<main className="w-full min-h-screen m-auto flex flex-col justify-center items-center">
<input type="file" onChange={handleChange} />
<button disabled={uploading} onClick={uploadFile}>
{uploading ? "Uploading..." : "Upload"}
</button>
{url && <img src={url} alt="Image from Pinata" />}
</main>
);
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 151855f

Please sign in to comment.