Skip to content

Commit

Permalink
fix: add mongo types + fix patch request
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspang committed Aug 2, 2023
1 parent f24c01b commit 0eb1270
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
17 changes: 10 additions & 7 deletions pages/update-profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ 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, id: number) {
const res = await fetch(`${process.env.BACKEND_URL}/users/${id}`, {
method: "patch",
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 ${id}`,
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,
Expand All @@ -30,12 +34,11 @@ export default function UpdateProfile() {
bio: data ? data.bio : ''
}
});
console.log({ data })

const onSubmit: SubmitHandler<User> = (formData) => {
try {
if (data)
// @ts-ignore ts doesn't like mongoose id's
updateUser(formData, data._id)
updateUser({ ...formData, _id: data._id }, idToken)
} catch (error) {
console.log(error)
}
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 0eb1270

Please sign in to comment.