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

172 lines
6.2 KiB

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
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
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. // group-admin.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 { Link, useParams, useHistory } from 'react-router-dom'
  16. import { faCheckCircle, faIdCard } from '@fortawesome/free-solid-svg-icons'
  17. import { handleApiError } from '../../api/errors'
  18. import { initForm, initField } from '../../actions/forms'
  19. import { fetchGroup, updateGroup } from '../../actions/groups'
  20. import { getEntity } from '../../selectors/entities'
  21. import { getForm } from '../../selectors/forms'
  22. import { useDeepCompareEffect, useTheme } from '../../hooks'
  23. import { setTitle, valueFromForm } from '../../utils'
  24. import {
  25. AppState,
  26. AppThunkDispatch,
  27. Group,
  28. GroupMembershipType,
  29. Tab,
  30. EntityType,
  31. } from '../../types'
  32. import Title from '../../components/title'
  33. import Subtitle from '../../components/subtitle'
  34. import Section from '../../components/section'
  35. import HorizontalRule from '../../components/horizontal-rule'
  36. import PrimaryButton from '../../components/controls/primary-button'
  37. import MemberList from '../../components/member-list'
  38. import GroupInvitations from '../../components/group-invitations'
  39. import GroupLogs from '../../components/group-logs'
  40. import Loading from '../../components/pages/loading'
  41. import TextareaField from '../../components/controls/textarea-field'
  42. import ImageField from '../../components/controls/image-field'
  43. import CoverImageField from '../../components/controls/cover-image-field'
  44. import IconImageField from '../../components/controls/icon-image-field'
  45. import StaticField from '../../components/controls/static-field'
  46. interface Params {
  47. id: string
  48. tab: string
  49. }
  50. const tabs: Tab[] = [
  51. { id: '', label: 'General' },
  52. { id: 'members', label: 'Members' },
  53. { id: 'logs', label: 'Logs' },
  54. ]
  55. const GroupAdmin: FC = () => {
  56. const theme = useTheme()
  57. const { id, tab = '' } = useParams<Params>()
  58. const history = useHistory()
  59. const group = useSelector<AppState, Group | undefined>(state => getEntity<Group>(state, EntityType.Group, id))
  60. const form = useSelector(getForm)
  61. const dispatch = useDispatch<AppThunkDispatch>()
  62. useEffect(() => {
  63. try {
  64. dispatch(fetchGroup(id))
  65. } catch (err) {
  66. handleApiError(err, dispatch, history)
  67. }
  68. }, [])
  69. useDeepCompareEffect(() => {
  70. if (group && group.membership) {
  71. if (group.membership !== GroupMembershipType.Admin) {
  72. history.push(`/c/${group.id}`)
  73. return
  74. }
  75. dispatch(initForm())
  76. dispatch(initField('about', group.about))
  77. dispatch(initField('expiration', '0'))
  78. dispatch(initField('limit', '0'))
  79. dispatch(initField('image', group.imageUrl))
  80. dispatch(initField('coverImage', group.coverImageUrl))
  81. dispatch(initField('iconImage', group.iconImageUrl))
  82. setTitle(`${group.name} Administration`)
  83. }
  84. }, [group])
  85. const handleUpdateGroup = async () => {
  86. try {
  87. await dispatch(updateGroup(id, {
  88. about: valueFromForm<string>(form, 'about'),
  89. imageUrl: valueFromForm<string>(form, 'image'),
  90. coverImageUrl: valueFromForm<string>(form, 'coverImage'),
  91. iconImageUrl: valueFromForm<string>(form, 'iconImage'),
  92. }))
  93. await dispatch(fetchGroup(id))
  94. } catch (err) {
  95. handleApiError(err, dispatch, history)
  96. }
  97. }
  98. if (!group) return <Loading />
  99. return (
  100. <div>
  101. <Section>
  102. <Title>{group.name}</Title>
  103. <Subtitle>Administration</Subtitle>
  104. <HorizontalRule />
  105. <div className="tabs" style={{ backgroundColor: theme.backgroundSecondary }}>
  106. {tabs.map(t => (
  107. <div key={t.id} className={tab === t.id ? 'active': ''} style={{ borderColor: theme.primary }}>
  108. <Link style={{ color: theme.secondary}} to={`/c/${group.id}/admin/${t.id}`}>
  109. {t.label}
  110. </Link>
  111. </div>
  112. ))}
  113. </div>
  114. <div>
  115. {tab === '' &&
  116. <div>
  117. <StaticField label="ID" icon={faIdCard} value={group.id} />
  118. <StaticField label="Name" value={group.name} />
  119. <TextareaField name="about" label="About" placeholder="About this Community" />
  120. <ImageField name="image" />
  121. <CoverImageField name="coverImage" />
  122. <IconImageField name="iconImage" />
  123. <br />
  124. <PrimaryButton text="Save" icon={faCheckCircle} onClick={e => handleUpdateGroup()} />
  125. </div>
  126. }
  127. {tab === 'members' &&
  128. <div>
  129. <GroupInvitations group={id} />
  130. <HorizontalRule />
  131. <Subtitle>Members</Subtitle>
  132. <MemberList group={id} />
  133. </div>
  134. }
  135. {tab === 'logs' &&
  136. <div>
  137. <GroupLogs group={id} />
  138. </div>
  139. }
  140. </div>
  141. </Section>
  142. </div>
  143. )
  144. }
  145. export default GroupAdmin