Skip to content

Commit

Permalink
Issue #59 (#64)
Browse files Browse the repository at this point in the history
* update

* Update index.tsx

* update

* Update index.tsx

* Update index.tsx

* Update index.tsx

* fix: add missing auth header

* fix: pool page taking in params

* fix: add mongo types + fix patch request

---------

Co-authored-by: marcuspang <[email protected]>
  • Loading branch information
kevincompton and marcuspang authored Aug 2, 2023
1 parent 9e5e7de commit aafebed
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
25 changes: 25 additions & 0 deletions hooks/useGetCurrentUser.tsx
Original file line number Diff line number Diff line change
@@ -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<User | undefined> {
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 !== '' });
}
6 changes: 3 additions & 3 deletions pages/pool/[id]/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<FundingPoolProjectHeader />
<ReadyToLaunchComponent />
<FundingPoolProjectHeader />
<ReadyToLaunchComponent />
</>
);
}
55 changes: 50 additions & 5 deletions pages/update-profile/index.tsx
Original file line number Diff line number Diff line change
@@ -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<User>();
const onSubmit: SubmitHandler<User> = (data) => console.log(data);
} = useForm<User>({
mode: "onBlur",
defaultValues: {
name: data ? data.name : '',
socials: data ? data.socials : '',
bio: data ? data.bio : ''
}
});

const onSubmit: SubmitHandler<User> = (formData) => {
try {
if (data)
updateUser({ ...formData, _id: data._id }, idToken)
} catch (error) {
console.log(error)
}
};

return (
<form
Expand All @@ -29,27 +62,39 @@ export default function UpdateProfile() {
<div className="grow pr-4">
<label>Your name</label>
<input
{...register(`name`)}
{...register(`name`, { required: "Name is required" })}
className="w-full input-field mb-2"
placeholder="Name"
/>
<span className="text-red-600 text-xs">
{" "}
{errors.name && errors.name.message}
</span>
</div>
<div className="grow">
<label>Where people can find you</label>
<input
{...register(`socials`)}
{...register(`socials`, { required: "Social is required" })}
className="w-full input-field mb-2"
placeholder="https://"
/>
<span className="text-red-600 text-xs">
{" "}
{errors.socials && errors.socials.message}
</span>
</div>
</div>
<hr className="border-b-1 border-slate-200 my-8" />
<label>Your Bio</label>
<textarea
{...register(`bio`)}
{...register(`bio`, { required: "Bio is required" })}
className="w-full input-field mb-2"
placeholder="Something about yourself..."
/>
<span className="text-red-600 text-xs">
{" "}
{errors.bio && errors.bio.message}
</span>
<hr className="border-b-1 border-slate-200 my-8" />
<button
className="bg-black text-white rounded leading-10 px-5"
Expand Down
21 changes: 19 additions & 2 deletions types/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,32 @@ export type Pool = {
};

export type User = {
_id: string;
name: string;
profile?: string;
bio?: string;
socials?: string;
wallet_address: string;
wallets: WalletResolvable[];
email?: string;
session_cookie: string;
};

export type Wallet = {
type: string;
};

export type SocialLoginWallet = {
public_key: string;
curve: string;
} & Wallet;

export type ExternalWallet = {
address: string;
} & Wallet;

export type WalletResolvable = Partial<SocialLoginWallet> &
Partial<ExternalWallet> &
Wallet;

type Sponsor = {
logo: string;
name: string;
Expand Down

0 comments on commit aafebed

Please sign in to comment.