2024-03-20 00:37:31 +00:00
|
|
|
import Layout from '@/components/layout'
|
2021-09-24 21:28:21 +00:00
|
|
|
import { gql, useMutation, useQuery } from '@apollo/client'
|
2024-03-20 00:37:31 +00:00
|
|
|
import UserHeader from '@/components/user-header'
|
2023-07-24 18:35:05 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
2024-03-20 00:37:31 +00:00
|
|
|
import styles from '@/styles/user.module.css'
|
2021-09-23 17:42:00 +00:00
|
|
|
import { useState } from 'react'
|
2024-03-20 00:37:31 +00:00
|
|
|
import ItemFull from '@/components/item-full'
|
|
|
|
import { Form, MarkdownInput } from '@/components/form'
|
|
|
|
import { useMe } from '@/components/me'
|
|
|
|
import { USER_FULL } from '@/fragments/users'
|
|
|
|
import { ITEM_FIELDS } from '@/fragments/items'
|
|
|
|
import { getGetServerSideProps } from '@/api/ssrApollo'
|
|
|
|
import { FeeButtonProvider } from '@/components/fee-button'
|
|
|
|
import { bioSchema } from '@/lib/validate'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2024-03-20 00:37:31 +00:00
|
|
|
import PageLoading from '@/components/page-loading'
|
|
|
|
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 })
|
2024-07-01 17:02:29 +00:00
|
|
|
if (error) throw error
|
2023-11-13 02:31:39 +00:00
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-23 15:32:20 +00:00
|
|
|
export function UserLayout ({ user, children, containClassName }) {
|
2023-07-23 15:08:43 +00:00
|
|
|
return (
|
2024-05-03 21:55:07 +00:00
|
|
|
<Layout user={user} footer footerLinks={false} containClassName={containClassName}>
|
2023-07-23 15:08:43 +00:00
|
|
|
<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()
|
2024-09-12 18:05:11 +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 (
|
2024-02-23 15:32:20 +00:00
|
|
|
<UserLayout user={user} containClassName={!user.bio && mine && styles.contain}>
|
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
|
|
|
}
|