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

71 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import React, { FC, useEffect } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import { useHistory } from 'react-router-dom'
  4. import { useAuthenticationCheck, useTheme } from '../../hooks'
  5. import { fetchPendingGroups, activateGroup } from '../../actions/groups'
  6. import { getAuthenticatedUser, getChecked } from '../../selectors/authentication'
  7. import { getPendingGroups } from '../../selectors/groups'
  8. import { handleApiError } from '../../api/errors'
  9. import { setTitle } from '../../utils'
  10. import { AppThunkDispatch } from '../../types'
  11. import Section from '../../components/section'
  12. import Title from '../../components/title'
  13. import Loading from '../../components/pages/loading'
  14. import GroupListItem from '../../components/group-list-item'
  15. import HorizontalRule from '../../components//horizontal-rule'
  16. import PrimaryButton from '../../components/controls/primary-button'
  17. const AdminGroups: FC = () => {
  18. useAuthenticationCheck()
  19. const theme = useTheme()
  20. const checked = useSelector(getChecked)
  21. const user = useSelector(getAuthenticatedUser)
  22. const groups = useSelector(getPendingGroups)
  23. const dispatch = useDispatch<AppThunkDispatch>()
  24. const history = useHistory()
  25. const handleClick = async (id: string) => {
  26. try {
  27. await dispatch(activateGroup(id))
  28. await dispatch(fetchPendingGroups())
  29. } catch (err) {
  30. handleApiError(err, dispatch, history)
  31. }
  32. }
  33. useEffect(() => {
  34. setTitle('Admin / Groups')
  35. const init = async () => {
  36. try {
  37. await dispatch(fetchPendingGroups())
  38. } catch (err) {
  39. handleApiError(err, dispatch, history)
  40. }
  41. }
  42. if (checked) init()
  43. }, [checked])
  44. if (!user) return <Loading />
  45. if (checked && !user.admin) history.push('/')
  46. return (
  47. <div>
  48. <Section>
  49. <Title>Pending Groups</Title>
  50. <HorizontalRule />
  51. </Section>
  52. {groups.map(group => (
  53. <div className="list-item" style={{ borderColor: theme.backgroundSecondary }} key={group.id}>
  54. <GroupListItem group={group} />
  55. <PrimaryButton text="Activate" onClick={() => handleClick(group.id)} />
  56. </div>
  57. ))}
  58. </div>
  59. )
  60. }
  61. export default AdminGroups