Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

☀️ IPFS unpinning #83

Open
wants to merge 3 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions BFF/ipfs/unpin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pinataSDK from "@pinata/sdk";

import dotenv from "dotenv";
dotenv.config();

export async function unpin({ hash }: { hash: string }) {
console.log("hash", hash);
const PINATA_KEY = process.env.PINATA_KEY;
const PINATA_SECRET_KEY = process.env.PINATA_SECRET_KEY;

const pinata = new pinataSDK(PINATA_KEY, PINATA_SECRET_KEY);

try {
const result = await pinata.unpin(hash);
return result;
} catch (error) {
console.error("Error unpinning file on IPFS:", error);
return { mediaError: "Internal Server Error" };
}
}
50 changes: 48 additions & 2 deletions components/AvatarSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const AvatarSelection = ({
//=============================================================================
const fileInputRef = useRef(null);

console.log("familyDetails - on load", familyDetails);

const openFileInput = () => {
fileInputRef.current.click();
};
Expand Down Expand Up @@ -79,6 +81,19 @@ export const AvatarSelection = ({
}
};

const unpinFromIpfs = async (ipfsHash: string) => {
try {
// send as query param
const result = await axios.get(
`/api/ipfs/unpin-from-ipfs?ipfsHash=${ipfsHash}`
);
console.log(result.data);
return result.data.success;
} catch (e) {
console.error(e as Error);
}
};

const handleSubmit = async () => {
console.log("selectedFile", selectedFile);
setIsLoading(true);
Expand All @@ -101,11 +116,40 @@ export const AvatarSelection = ({
return;
}

setActiveStep(1);

const avatar = `https://ipfs.io/ipfs/${ifpsHash}`;
console.log(avatar);

// delete old avatar from ipfs
console.log("familyDetails?.avatarURI", familyDetails?.avatarURI);
console.log("avatar", avatar);
console.log(
"familyDetails?.avatarURI.includes(ipfs)",
familyDetails?.avatarURI.includes("ipfs")
);
console.log(
"familyDetails?.avatarURI !== avatar",
familyDetails?.avatarURI !== avatar
);
if (
familyDetails?.avatarURI &&
familyDetails?.avatarURI.includes("ipfs") &&
familyDetails?.avatarURI !== avatar
) {
const oldIpfsHash = familyDetails?.avatarURI.split("/")[4];
const result = await unpinFromIpfs(oldIpfsHash);
if (result.success === false) {
toast({
title: "Error",
description: "Failed to delete old avatar from IPFS",
status: "error",
});
setIsLoading(false);
return;
}
}

setActiveStep(1);

const body = {
...familyDetails,
avatarURI: avatar,
Expand All @@ -116,6 +160,8 @@ export const AvatarSelection = ({
value: body,
};

console.log("payload", payload);

await axios.post(`/api/vercel/set-json`, payload);
setUserDetails(body);
fetchFamilyDetails();
Expand Down
28 changes: 28 additions & 0 deletions components/ComingSoon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Box, Flex, Heading, Text, Image } from "@chakra-ui/react";

const ComingSoon = () => {
const Logo = () => {
return (
<Flex align="center" ml={5} mt={5}>
<Image src={"/pig_logo.png"} alt="Loader" width="50" height="50" />
<Heading size="lg" ml={5}>
Defikids
</Heading>
</Flex>
);
};

return (
<Box h="100vh">
{Logo()}
<Flex direction="column" align="center" justify="center" h="90vh">
<Heading size={"lg"}>Coming Soon</Heading>
<Text my={5} color="gray" fontSize="lg">
We are working hard to bring you the best experience.
</Text>
</Flex>
</Box>
);
};

export default ComingSoon;
1 change: 1 addition & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { chains, wagmiConfig } from "@/services/wagmi/wagmiConfig";
import "@rainbow-me/rainbowkit/styles.css";
import { RainbowKitProvider } from "@rainbow-me/rainbowkit";
import { WagmiConfig } from "wagmi";
import ComingSoon from "../components/ComingSoon";

function MyApp({ Component, pageProps }) {
const [hasCheckedUserType, setHasCheckedUserType] = useState(false);
Expand Down
22 changes: 22 additions & 0 deletions pages/api/ipfs/unpin-from-ipfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextApiRequest, NextApiResponse } from "next";
import { unpin } from "@/BFF/ipfs/unpin";

export default async function unpinFromIpfsRoute(
req: NextApiRequest,
res: NextApiResponse
) {
const { ipfsHash } = req.query as { ipfsHash: string };
console.log("ipfsHash - req.query", ipfsHash);

try {
const result = await unpin({ hash: ipfsHash });
return res.status(200).json({ success: true });
} catch (error) {
if (error.reason === "CURRENT_USER_HAS_NOT_PINNED_CID") {
return res.status(200).json({ success: true });
} else {
console.error("Error unpinning file on IPFS:", error);
return { mediaError: "Internal Server Error" };
}
}
}