You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.7 KiB
84 lines
2.7 KiB
import React, { FC, useEffect } from 'react'
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
import { useParams, useHistory } from 'react-router-dom'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { faArrowsAltV } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import { handleApiError } from 'src/api/errors'
|
|
import { fetchPost } from 'src/actions/posts'
|
|
import { getAuthenticated, getChecked } from 'src/selectors/authentication'
|
|
import { getEntity } from 'src/selectors/entities'
|
|
import { getPostParents, getPostChildren } from 'src/selectors/posts'
|
|
|
|
import { setTitle } from 'src/utils'
|
|
import { AppState, AppThunkDispatch, EntityType, Post } from 'src/types'
|
|
|
|
import Title from 'src/components/title'
|
|
import Loading from 'src/components/pages/loading'
|
|
import PostComponent from 'src/components/post'
|
|
import PostList from 'src/components/post-list'
|
|
import Composer from 'src/components/composer'
|
|
|
|
interface Params {
|
|
id: string
|
|
}
|
|
|
|
const ViewPost: FC = () => {
|
|
const { id } = useParams<Params>()
|
|
const post = useSelector<AppState, Post | undefined>(state => getEntity<Post>(state, EntityType.Post, id))
|
|
const parents = useSelector<AppState, Post[]>(state => getPostParents(state, id))
|
|
const replies = useSelector<AppState, Post[]>(state => getPostChildren(state, id))
|
|
const checked = useSelector<AppState, boolean>(getChecked)
|
|
const authenticated = useSelector<AppState, boolean>(getAuthenticated)
|
|
const dispatch = useDispatch<AppThunkDispatch>()
|
|
const history = useHistory()
|
|
|
|
const fetch = async () => {
|
|
try {
|
|
await dispatch(fetchPost(id))
|
|
} catch (err) {
|
|
handleApiError(err, dispatch, history)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (checked) fetch()
|
|
}, [checked, id])
|
|
|
|
useEffect(() => {
|
|
if (post) setTitle('Post')
|
|
}, [post])
|
|
|
|
if (!post) return <Loading />
|
|
|
|
return (
|
|
<div>
|
|
{parents.length > 0 &&
|
|
<div>
|
|
<PostList posts={parents} collapseText="Show Older Posts" />
|
|
<div style={{ textAlign: 'center' }}>
|
|
<FontAwesomeIcon icon={faArrowsAltV} />
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
<PostComponent post={post} />
|
|
|
|
{authenticated &&
|
|
<div>
|
|
<Title>Reply</Title>
|
|
<Composer parent={post} onPost={fetch} />
|
|
</div>
|
|
}
|
|
|
|
{replies.length > 0 &&
|
|
<div>
|
|
<Title>Replies</Title>
|
|
<PostList posts={replies} />
|
|
</div>
|
|
}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ViewPost
|