// group-admin.tsx
// Copyright (C) 2020 Dwayne Harris
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
import React, { FC, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Link, useParams, useHistory } from 'react-router-dom'
import { faCheckCircle, faIdCard } from '@fortawesome/free-solid-svg-icons'
import { handleApiError } from '../../api/errors'
import { initForm, initField } from '../../actions/forms'
import { fetchGroup, updateGroup } from '../../actions/groups'
import { getEntity } from '../../selectors/entities'
import { getForm } from '../../selectors/forms'
import { useDeepCompareEffect, useTheme } from '../../hooks'
import { setTitle, valueFromForm } from '../../utils'
import {
AppState,
AppThunkDispatch,
Group,
GroupMembershipType,
Tab,
EntityType,
} from '../../types'
import Title from '../../components/title'
import Subtitle from '../../components/subtitle'
import Section from '../../components/section'
import HorizontalRule from '../../components/horizontal-rule'
import PrimaryButton from '../../components/controls/primary-button'
import MemberList from '../../components/member-list'
import GroupInvitations from '../../components/group-invitations'
import GroupLogs from '../../components/group-logs'
import Loading from '../../components/pages/loading'
import TextareaField from '../../components/controls/textarea-field'
import ImageField from '../../components/controls/image-field'
import CoverImageField from '../../components/controls/cover-image-field'
import IconImageField from '../../components/controls/icon-image-field'
import StaticField from '../../components/controls/static-field'
interface Params {
id: string
tab: string
}
const tabs: Tab[] = [
{ id: '', label: 'General' },
{ id: 'members', label: 'Members' },
{ id: 'logs', label: 'Logs' },
]
const GroupAdmin: FC = () => {
const theme = useTheme()
const { id, tab = '' } = useParams()
const history = useHistory()
const group = useSelector(state => getEntity(state, EntityType.Group, id))
const form = useSelector(getForm)
const dispatch = useDispatch()
useEffect(() => {
try {
dispatch(fetchGroup(id))
} catch (err) {
handleApiError(err, dispatch, history)
}
}, [])
useDeepCompareEffect(() => {
if (group && group.membership) {
if (group.membership !== GroupMembershipType.Admin) {
history.push(`/c/${group.id}`)
return
}
dispatch(initForm())
dispatch(initField('about', group.about))
dispatch(initField('expiration', '0'))
dispatch(initField('limit', '0'))
dispatch(initField('image', group.imageUrl))
dispatch(initField('coverImage', group.coverImageUrl))
dispatch(initField('iconImage', group.iconImageUrl))
setTitle(`${group.name} Administration`)
}
}, [group])
const handleUpdateGroup = async () => {
try {
await dispatch(updateGroup(id, {
about: valueFromForm(form, 'about'),
imageUrl: valueFromForm(form, 'image'),
coverImageUrl: valueFromForm(form, 'coverImage'),
iconImageUrl: valueFromForm(form, 'iconImage'),
}))
await dispatch(fetchGroup(id))
} catch (err) {
handleApiError(err, dispatch, history)
}
}
if (!group) return
return (