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

85 lines
2.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
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 { fetchPendingApps, activateApp, setPreinstall } from '../../actions/apps'
  6. import { getAuthenticatedUser, getChecked } from '../../selectors/authentication'
  7. import { getPendingApps } from '../../selectors/apps'
  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 AppListItem from '../../components/app-list-item'
  15. import HorizontalRule from '../../components/horizontal-rule'
  16. import PrimaryButton from '../../components/controls/primary-button'
  17. import SecondaryButton from '../../components/controls/secondary-button'
  18. const AdminApps: FC = () => {
  19. useAuthenticationCheck()
  20. const theme = useTheme()
  21. const checked = useSelector(getChecked)
  22. const user = useSelector(getAuthenticatedUser)
  23. const apps = useSelector(getPendingApps)
  24. const dispatch = useDispatch<AppThunkDispatch>()
  25. const history = useHistory()
  26. const handleActivate = async (id: string) => {
  27. try {
  28. await dispatch(activateApp(id))
  29. await dispatch(fetchPendingApps())
  30. } catch (err) {
  31. handleApiError(err, dispatch, history)
  32. }
  33. }
  34. const handleSetPreinstall = async (id: string) => {
  35. try {
  36. await dispatch(setPreinstall(id))
  37. await dispatch(fetchPendingApps())
  38. } catch (err) {
  39. handleApiError(err, dispatch, history)
  40. }
  41. }
  42. useEffect(() => {
  43. setTitle('Admin / Apps')
  44. const init = async () => {
  45. try {
  46. await dispatch(fetchPendingApps())
  47. } catch (err) {
  48. handleApiError(err, dispatch, history)
  49. }
  50. }
  51. if (checked) init()
  52. }, [checked])
  53. if (!user) return <Loading />
  54. if (checked && !user.admin) history.push('/')
  55. return (
  56. <div>
  57. <Section>
  58. <Title>Pending Apps</Title>
  59. <HorizontalRule />
  60. </Section>
  61. {apps.map(app => (
  62. <div className="list-item" style={{ borderColor: theme.backgroundSecondary }} key={app.id}>
  63. <AppListItem app={app} />
  64. <div className="buttons">
  65. <PrimaryButton text="Activate" onClick={() => handleActivate(app.id)} />
  66. <SecondaryButton text="Set Preinstall" onClick={() => handleSetPreinstall(app.id)} />
  67. </div>
  68. </div>
  69. ))}
  70. </div>
  71. )
  72. }
  73. export default AdminApps