Skip to content

Commit

Permalink
feat: connects user wallet while creating profile
Browse files Browse the repository at this point in the history
  • Loading branch information
yashhhguptaaa committed May 10, 2024
1 parent 695cc34 commit 5659ffa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
40 changes: 24 additions & 16 deletions components/ProfileCreationForm/ProfileCreationForm.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"use client";


// TODO:
// 1) Only upload image to IPFS
// 2) Call to supabase (add trpc?)
// 3) Use react hook form
// Fix color schemes
// Add connect to wallet while creating profile and get address
//
// TODO:
// 1) Only upload image to IPFS (DONE)
// 2) Call to supabase (add trpc?)
// 3) Use react hook form
// Fix color schemes
// Add connect to wallet while creating profile and get address (DONE)
//

import React, { useState } from "react";


import SubmitLinksSection, { TLink } from "./SubmitLinksSection";
import UserMetaDetailsSection, {
TUserMetaDetails,
Expand All @@ -20,9 +18,13 @@ import { IconGhost } from "@tabler/icons-react";
import { BottomGradient } from "./GradiantComponents";
import { cn } from "@/lib/utils";
import { useWeb3StorageUtilities } from "@/lib/hooks/useWeb3StorageUtilities";
import { useConnectWallet } from "@/lib/hooks/useConnectWallet";

const ProfileCreationForm = () => {
const { uploadFileToWeb3Storage } = useWeb3StorageUtilities();
const { isConnected, handleConnectWallet, isSignedIn, handleSignIn } =
useConnectWallet();

const [userMetaDetails, setUserMetaDetails] = useState<TUserMetaDetails>({
username: "",
bio: "",
Expand All @@ -40,19 +42,25 @@ const ProfileCreationForm = () => {

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

const body = {
userName: userMetaDetails.username,
bio: userMetaDetails.bio,

if (!isConnected) {
handleConnectWallet();
}
if (!isSignedIn) {
await handleSignIn();
}

// Upload image to IPFS
const formImages = {
profileImage: userMetaDetails.profileImage,
backgroundImage: userMetaDetails.backgroundImage,
links: linksData.map((link) => ({
icon: link.icon,
name: link.name,
url: link.url,
})),
};
const cid = await uploadFileToWeb3Storage<typeof body>({ payload: body });
const cid = await uploadFileToWeb3Storage<typeof formImages>({
payload: formImages,
});
const ipfsUrl = `ipfs://${cid}`;
console.log("ipfsUrl:", ipfsUrl);
};
Expand Down
43 changes: 43 additions & 0 deletions lib/hooks/useConnectWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useSIWE, useModal, SIWESession } from "connectkit";
import { useAccount } from "wagmi";

export const useConnectWallet = () => {
const { setOpen } = useModal();
const { isConnected } = useAccount();

const { data, isReady, isRejected, isLoading, isSignedIn, signOut, signIn } =
useSIWE({
onSignIn: (session?: SIWESession) => {
// Do something with the data
},
onSignOut: () => {
// Do something when signed out
},
});

const handleSignIn = async () => {
await signIn()?.then((session?: SIWESession) => {
// Do something when signed in
});
};

const handleSignOut = async () => {
await signOut()?.then(() => {
// Do something when signed out
});
};

return {
isSignedIn,
isConnected,
handleSignIn,
handleSignOut,
isReady,
isRejected,
isLoading,
handleConnectWallet: function () {
setOpen(true);
},
data,
};
};

0 comments on commit 5659ffa

Please sign in to comment.