[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.

110 lines
3.6 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. // view-post.tsx
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import React, { FC, useEffect } from 'react'
  14. import { useSelector, useDispatch } from 'react-redux'
  15. import { useParams, useHistory } from 'react-router-dom'
  16. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  17. import { faArrowsAltV } from '@fortawesome/free-solid-svg-icons'
  18. import { handleApiError } from '../../api/errors'
  19. import { fetchPost } from '../../actions/posts'
  20. import { getAuthenticated, getChecked } from '../../selectors/authentication'
  21. import { getEntity } from '../../selectors/entities'
  22. import { getPostParents, getPostChildren } from '../../selectors/posts'
  23. import { setTitle } from '../../utils'
  24. import { AppState, AppThunkDispatch, EntityType, Post } from '../../types'
  25. import Title from '../../components/title'
  26. import Subtitle from '../../components/subtitle'
  27. import Section from '../../components/section'
  28. import Loading from '../../components/pages/loading'
  29. import PostComponent from '../../components/post'
  30. import PostList from '../../components/post-list'
  31. import Composer from '../../components/composer'
  32. interface Params {
  33. id: string
  34. }
  35. const ViewPost: FC = () => {
  36. const { id } = useParams<Params>()
  37. const post = useSelector<AppState, Post | undefined>(state => getEntity<Post>(state, EntityType.Post, id))
  38. const parents = useSelector<AppState, Post[]>(state => getPostParents(state, id))
  39. const replies = useSelector<AppState, Post[]>(state => getPostChildren(state, id))
  40. const checked = useSelector(getChecked)
  41. const authenticated = useSelector(getAuthenticated)
  42. const dispatch = useDispatch<AppThunkDispatch>()
  43. const history = useHistory()
  44. const fetch = async () => {
  45. try {
  46. await dispatch(fetchPost(id))
  47. } catch (err) {
  48. handleApiError(err, dispatch, history)
  49. }
  50. }
  51. useEffect(() => {
  52. if (checked) fetch()
  53. }, [checked, id])
  54. useEffect(() => {
  55. if (post) setTitle('Post')
  56. }, [post])
  57. if (!post) return <Loading />
  58. return (
  59. <div>
  60. <Section>
  61. <Title>Post</Title>
  62. </Section>
  63. {parents.length > 0 &&
  64. <div>
  65. <PostList posts={parents} collapseText="Show Older Posts" />
  66. <div style={{ textAlign: 'center' }}>
  67. <FontAwesomeIcon icon={faArrowsAltV} />
  68. </div>
  69. </div>
  70. }
  71. <PostComponent post={post} />
  72. {authenticated &&
  73. <div>
  74. <Section>
  75. <Subtitle>Reply</Subtitle>
  76. </Section>
  77. <Composer parent={post} onPost={fetch} />
  78. </div>
  79. }
  80. {replies.length > 0 &&
  81. <div>
  82. <Section>
  83. <Subtitle>Replies</Subtitle>
  84. </Section>
  85. <PostList posts={replies} />
  86. </div>
  87. }
  88. </div>
  89. )
  90. }
  91. export default ViewPost