2022-11-10 02:11:36 -05:00
|
|
|
"use client"
|
|
|
|
|
2022-11-09 21:38:05 -05:00
|
|
|
import { Note, Input, Textarea, Button, useToasts } from "@geist-ui/core/dist"
|
2022-11-14 20:26:37 -05:00
|
|
|
import { User } from "@lib/server/prisma"
|
2022-11-12 04:28:06 -05:00
|
|
|
import { useState } from "react"
|
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({
|
|
|
|
text: "Please fill out at least one field",
|
|
|
|
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: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
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({
|
|
|
|
text: "Profile updated",
|
|
|
|
type: "success"
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setToast({
|
|
|
|
text: "Something went wrong updating your profile",
|
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 01:14:08 -04:00
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Note type="warning" marginBottom={"var(--gap)"}>
|
|
|
|
This information will be publicly available on your profile
|
|
|
|
</Note>
|
|
|
|
<form
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
gap: "var(--gap)",
|
|
|
|
maxWidth: "300px"
|
|
|
|
}}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="displayName">Display name</label>
|
|
|
|
<Input
|
|
|
|
id="displayName"
|
|
|
|
width={"100%"}
|
|
|
|
placeholder="my name"
|
|
|
|
value={name || ""}
|
|
|
|
onChange={handleNameChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="email">Email</label>
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
htmlType="email"
|
|
|
|
width={"100%"}
|
|
|
|
placeholder="my@email.io"
|
2022-11-14 02:02:31 -05:00
|
|
|
value={user.email || undefined}
|
|
|
|
disabled
|
2022-04-22 01:01:59 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label htmlFor="bio">Biography (max 250 characters)</label>
|
|
|
|
<Textarea
|
|
|
|
id="bio"
|
|
|
|
width="100%"
|
|
|
|
maxLength={250}
|
|
|
|
placeholder="I enjoy..."
|
|
|
|
value={bio || ""}
|
|
|
|
onChange={handleBioChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button htmlType="submit" auto>
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
)
|
2022-04-20 01:14:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-22 01:01:59 -04:00
|
|
|
export default Profile
|