2022-02-04 03:04:56 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { PencilIcon } from '@heroicons/react/outline'
|
2022-02-04 03:19:42 +00:00
|
|
|
import Router from 'next/router'
|
2022-02-04 03:04:56 +00:00
|
|
|
|
|
|
|
import { AddFundsButton } from '../components/add-funds-button'
|
|
|
|
import { Page } from '../components/page'
|
|
|
|
import { SEO } from '../components/SEO'
|
|
|
|
import { Title } from '../components/title'
|
|
|
|
import { usePrivateUser, useUser } from '../hooks/use-user'
|
|
|
|
import { formatMoney } from '../../common/util/format'
|
|
|
|
import {
|
|
|
|
cleanDisplayName,
|
|
|
|
cleanUsername,
|
|
|
|
} from '../../common/util/clean-username'
|
|
|
|
import { changeUserInfo } from '../lib/firebase/api-call'
|
|
|
|
import { uploadImage } from '../lib/firebase/storage'
|
|
|
|
import { Col } from '../components/layout/col'
|
|
|
|
import { Row } from '../components/layout/row'
|
|
|
|
|
|
|
|
export default function ProfilePage() {
|
|
|
|
const user = useUser()
|
|
|
|
const privateUser = usePrivateUser(user?.id)
|
|
|
|
|
|
|
|
const [avatarUrl, setAvatarUrl] = useState(user?.avatarUrl || '')
|
|
|
|
const [avatarLoading, setAvatarLoading] = useState(false)
|
|
|
|
const [name, setName] = useState(user?.name || '')
|
|
|
|
const [username, setUsername] = useState(user?.username || '')
|
|
|
|
|
|
|
|
const [isEditing, setIsEditing] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) {
|
|
|
|
setAvatarUrl(user.avatarUrl || '')
|
|
|
|
setName(user.name || '')
|
|
|
|
setUsername(user.username || '')
|
|
|
|
}
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
const updateDisplayName = async () => {
|
|
|
|
const newName = cleanDisplayName(name)
|
|
|
|
|
|
|
|
if (newName) {
|
|
|
|
setName(newName)
|
|
|
|
|
|
|
|
await changeUserInfo({ name: newName })
|
|
|
|
.catch(() => ({ status: 'error' }))
|
|
|
|
.then((r) => {
|
|
|
|
if (r.status === 'error') setName(user?.name || '')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setName(user?.name || '')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateUsername = async () => {
|
|
|
|
const newUsername = cleanUsername(username)
|
|
|
|
|
|
|
|
if (newUsername) {
|
|
|
|
setUsername(newUsername)
|
|
|
|
await changeUserInfo({ username: newUsername })
|
|
|
|
.catch(() => ({ status: 'error' }))
|
|
|
|
.then((r) => {
|
|
|
|
if (r.status === 'error') setUsername(user?.username || '')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setUsername(user?.username || '')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileHandler = async (event: any) => {
|
|
|
|
const file = event.target.files[0]
|
|
|
|
|
|
|
|
setAvatarLoading(true)
|
|
|
|
|
|
|
|
await uploadImage(user?.username || 'default', file)
|
|
|
|
.then(async (url) => {
|
|
|
|
await changeUserInfo({ avatarUrl: url })
|
|
|
|
setAvatarUrl(url)
|
|
|
|
setAvatarLoading(false)
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setAvatarLoading(false)
|
|
|
|
setAvatarUrl(user?.avatarUrl || '')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:19:42 +00:00
|
|
|
if (user === null) {
|
|
|
|
Router.replace('/')
|
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:04:56 +00:00
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<SEO title="Profile" description="User profile settings" url="/profile" />
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="max-w-lg rounded bg-white p-6 shadow-md sm:mx-auto">
|
2022-02-04 03:04:56 +00:00
|
|
|
<Row className="justify-between">
|
|
|
|
<Title className="!mt-0" text="Profile" />
|
|
|
|
{isEditing ? (
|
|
|
|
<button
|
|
|
|
className="btn btn-primary"
|
|
|
|
onClick={() => setIsEditing(false)}
|
|
|
|
>
|
|
|
|
Done
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className="btn btn-ghost"
|
|
|
|
onClick={() => setIsEditing(true)}
|
|
|
|
>
|
2022-02-11 18:40:22 +00:00
|
|
|
<PencilIcon className="h-5 w-5" />{' '}
|
2022-02-04 03:04:56 +00:00
|
|
|
<div className="ml-2">Edit</div>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
<Col className="gap-4">
|
|
|
|
<Row className="items-center gap-4">
|
|
|
|
{avatarLoading ? (
|
|
|
|
<button className="btn btn-ghost btn-lg btn-circle loading"></button>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<img
|
|
|
|
src={avatarUrl}
|
|
|
|
width={80}
|
|
|
|
height={80}
|
2022-02-11 18:40:22 +00:00
|
|
|
className="flex items-center justify-center rounded-full bg-gray-400"
|
2022-02-04 03:04:56 +00:00
|
|
|
/>
|
|
|
|
{isEditing && (
|
|
|
|
<input type="file" name="file" onChange={fileHandler} />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label className="label">Display name</label>
|
|
|
|
|
|
|
|
{isEditing ? (
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="Display name"
|
|
|
|
className="input input-bordered"
|
|
|
|
value={name}
|
|
|
|
onChange={(e) => setName(e.target.value || '')}
|
|
|
|
onBlur={updateDisplayName}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="ml-1 text-gray-500">{name}</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label className="label">Username</label>
|
|
|
|
|
|
|
|
{isEditing ? (
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="Username"
|
|
|
|
className="input input-bordered"
|
|
|
|
value={username}
|
|
|
|
onChange={(e) => setUsername(e.target.value || '')}
|
|
|
|
onBlur={updateUsername}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="ml-1 text-gray-500">{username}</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label className="label">Email</label>
|
|
|
|
<div className="ml-1 text-gray-500">
|
|
|
|
{privateUser?.email ?? '\u00a0'}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label className="label">Balance</label>
|
2022-02-11 18:40:22 +00:00
|
|
|
<Row className="ml-1 items-start gap-4 text-gray-500">
|
2022-02-04 03:04:56 +00:00
|
|
|
{formatMoney(user?.balance || 0)}
|
|
|
|
<AddFundsButton />
|
|
|
|
</Row>
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|