CoastalCommitsPastes/client/app/settings/components/sections/profile.tsx

100 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-11-10 02:11:36 -05:00
"use client"
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"
import styles from "./profile.module.css"
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
const [name, setName] = useState<string>(user.name || "")
const [bio, setBio] = useState<string>()
const { setToast } = useToasts()
const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setName(e.target.value)
}
const handleBioChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setBio(e.target.value)
}
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (!name && !bio) {
setToast({
2022-11-28 21:36:11 -05:00
message: "Please fill out at least one field",
type: "error"
})
return
}
const data = {
displayName: name,
bio
}
const res = await fetch(`/api/user/${user.id}`, {
method: "PUT",
headers: {
2022-11-18 01:36:53 -05:00
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
if (res.status === 200) {
setToast({
2022-11-28 21:36:11 -05:00
message: "Profile updated",
type: "success"
})
} else {
setToast({
2022-11-28 21:36:11 -05:00
message: "Something went wrong updating your profile",
type: "error"
})
}
}
return (
<>
<Note type="warning">
This information will be publicly available on your profile
</Note>
<form onSubmit={onSubmit} className={styles.form}>
<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"
minLength={1}
maxLength={32}
/>
</div>
<div>
<label htmlFor="email">Email</label>
<Input
id="email"
type="email"
width={"100%"}
placeholder="my@email.io"
value={user.email || undefined}
disabled
2022-11-28 21:36:11 -05:00
aria-label="Email"
/>
</div>
<Button type="submit">Submit</Button>
</form>
</>
)
}
export default Profile