130 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-09-30 10:46:58 -05:00
import Layout from '../../components/layout'
2021-09-24 16:28:21 -05:00
import { gql, useMutation, useQuery } from '@apollo/client'
2021-09-30 10:46:58 -05:00
import UserHeader from '../../components/user-header'
import Seo from '../../components/seo'
2021-09-23 12:42:00 -05:00
import { Button } from 'react-bootstrap'
2021-09-30 10:46:58 -05:00
import styles from '../../styles/user.module.css'
2021-09-23 12:42:00 -05:00
import { useState } from 'react'
2021-09-30 10:46:58 -05:00
import ItemFull from '../../components/item-full'
import { Form, MarkdownInput, SubmitButton } from '../../components/form'
2021-09-23 15:25:38 -05:00
import TextareaAutosize from 'react-textarea-autosize'
2021-09-30 10:46:58 -05:00
import { useMe } from '../../components/me'
import { USER_FULL } from '../../fragments/users'
import { ITEM_FIELDS } from '../../fragments/items'
2021-10-26 15:49:37 -05:00
import { getGetServerSideProps } from '../../api/ssrApollo'
2022-08-18 13:15:24 -05:00
import FeeButton, { EditFeeButton } from '../../components/fee-button'
2023-02-08 13:38:04 -06:00
import { bioSchema } from '../../lib/validate'
2023-06-12 14:34:10 -05:00
import CancelButton from '../../components/cancel-button'
2021-04-22 17:14:32 -05:00
export const getServerSideProps = getGetServerSideProps(USER_FULL, null,
data => !data.user)
2021-04-22 17:14:32 -05:00
export function BioForm ({ handleDone, bio }) {
2021-09-24 16:28:21 -05:00
const [upsertBio] = useMutation(
2021-09-23 12:42:00 -05:00
gql`
2021-09-24 16:28:21 -05:00
${ITEM_FIELDS}
mutation upsertBio($bio: String!) {
upsertBio(bio: $bio) {
2021-09-23 12:42:00 -05:00
id
2021-09-24 16:28:21 -05:00
bio {
...ItemFields
text
}
2021-09-23 12:42:00 -05:00
}
}`, {
2021-09-24 16:28:21 -05:00
update (cache, { data: { upsertBio } }) {
2021-09-23 12:42:00 -05:00
cache.modify({
2021-09-24 16:28:21 -05:00
id: `User:${upsertBio.id}`,
2021-09-23 12:42:00 -05:00
fields: {
bio () {
2021-09-24 16:28:21 -05:00
return upsertBio.bio
2021-09-23 12:42:00 -05:00
}
}
})
}
}
)
return (
<div className={styles.createFormContainer}>
2021-09-23 15:25:38 -05:00
<Form
initial={{
2021-09-24 16:28:21 -05:00
bio: bio?.text || ''
2021-09-23 15:25:38 -05:00
}}
2023-02-08 13:38:04 -06:00
schema={bioSchema}
2021-09-23 15:25:38 -05:00
onSubmit={async values => {
2021-09-24 16:28:21 -05:00
const { error } = await upsertBio({ variables: values })
2021-09-23 12:42:00 -05:00
if (error) {
throw new Error({ message: error.toString() })
}
handleDone?.()
2021-09-23 12:42:00 -05:00
}}
2021-09-23 15:25:38 -05:00
>
<MarkdownInput
topLevel
2021-09-23 15:25:38 -05:00
name='bio'
as={TextareaAutosize}
2021-11-12 16:39:52 -06:00
minRows={6}
2021-09-23 15:25:38 -05:00
/>
2023-06-12 14:34:10 -05:00
<div className='d-flex mt-3 justify-content-end'>
<CancelButton onClick={handleDone} />
2022-08-10 10:06:31 -05:00
{bio?.text
2022-08-18 13:15:24 -05:00
? <EditFeeButton
2022-09-27 16:19:15 -05:00
paidSats={bio?.meSats}
2022-08-18 13:15:24 -05:00
parentId={null} text='save' ChildButton={SubmitButton} variant='secondary'
/>
2022-08-10 10:06:31 -05:00
: <FeeButton
2022-09-27 16:19:15 -05:00
baseFee={1} parentId={null} text='create'
2022-08-10 10:06:31 -05:00
ChildButton={SubmitButton} variant='secondary'
/>}
</div>
2021-09-23 15:25:38 -05:00
</Form>
2021-09-23 12:42:00 -05:00
</div>
)
}
2021-10-26 15:49:37 -05:00
export default function User ({ data: { user } }) {
2021-09-23 12:42:00 -05:00
const [create, setCreate] = useState(false)
2021-09-24 16:28:21 -05:00
const [edit, setEdit] = useState(false)
2021-09-23 17:18:48 -05:00
const me = useMe()
2021-09-30 10:46:58 -05:00
2021-10-26 15:49:37 -05:00
const { data } = useQuery(USER_FULL, { variables: { name: user.name } })
2021-09-24 16:28:21 -05:00
if (data) {
({ user } = data)
}
const mine = me?.name === user.name
2021-04-22 17:14:32 -05:00
return (
2021-09-23 12:42:00 -05:00
<Layout noSeo containClassName={styles.contain}>
2021-07-07 19:15:27 -05:00
<Seo user={user} />
2021-04-22 17:14:32 -05:00
<UserHeader user={user} />
2021-09-23 12:42:00 -05:00
{user.bio
2021-09-24 16:28:21 -05:00
? (edit
? (
<div className={styles.create}>
<BioForm bio={user.bio} handleDone={() => setEdit(false)} />
2021-09-24 16:28:21 -05:00
</div>)
: <ItemFull item={user.bio} bio handleClick={setEdit} />
)
: (mine &&
2021-09-23 12:42:00 -05:00
<div className={styles.create}>
{create
? <BioForm handleDone={() => setCreate(false)} />
2021-09-23 12:42:00 -05:00
: (
2021-09-24 16:28:21 -05:00
mine &&
2021-11-23 15:23:25 -06:00
<div className='text-center'>
<Button
onClick={setCreate}
size='md' variant='secondary'
>create bio
</Button>
2023-07-09 12:37:12 -05:00
<small className='d-block mt-3 text-muted'>your bio is also a post introducing yourself to other stackers</small>
2021-11-23 15:23:25 -06:00
</div>
2021-09-23 12:42:00 -05:00
)}
</div>)}
2021-04-22 17:14:32 -05:00
</Layout>
)
2021-04-14 18:56:29 -05:00
}