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

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