[ABANDONED] React/Redux front end for the Flexor social network.
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.

94 lines
3.0 KiB

5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC, useEffect } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import { useParams, useHistory } from 'react-router-dom'
  4. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  5. import { faArrowsAltV } from '@fortawesome/free-solid-svg-icons'
  6. import { handleApiError } from '../../api/errors'
  7. import { fetchPost } from '../../actions/posts'
  8. import { getAuthenticated, getChecked } from '../../selectors/authentication'
  9. import { getEntity } from '../../selectors/entities'
  10. import { getPostParents, getPostChildren } from '../../selectors/posts'
  11. import { setTitle } from '../../utils'
  12. import { AppState, AppThunkDispatch, EntityType, Post } from '../../types'
  13. import Title from '../../components/title'
  14. import Subtitle from '../../components/subtitle'
  15. import Section from '../../components/section'
  16. import Loading from '../../components/pages/loading'
  17. import PostComponent from '../../components/post'
  18. import PostList from '../../components/post-list'
  19. import Composer from '../../components/composer'
  20. interface Params {
  21. id: string
  22. }
  23. const ViewPost: FC = () => {
  24. const { id } = useParams<Params>()
  25. const post = useSelector<AppState, Post | undefined>(state => getEntity<Post>(state, EntityType.Post, id))
  26. const parents = useSelector<AppState, Post[]>(state => getPostParents(state, id))
  27. const replies = useSelector<AppState, Post[]>(state => getPostChildren(state, id))
  28. const checked = useSelector(getChecked)
  29. const authenticated = useSelector(getAuthenticated)
  30. const dispatch = useDispatch<AppThunkDispatch>()
  31. const history = useHistory()
  32. const fetch = async () => {
  33. try {
  34. await dispatch(fetchPost(id))
  35. } catch (err) {
  36. handleApiError(err, dispatch, history)
  37. }
  38. }
  39. useEffect(() => {
  40. if (checked) fetch()
  41. }, [checked, id])
  42. useEffect(() => {
  43. if (post) setTitle('Post')
  44. }, [post])
  45. if (!post) return <Loading />
  46. return (
  47. <div>
  48. <Section>
  49. <Title>Post</Title>
  50. </Section>
  51. {parents.length > 0 &&
  52. <div>
  53. <PostList posts={parents} collapseText="Show Older Posts" />
  54. <div style={{ textAlign: 'center' }}>
  55. <FontAwesomeIcon icon={faArrowsAltV} />
  56. </div>
  57. </div>
  58. }
  59. <PostComponent post={post} />
  60. {authenticated &&
  61. <div>
  62. <Section>
  63. <Subtitle>Reply</Subtitle>
  64. </Section>
  65. <Composer parent={post} onPost={fetch} />
  66. </div>
  67. }
  68. {replies.length > 0 &&
  69. <div>
  70. <Section>
  71. <Subtitle>Replies</Subtitle>
  72. </Section>
  73. <PostList posts={replies} />
  74. </div>
  75. }
  76. </div>
  77. )
  78. }
  79. export default ViewPost