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

87 lines
3.0 KiB

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