2022-11-10 02:11:36 -05:00
|
|
|
"use client"
|
|
|
|
|
2022-11-18 02:39:52 -05:00
|
|
|
import Button from "@components/button"
|
|
|
|
import Input from "@components/input"
|
|
|
|
import Note from "@components/note"
|
2022-11-28 21:36:11 -05:00
|
|
|
import { useToasts } from "@components/toasts"
|
2022-11-14 21:39:42 -05:00
|
|
|
import { User } from "next-auth"
|
2022-11-18 01:36:53 -05:00
|
|
|
import { useState } from "react"
|
2022-11-30 01:10:51 -05:00
|
|
|
import styles from "./profile.module.css"
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
const Profile = ({ user }: { user: User }) => {
|
2022-11-14 20:26:37 -05:00
|
|
|
// TODO: make this displayName, requires fetching user from DB as session doesnt have it
|
2022-11-12 02:59:33 -05:00
|
|
|
const [name, setName] = useState<string>(user.name || "")
|
2022-04-22 01:01:59 -04:00
|
|
|
const [bio, setBio] = useState<string>()
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
const { setToast } = useToasts()
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setName(e.target.value)
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
const handleBioChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
setBio(e.target.value)
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
e.preventDefault()
|
2022-11-14 02:02:31 -05:00
|
|
|
if (!name && !bio) {
|
2022-04-22 01:01:59 -04:00
|
|
|
setToast({
|
2022-11-28 21:36:11 -05:00
|
|
|
message: "Please fill out at least one field",
|
2022-04-22 01:01:59 -04:00
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
const data = {
|
|
|
|
displayName: name,
|
|
|
|
bio
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const res = await fetch(`/api/user/${user.id}`, {
|
2022-04-22 01:01:59 -04:00
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
2022-11-18 01:36:53 -05:00
|
|
|
"Content-Type": "application/json"
|
2022-04-22 01:01:59 -04:00
|
|
|
},
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
})
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
if (res.status === 200) {
|
|
|
|
setToast({
|
2022-11-28 21:36:11 -05:00
|
|
|
message: "Profile updated",
|
2022-04-22 01:01:59 -04:00
|
|
|
type: "success"
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setToast({
|
2022-11-28 21:36:11 -05:00
|
|
|
message: "Something went wrong updating your profile",
|
2022-04-22 01:01:59 -04:00
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
return (
|
|
|
|
<>
|
2022-11-18 02:39:52 -05:00
|
|
|
<Note type="warning">
|
2022-04-22 01:01:59 -04:00
|
|
|
This information will be publicly available on your profile
|
|
|
|
</Note>
|
2022-11-30 01:10:51 -05:00
|
|
|
<form onSubmit={onSubmit} className={styles.form}>
|
2022-04-22 01:01:59 -04:00
|
|
|
<div>
|
|
|
|
<label htmlFor="displayName">Display name</label>
|
|
|
|
<Input
|
|
|
|
id="displayName"
|
|
|
|
width={"100%"}
|
|
|
|
placeholder="my name"
|
|
|
|
value={name || ""}
|
|
|
|
onChange={handleNameChange}
|
2022-11-28 21:36:11 -05:00
|
|
|
aria-label="Display name"
|
2022-11-30 01:10:51 -05:00
|
|
|
minLength={1}
|
|
|
|
maxLength={32}
|
2022-04-22 01:01:59 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="email">Email</label>
|
|
|
|
<Input
|
|
|
|
id="email"
|
2022-11-18 02:39:52 -05:00
|
|
|
type="email"
|
2022-04-22 01:01:59 -04:00
|
|
|
width={"100%"}
|
|
|
|
placeholder="my@email.io"
|
2022-11-14 02:02:31 -05:00
|
|
|
value={user.email || undefined}
|
|
|
|
disabled
|
2022-11-28 21:36:11 -05:00
|
|
|
aria-label="Email"
|
2022-04-22 01:01:59 -04:00
|
|
|
/>
|
|
|
|
</div>
|
2022-11-30 01:10:51 -05:00
|
|
|
<Button type="submit">Submit</Button>
|
2022-04-22 01:01:59 -04:00
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
)
|
2022-04-20 01:14:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
export default Profile
|