stacker.news/pages/[username]/index.js

140 lines
3.6 KiB
JavaScript
Raw Normal View History

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'
import Seo from '../../components/seo'
2021-09-23 17:42:00 +00:00
import { Button } from 'react-bootstrap'
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'
2021-09-23 20:25:38 +00:00
import * as Yup from 'yup'
2021-09-30 15:46:58 +00:00
import { Form, MarkdownInput, SubmitButton } from '../../components/form'
import ActionTooltip from '../../components/action-tooltip'
2021-09-23 20:25:38 +00:00
import TextareaAutosize from 'react-textarea-autosize'
2021-09-30 15:46:58 +00:00
import { useMe } from '../../components/me'
import { USER_FULL } from '../../fragments/users'
2021-09-24 21:28:21 +00:00
import { useRouter } from 'next/router'
2021-09-30 15:46:58 +00:00
import { ITEM_FIELDS } from '../../fragments/items'
import getSSRApolloClient from '../../api/ssrApollo'
2021-04-22 22:14:32 +00:00
2021-09-30 15:46:58 +00:00
export async function getServerSideProps ({ req, params: { username } }) {
const client = await getSSRApolloClient(req)
const { error, data } = await client.query({
query: USER_FULL(username)
2021-04-22 22:14:32 +00:00
})
2021-09-30 15:46:58 +00:00
if (error || !data?.user) {
2021-04-22 22:14:32 +00:00
return {
notFound: true
}
}
return {
props: {
2021-09-30 15:46:58 +00:00
user: data.user
2021-04-22 22:14:32 +00:00
}
}
}
2021-09-23 20:25:38 +00:00
const BioSchema = Yup.object({
bio: Yup.string().required('required').trim()
})
2021-09-24 21:28:21 +00:00
export function BioForm ({ handleSuccess, bio }) {
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}>
2021-09-23 20:25:38 +00:00
<Form
initial={{
2021-09-24 21:28:21 +00:00
bio: bio?.text || ''
2021-09-23 20:25:38 +00:00
}}
schema={BioSchema}
onSubmit={async values => {
2021-09-24 21:28:21 +00:00
const { error } = await upsertBio({ variables: values })
2021-09-23 17:42:00 +00:00
if (error) {
throw new Error({ message: error.toString() })
}
2021-09-24 21:28:21 +00:00
handleSuccess && handleSuccess()
2021-09-23 17:42:00 +00:00
}}
2021-09-23 20:25:38 +00:00
>
<MarkdownInput
name='bio'
as={TextareaAutosize}
minRows={4}
/>
<ActionTooltip>
2021-09-24 21:28:21 +00:00
<SubmitButton variant='secondary' className='mt-3'>{bio?.text ? 'save' : 'create'}</SubmitButton>
2021-09-23 20:25:38 +00:00
</ActionTooltip>
</Form>
2021-09-23 17:42:00 +00:00
</div>
)
}
2021-04-22 22:14:32 +00:00
export default function User ({ user }) {
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)
2021-09-23 22:18:48 +00:00
const me = useMe()
2021-09-24 21:28:21 +00:00
const router = useRouter()
2021-09-30 15:46:58 +00:00
const { data } = useQuery(USER_FULL(user.name), {
2021-09-24 21:28:21 +00:00
fetchPolicy: router.query.cache ? 'cache-first' : undefined
})
if (data) {
({ user } = data)
}
const mine = me?.name === user.name
2021-04-22 22:14:32 +00:00
return (
2021-09-23 17:42:00 +00:00
<Layout noSeo containClassName={styles.contain}>
2021-07-08 00:15:27 +00:00
<Seo user={user} />
2021-04-22 22:14:32 +00:00
<UserHeader 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}>
<BioForm bio={user.bio} handleSuccess={() => setEdit(false)} />
</div>)
: <ItemFull item={user.bio} bio handleClick={setEdit} />
)
: (mine &&
2021-09-23 17:42:00 +00:00
<div className={styles.create}>
{create
2021-09-24 21:28:21 +00:00
? <BioForm handleSuccess={() => setCreate(false)} />
2021-09-23 17:42:00 +00:00
: (
2021-09-24 21:28:21 +00:00
mine &&
<Button
onClick={setCreate}
size='md' variant='secondary'
>create bio
</Button>
2021-09-23 17:42:00 +00:00
)}
</div>)}
2021-04-22 22:14:32 +00:00
</Layout>
)
2021-04-14 23:56:29 +00:00
}