diff --git a/hooks/useGetCurrentUser.tsx b/hooks/useGetCurrentUser.tsx new file mode 100644 index 0000000..5005359 --- /dev/null +++ b/hooks/useGetCurrentUser.tsx @@ -0,0 +1,25 @@ +import { AuthContext } from "@/components/AuthProvider"; +import { User } from "@/types/mongo"; +import { useContext } from "react"; +import { useQuery } from "wagmi"; + +async function getCurrentUser(idToken: string): Promise { + try { + return fetch(`${process.env.BACKEND_URL}/users/@me`, { + headers: { + Authorization: `Bearer ${idToken}`, + } + }).then((res) => + res.json() + ); + } catch (e) { + console.error(e); + return; + } +} + +export function useGetCurrentUser() { + const { idToken } = useContext(AuthContext); + + return useQuery(["users"], () => getCurrentUser(idToken), { enabled: idToken !== '' }); +} diff --git a/pages/pool/[id]/index.tsx b/pages/pool/[id]/index.tsx index a536293..8ba6a2b 100644 --- a/pages/pool/[id]/index.tsx +++ b/pages/pool/[id]/index.tsx @@ -1,11 +1,11 @@ import { FundingPoolProjectHeader } from "@/devlink"; import { ReadyToLaunchComponent } from "@/devlink/ReadyToLaunchComponent"; -export default function PoolPage({ params }: { params: { slug: string } }) { +export default function PoolPage() { return ( <> - - + + ); } diff --git a/pages/update-profile/index.tsx b/pages/update-profile/index.tsx index 675fdf9..2238827 100644 --- a/pages/update-profile/index.tsx +++ b/pages/update-profile/index.tsx @@ -1,15 +1,48 @@ import { AdminNav } from "@/components/AdminNav"; import { useForm, SubmitHandler } from "react-hook-form"; import { User } from "@/types/mongo"; +import { useGetCurrentUser } from "@/hooks/useGetCurrentUser"; +import { useContext } from "react"; +import { AuthContext } from "@/components/AuthProvider"; +async function updateUser(values: User, idToken: string) { + const res = await fetch(`${process.env.BACKEND_URL}/users/${values._id}`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${idToken}`, + }, + body: JSON.stringify(values), + }); + return await res.json(); +} + +// TODO: create separate form schema distinct from User export default function UpdateProfile() { + const { data } = useGetCurrentUser() + const { idToken } = useContext(AuthContext) const { register, handleSubmit, watch, formState: { errors }, - } = useForm(); - const onSubmit: SubmitHandler = (data) => console.log(data); + } = useForm({ + mode: "onBlur", + defaultValues: { + name: data ? data.name : '', + socials: data ? data.socials : '', + bio: data ? data.bio : '' + } + }); + + const onSubmit: SubmitHandler = (formData) => { + try { + if (data) + updateUser({ ...formData, _id: data._id }, idToken) + } catch (error) { + console.log(error) + } + }; return (
+ + {" "} + {errors.name && errors.name.message} +
+ + {" "} + {errors.socials && errors.socials.message} +