Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincompton committed Aug 2, 2023
1 parent 9e5e7de commit c50091b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
17 changes: 17 additions & 0 deletions hooks/useGetSignedUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { User } from "@/types/mongo";
import { useQuery } from "wagmi";

async function getSignedUser(): Promise<User[]> {
try {
return fetch(`${process.env.BACKEND_URL}/users/@me`).then((res) =>
res.json()
);
} catch (e) {
console.error(e);
return [];
}
}

export function useGetSignedUser() {
return useQuery(["users"], getSignedUser);
}
32 changes: 30 additions & 2 deletions pages/update-profile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import { AdminNav } from "@/components/AdminNav";
import { useForm, SubmitHandler } from "react-hook-form";
import { User } from "@/types/mongo";
import { useGetSignedUser } from "@/hooks/useGetSignedUser";

async function updateUser(values:User, id:number) {
const res = await fetch(`${process.env.BACKEND_URL}/users/${id}`, {
method: "put",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${id}`,
},
body: JSON.stringify(values),
});
return await res.json();
}

export default function UpdateProfile() {
const { data } = useGetSignedUser()
const {
register,
handleSubmit,
watch,
formState: { errors },
} = useForm<User>();
const onSubmit: SubmitHandler<User> = (data) => console.log(data);
} = useForm<User>({
mode: "onBlur",
defaultValues: {
name: data ? data[0].name : '',
socials: data ? data[0].socials : '',
bio: data ? data[0].bio : ''
}
});
const onSubmit: SubmitHandler<User> = (data) => {
try {
// @ts-ignore
updateUser(data, data._id)
} catch (error) {
console.log(error)
}
};

return (
<form
Expand Down

0 comments on commit c50091b

Please sign in to comment.