2021-09-30 15:46:58 +00:00
|
|
|
import Layout from '../../components/layout'
|
2021-09-24 21:28:21 +00:00
|
|
|
import { gql, useMutation, useQuery } from '@apollo/client'
|
2021-09-30 15:46:58 +00:00
|
|
|
import UserHeader from '../../components/user-header'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
2021-09-30 15:46:58 +00:00
|
|
|
import styles from '../../styles/user.module.css'
|
2021-09-23 17:42:00 +00:00
|
|
|
import { useState } from 'react'
|
2021-09-30 15:46:58 +00:00
|
|
|
import ItemFull from '../../components/item-full'
|
2023-11-11 00:18:10 +00:00
|
|
|
import { Form, MarkdownInput } from '../../components/form'
|
2021-09-30 15:46:58 +00:00
|
|
|
import { useMe } from '../../components/me'
|
|
|
|
import { USER_FULL } from '../../fragments/users'
|
|
|
|
import { ITEM_FIELDS } from '../../fragments/items'
|
2021-10-26 20:49:37 +00:00
|
|
|
import { getGetServerSideProps } from '../../api/ssrApollo'
|
2023-11-19 20:16:35 +00:00
|
|
|
import { FeeButtonProvider } from '../../components/fee-button'
|
2023-02-08 19:38:04 +00:00
|
|
|
import { bioSchema } from '../../lib/validate'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import PageLoading from '../../components/page-loading'
|
2023-11-19 20:16:35 +00:00
|
|
|
import { ItemButtonBar } from '../../components/post'
|
2021-04-22 22:14:32 +00:00
|
|
|
|
2023-08-28 17:52:15 +00:00
|
|
|
export const getServerSideProps = getGetServerSideProps({
|
|
|
|
query: USER_FULL,
|
|
|
|
notFound: data => !data.user
|
|
|
|
})
|
2021-04-22 22:14:32 +00:00
|
|
|
|
2023-06-12 17:35:28 +00:00
|
|
|
export function BioForm ({ handleDone, bio }) {
|
2021-09-24 21:28:21 +00:00
|
|
|
const [upsertBio] = useMutation(
|
2021-09-23 17:42:00 +00:00
|
|
|
gql`
|
2021-09-24 21:28:21 +00:00
|
|
|
${ITEM_FIELDS}
|
|
|
|
mutation upsertBio($bio: String!) {
|
|
|
|
upsertBio(bio: $bio) {
|
2021-09-23 17:42:00 +00:00
|
|
|
id
|
2021-09-24 21:28:21 +00:00
|
|
|
bio {
|
|
|
|
...ItemFields
|
|
|
|
text
|
|
|
|
}
|
2021-09-23 17:42:00 +00:00
|
|
|
}
|
|
|
|
}`, {
|
2021-09-24 21:28:21 +00:00
|
|
|
update (cache, { data: { upsertBio } }) {
|
2021-09-23 17:42:00 +00:00
|
|
|
cache.modify({
|
2021-09-24 21:28:21 +00:00
|
|
|
id: `User:${upsertBio.id}`,
|
2021-09-23 17:42:00 +00:00
|
|
|
fields: {
|
|
|
|
bio () {
|
2021-09-24 21:28:21 +00:00
|
|
|
return upsertBio.bio
|
2021-09-23 17:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.createFormContainer}>
|
2023-11-13 02:31:39 +00:00
|
|
|
<FeeButtonProvider>
|
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
bio: bio?.text || ''
|
|
|
|
}}
|
|
|
|
schema={bioSchema}
|
|
|
|
onSubmit={async values => {
|
|
|
|
const { error } = await upsertBio({ variables: values })
|
|
|
|
if (error) {
|
|
|
|
throw new Error({ message: error.toString() })
|
|
|
|
}
|
|
|
|
handleDone?.()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<MarkdownInput
|
|
|
|
topLevel
|
|
|
|
name='bio'
|
|
|
|
minRows={6}
|
|
|
|
/>
|
2023-11-19 20:16:35 +00:00
|
|
|
<ItemButtonBar createText='save' onCancel={handleDone} />
|
2023-11-13 02:31:39 +00:00
|
|
|
</Form>
|
|
|
|
</FeeButtonProvider>
|
2021-09-23 17:42:00 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
export function UserLayout ({ user, children }) {
|
|
|
|
return (
|
|
|
|
<Layout user={user} containClassName={styles.contain}>
|
|
|
|
<UserHeader user={user} />
|
|
|
|
{children}
|
|
|
|
</Layout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function User ({ ssrData }) {
|
2021-09-23 17:42:00 +00:00
|
|
|
const [create, setCreate] = useState(false)
|
2021-09-24 21:28:21 +00:00
|
|
|
const [edit, setEdit] = useState(false)
|
2023-07-23 15:08:43 +00:00
|
|
|
const router = useRouter()
|
2021-09-23 22:18:48 +00:00
|
|
|
const me = useMe()
|
2021-09-30 15:46:58 +00:00
|
|
|
|
2023-07-26 00:45:35 +00:00
|
|
|
const { data } = useQuery(USER_FULL, { variables: { ...router.query } })
|
2023-07-23 15:08:43 +00:00
|
|
|
if (!data && !ssrData) return <PageLoading />
|
2021-09-24 21:28:21 +00:00
|
|
|
|
2023-07-23 15:08:43 +00:00
|
|
|
const { user } = data || ssrData
|
2021-09-24 21:28:21 +00:00
|
|
|
const mine = me?.name === user.name
|
|
|
|
|
2021-04-22 22:14:32 +00:00
|
|
|
return (
|
2023-07-23 15:08:43 +00:00
|
|
|
<UserLayout user={user}>
|
2021-09-23 17:42:00 +00:00
|
|
|
{user.bio
|
2021-09-24 21:28:21 +00:00
|
|
|
? (edit
|
|
|
|
? (
|
|
|
|
<div className={styles.create}>
|
2023-06-12 17:35:28 +00:00
|
|
|
<BioForm bio={user.bio} handleDone={() => setEdit(false)} />
|
2021-09-24 21:28:21 +00:00
|
|
|
</div>)
|
|
|
|
: <ItemFull item={user.bio} bio handleClick={setEdit} />
|
|
|
|
)
|
|
|
|
: (mine &&
|
2021-09-23 17:42:00 +00:00
|
|
|
<div className={styles.create}>
|
|
|
|
{create
|
2023-06-12 17:35:28 +00:00
|
|
|
? <BioForm handleDone={() => setCreate(false)} />
|
2021-09-23 17:42:00 +00:00
|
|
|
: (
|
2021-09-24 21:28:21 +00:00
|
|
|
mine &&
|
2021-11-23 21:23:25 +00:00
|
|
|
<div className='text-center'>
|
|
|
|
<Button
|
|
|
|
onClick={setCreate}
|
|
|
|
size='md' variant='secondary'
|
|
|
|
>create bio
|
|
|
|
</Button>
|
2023-07-09 17:37:12 +00:00
|
|
|
<small className='d-block mt-3 text-muted'>your bio is also a post introducing yourself to other stackers</small>
|
2021-11-23 21:23:25 +00:00
|
|
|
</div>
|
2021-09-23 17:42:00 +00:00
|
|
|
)}
|
|
|
|
</div>)}
|
2023-07-23 15:08:43 +00:00
|
|
|
</UserLayout>
|
2021-04-22 22:14:32 +00:00
|
|
|
)
|
2021-04-14 23:56:29 +00:00
|
|
|
}
|