stacker.news/components/user-header.js

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-05-21 22:32:21 +00:00
import { Button } from 'react-bootstrap'
import { useSession } from 'next-auth/client'
2021-04-22 22:14:32 +00:00
import Link from 'next/link'
import { useRouter } from 'next/router'
import Nav from 'react-bootstrap/Nav'
2021-05-21 22:32:21 +00:00
import { useState } from 'react'
import { Form, Input, SubmitButton } from './form'
import InputGroup from 'react-bootstrap/InputGroup'
import * as Yup from 'yup'
2021-05-22 00:09:11 +00:00
import { gql, useApolloClient, useMutation } from '@apollo/client'
2021-05-25 00:08:56 +00:00
import styles from './user-header.module.css'
2021-05-21 22:32:21 +00:00
const NAME_QUERY =
gql`
query nameAvailable($name: String!) {
nameAvailable(name: $name)
}
`
2021-04-22 22:14:32 +00:00
2021-05-22 00:09:11 +00:00
const NAME_MUTATION =
gql`
mutation setName($name: String!) {
setName(name: $name)
}
`
2021-04-22 22:14:32 +00:00
export default function UserHeader ({ user }) {
2021-05-21 22:32:21 +00:00
const [editting, setEditting] = useState(false)
const [session] = useSession()
2021-04-22 22:14:32 +00:00
const router = useRouter()
2021-05-21 22:32:21 +00:00
const client = useApolloClient()
2021-05-22 00:09:11 +00:00
const [setName] = useMutation(NAME_MUTATION)
2021-05-21 22:32:21 +00:00
2021-08-18 13:52:45 +00:00
const Satistics = () => <h1 className='ml-2'><small className='text-success'>{user.sats} sats \ {user.stacked} stacked</small></h1>
2021-05-21 22:32:21 +00:00
const UserSchema = Yup.object({
name: Yup.string()
.required('required')
.matches(/^[\w_]+$/, 'only letters, numbers, and _')
.max(32, 'too long')
.test({
name: 'name',
test: async name => {
if (!name || !name.length) return false
const { data } = await client.query({ query: NAME_QUERY, variables: { name } })
return data.nameAvailable
},
message: 'taken'
})
})
2021-04-22 22:14:32 +00:00
return (
<>
2021-05-21 22:32:21 +00:00
{editting
? (
<Form
className='d-flex align-items-center flex-wrap'
schema={UserSchema}
initial={{
name: user.name
}}
2021-05-25 00:08:56 +00:00
validateImmediately
2021-05-22 00:09:11 +00:00
onSubmit={async ({ name }) => {
if (name === user.name) {
setEditting(false)
return
}
const { error } = await setName({ variables: { name } })
if (error) {
throw new Error({ message: error.toString() })
}
window.location = `/${name}`
}}
2021-05-21 22:32:21 +00:00
>
<Input
prepend=<InputGroup.Text>@</InputGroup.Text>
name='name'
autoFocus
2021-05-25 00:08:56 +00:00
groupClassName={`mb-0 ${styles.username}`}
2021-05-21 22:32:21 +00:00
showValid
/>
<Satistics user={user} />
<SubmitButton className='ml-2' variant='info' size='sm' onClick={() => setEditting(true)}>save</SubmitButton>
</Form>
)
: (
<div className='d-flex align-items-center flex-wrap'>
<h1>@{user.name}</h1>
<Satistics user={user} />
{session && session.user && session.user.name === user.name &&
<Button className='ml-2' variant='boost' size='sm' onClick={() => setEditting(true)}>edit</Button>}
</div>
)}
2021-04-22 22:14:32 +00:00
<Nav
activeKey={router.asPath}
>
<Nav.Item>
<Link href={'/' + user.name} passHref>
<Nav.Link>{user.nitems} posts</Nav.Link>
</Link>
</Nav.Item>
<Nav.Item>
<Link href={'/' + user.name + '/comments'} passHref>
<Nav.Link>{user.ncomments} comments</Nav.Link>
</Link>
</Nav.Item>
2021-05-11 15:52:50 +00:00
{/* <Nav.Item>
2021-04-22 22:14:32 +00:00
<Link href={'/' + user.name + '/sativity'} passHref>
<Nav.Link>sativity</Nav.Link>
</Link>
2021-05-11 15:52:50 +00:00
</Nav.Item> */}
2021-04-22 22:14:32 +00:00
</Nav>
</>
)
}